Sfoglia il codice sorgente

Added schedule for checks

Andrew Kane 8 anni fa
parent
commit
ea2e544503

+ 4 - 0
CHANGELOG.md

@@ -1,3 +1,7 @@
+## 1.3.0 [unreleased]
+
+- Added schedule for checks
+
 ## 1.2.1
 
 - Fixed checks

+ 1 - 1
app/controllers/blazer/checks_controller.rb

@@ -41,7 +41,7 @@ module Blazer
     private
 
     def check_params
-      params.require(:check).permit(:query_id, :emails, :invert)
+      params.require(:check).permit(:query_id, :emails, :invert, :schedule)
     end
 
     def set_check

+ 2 - 0
app/models/blazer/check.rb

@@ -19,6 +19,8 @@ module Blazer
           invert ? "failing" : "passing"
         end
 
+      self.last_run_at = Time.now if self.respond_to?(:last_run_at=)
+
       # do not notify on creation, except when not passing
       if (state_was || state != "passing") && state != state_was && emails.present?
         Blazer::CheckMailer.state_change(self, state, state_was, rows.size, error).deliver_later

+ 22 - 10
app/views/blazer/checks/_form.html.erb

@@ -17,17 +17,29 @@
     </script>
   </div>
 
-<% if @check.respond_to?(:invert) %>
-  <div class="form-group">
-    <%= f.label "Fails if" %>
-    <div class="hide">
-      <%= f.select :invert, [["Any results (bad data)", false], ["No results (missing data)", true]], {}, {id: "select-invert"} %>
+  <% if @check.respond_to?(:invert) %>
+    <div class="form-group">
+      <%= f.label :invert, "Fails if" %>
+      <div class="hide">
+        <%= f.select :invert, [["Any results (bad data)", false], ["No results (missing data)", true]] %>
+      </div>
+      <script>
+        $("#check_invert").selectize({}).parent().removeClass("hide");
+      </script>
     </div>
-    <script>
-      $("#select-invert").selectize({}).parent().removeClass("hide");
-    </script>
-  </div>
-<% end %>
+  <% end %>
+
+  <% if @check.respond_to?(:schedule) %>
+    <div class="form-group">
+      <%= f.label :schedule, "Run every" %>
+      <div class="hide">
+        <%= f.select :schedule, ["1 day", "1 hour", "5 minutes"].map { |v| [v, v] } %>
+      </div>
+      <script>
+        $("#check_schedule").selectize({}).parent().removeClass("hide");
+      </script>
+    </div>
+  <% end %>
 
   <div class="form-group">
     <%= f.label :emails %>

+ 3 - 1
app/views/blazer/checks/index.html.erb

@@ -8,7 +8,8 @@
   <thead>
     <tr>
       <th>Query</th>
-      <th style="width: 15%;">State</th>
+      <th style="width: 10%;">State</th>
+      <th style="width: 10%;">Run</th>
       <th style="width: 20%;">Emails</th>
       <th style="width: 15%;"></th>
     </tr>
@@ -22,6 +23,7 @@
             <small style="font-weight: bold; color: <%= colors[check.state.to_sym] %>;"><%= check.state.upcase %></small>
           <% end %>
         </td>
+        <td><%= check.schedule if check.respond_to?(:schedule) %></td>
         <td>
           <ul class="list-unstyled" style="margin-bottom: 0;">
             <% check.split_emails.each do |email| %>

+ 4 - 2
lib/blazer.rb

@@ -50,8 +50,10 @@ module Blazer
     end
   end
 
-  def self.run_checks
-    Blazer::Check.includes(:query).find_each do |check|
+  def self.run_checks(schedule: nil)
+    checks = Blazer::Check.includes(:query)
+    checks = checks.where(schedule: schedule) if schedule
+    checks.find_each do |check|
       rows = nil
       error = nil
       tries = 0

+ 1 - 1
lib/blazer/tasks.rb

@@ -3,7 +3,7 @@ require "rake"
 namespace :blazer do
   desc "run checks"
   task run_checks: :environment do
-    Blazer.run_checks
+    Blazer.run_checks(schedule: ENV["SCHEDULE"])
   end
 
   task send_failing_checks: :environment do

+ 3 - 0
lib/generators/blazer/templates/install.rb

@@ -32,7 +32,10 @@ class <%= migration_class_name %> < ActiveRecord::Migration
     create_table :blazer_checks do |t|
       t.references :query
       t.string :state
+      t.string :schedule
       t.text :emails
+      t.boolean :invert
+      t.timestamp :last_run_at
       t.timestamps
     end
   end