Merge pull request #314 from cantino/alias1-196-disable-agents

Alias1's Disabled Agents patch

Andrew Cantino 10 ans auparavant
Parent
Commettre
ee05a9bc68

+ 9 - 1
app/assets/stylesheets/application.css.scss.erb

@@ -134,8 +134,16 @@ span.not-applicable:after {
134 134
   height: 400px;
135 135
 }
136 136
 
137
+// Disabled
138
+
139
+tr.agent-disabled {
140
+  td {
141
+    opacity: 0.5;
142
+  }
143
+}
144
+
137 145
 // Fix JSON Editor
138 146
 
139 147
 .json-editor blockquote {
140 148
   font-size: 14px;
141
-}
149
+}

+ 7 - 1
app/controllers/agents_controller.rb

@@ -113,7 +113,13 @@ class AgentsController < ApplicationController
113 113
 
114 114
     respond_to do |format|
115 115
       if @agent.update_attributes(params[:agent])
116
-        format.html { redirect_to agents_path, notice: 'Your Agent was successfully updated.' }
116
+        format.html {
117
+          if params[:return] == "show"
118
+            redirect_to agent_path(@agent), notice: 'Your Agent was successfully updated.'
119
+          else
120
+            redirect_to agents_path, notice: 'Your Agent was successfully updated.'
121
+          end
122
+        }
117 123
         format.json { head :no_content }
118 124
       else
119 125
         format.html { render action: "edit" }

+ 4 - 2
app/helpers/application_helper.rb

@@ -8,10 +8,12 @@ module ApplicationHelper
8 8
   end
9 9
 
10 10
   def working(agent)
11
-    if agent.working?
11
+    if agent.disabled?
12
+      '<span class="label label-warning">Disabled</span>'.html_safe
13
+    elsif agent.working?
12 14
       '<span class="label label-success">Yes</span>'.html_safe
13 15
     else
14
-      link_to '<span class="label label-warning">No</span>'.html_safe, agent_path(agent, :tab => (agent.recent_error_logs? ? 'logs' : 'details'))
16
+      link_to '<span class="label btn-danger">No</span>'.html_safe, agent_path(agent, :tab => (agent.recent_error_logs? ? 'logs' : 'details'))
15 17
     end
16 18
   end
17 19
 end

+ 7 - 3
app/helpers/dot_helper.rb

@@ -22,16 +22,20 @@ module DotHelper
22 22
     '"%s"' % string.gsub(/\\/, "\\\\\\\\").gsub(/"/, "\\\\\"")
23 23
   end
24 24
 
25
+  def disabled_label(agent)
26
+    agent.disabled? ? dot_id(agent.name + " (Disabled)") : dot_id(agent.name)
27
+  end
28
+
25 29
   def agents_dot(agents, rich = false)
26 30
     "digraph foo {".tap { |dot|
27 31
       agents.each.with_index do |agent, index|
28 32
         if rich
29
-          dot << '%s[URL=%s];' % [dot_id(agent.name), dot_id(agent_path(agent.id))]
33
+          dot << '%s[URL=%s];' % [disabled_label(agent), dot_id(agent_path(agent.id))]
30 34
         else
31
-          dot << '%s;' % dot_id(agent.name)
35
+          dot << '%s;' % disabled_label(agent)
32 36
         end
33 37
         agent.receivers.each do |receiver|
34
-          dot << "%s->%s;" % [dot_id(agent.name), dot_id(receiver.name)]
38
+          dot << "%s->%s;" % [disabled_label(agent), disabled_label(receiver)]
35 39
         end
36 40
       end
37 41
       dot << "}"

+ 9 - 7
app/models/agent.rb

@@ -22,7 +22,7 @@ class Agent < ActiveRecord::Base
22 22
 
23 23
   EVENT_RETENTION_SCHEDULES = [["Forever", 0], ["1 day", 1], *([2, 3, 4, 5, 7, 14, 21, 30, 45, 90, 180, 365].map {|n| ["#{n} days", n] })]
24 24
 
25
-  attr_accessible :options, :memory, :name, :type, :schedule, :source_ids, :keep_events_for, :propagate_immediately
25
+  attr_accessible :options, :memory, :name, :type, :schedule, :disabled, :source_ids, :keep_events_for, :propagate_immediately
26 26
 
27 27
   json_serialize :options, :memory
28 28
 
@@ -86,8 +86,8 @@ class Agent < ActiveRecord::Base
86 86
 
87 87
   def create_event(attrs)
88 88
     if can_create_events?
89
-      events.create!({ 
90
-         :user => user, 
89
+      events.create!({
90
+         :user => user,
91 91
          :expires_at => new_event_expiration_date
92 92
       }.merge(attrs))
93 93
     else
@@ -117,7 +117,7 @@ class Agent < ActiveRecord::Base
117 117
     if keep_events_for == 0
118 118
       events.update_all :expires_at => nil
119 119
     else
120
-      events.update_all "expires_at = " + rdbms_date_add("created_at", "DAY", keep_events_for.to_i) 
120
+      events.update_all "expires_at = " + rdbms_date_add("created_at", "DAY", keep_events_for.to_i)
121 121
     end
122 122
   end
123 123
 
@@ -275,11 +275,11 @@ class Agent < ActiveRecord::Base
275 275
                 joins("JOIN links ON (links.receiver_id = agents.id)").
276 276
                 joins("JOIN agents AS sources ON (links.source_id = sources.id)").
277 277
                 joins("JOIN events ON (events.agent_id = sources.id AND events.id > links.event_id_at_creation)").
278
-                where("agents.last_checked_event_id IS NULL OR events.id > agents.last_checked_event_id")
278
+                where("NOT agents.disabled AND (agents.last_checked_event_id IS NULL OR events.id > agents.last_checked_event_id)")
279 279
         if options[:only_receivers].present?
280 280
           scope = scope.where("agents.id in (?)", options[:only_receivers])
281 281
         end
282
- 
282
+
283 283
         sql = scope.to_sql()
284 284
 
285 285
         agents_to_events = {}
@@ -310,6 +310,7 @@ class Agent < ActiveRecord::Base
310 310
     def async_receive(agent_id, event_ids)
311 311
       agent = Agent.find(agent_id)
312 312
       begin
313
+        return if agent.disabled?
313 314
         agent.receive(Event.where(:id => event_ids))
314 315
         agent.last_receive_at = Time.now
315 316
         agent.save!
@@ -334,7 +335,7 @@ class Agent < ActiveRecord::Base
334 335
     # per type of agent, so you can override this to define custom bulk check behavior for your custom Agent type.
335 336
     def bulk_check(schedule)
336 337
       raise "Call #bulk_check on the appropriate subclass of Agent" if self == Agent
337
-      where(:schedule => schedule).pluck("agents.id").each do |agent_id|
338
+      where("agents.schedule = ? and disabled = false", schedule).pluck("agents.id").each do |agent_id|
338 339
         async_check(agent_id)
339 340
       end
340 341
     end
@@ -347,6 +348,7 @@ class Agent < ActiveRecord::Base
347 348
     def async_check(agent_id)
348 349
       agent = Agent.find(agent_id)
349 350
       begin
351
+        return if agent.disabled?
350 352
         agent.check
351 353
         agent.last_check_at = Time.now
352 354
         agent.save!

+ 1 - 1
app/views/agents/_form.html.erb

@@ -96,4 +96,4 @@
96 96
     </div>
97 97
   </div>
98 98
       
99
-<% end %>
99
+<% end %>

+ 2 - 2
app/views/agents/index.html.erb

@@ -19,7 +19,7 @@
19 19
           </tr>
20 20
 
21 21
           <% @agents.each do |agent| %>
22
-            <tr>
22
+            <tr class='<%= "agent-disabled" if agent.disabled? %>'>
23 23
               <td>
24 24
                 <%= agent.name %>
25 25
                 <br/>
@@ -66,7 +66,7 @@
66 66
                   <%= link_to 'Show', agent_path(agent), class: "btn btn-default" %>
67 67
                   <%= link_to 'Edit', edit_agent_path(agent), class: "btn btn-default" %>
68 68
                   <%= link_to 'Delete', agent_path(agent), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-default" %>
69
-                  <% if agent.can_be_scheduled? %>
69
+                  <% if agent.can_be_scheduled? && !agent.disabled? %>
70 70
                     <%= link_to 'Run', run_agent_path(agent, :return => "index"), method: :post, class: "btn btn-default" %>
71 71
                   <% else %>
72 72
                     <%= link_to 'Run', "#", class: "btn btn-default disabled" %>

+ 8 - 0
app/views/agents/show.html.erb

@@ -37,6 +37,14 @@
37 37
               </li>
38 38
 
39 39
               <li>
40
+                <% if @agent.disabled? %>
41
+                  <%= link_to '<i class="glyphicon glyphicon-play"></i> Enable agent'.html_safe, agent_path(@agent, :agent => { :disabled => false }, :return => "show"), :method => :put %>
42
+                <% else %>
43
+                  <%= link_to '<i class="glyphicon glyphicon-pause"></i> Disable agent'.html_safe, agent_path(@agent, :agent => { :disabled => true }, :return => "show"), :method => :put %>
44
+                <% end %>
45
+              </li>
46
+
47
+              <li>
40 48
                 <%= link_to '<span class="glyphicon glyphicon-remove"></span> Delete'.html_safe, agent_path(@agent), method: :delete, data: { confirm: 'Are you sure?' }, :tabindex => "-1" %>
41 49
               </li>
42 50
             </ul>

+ 1 - 1
app/views/events/index.html.erb

@@ -26,7 +26,7 @@
26 26
               <div class="btn-group btn-group-xs">
27 27
                 <%= link_to 'Show', event_path(event), class: "btn btn-default" %>
28 28
                 <%= link_to 'Re-emit', reemit_event_path(event), method: :post, data: { confirm: 'Are you sure you want to duplicate this event and emit the new one now?' }, class: "btn btn-default" %>
29
-                <%= link_to 'Delete', event_path(event), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-default" %>
29
+                <%= link_to 'Delete', event_path(event), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-default btn-danger" %>
30 30
               </div>
31 31
             </td>
32 32
           </tr>

+ 5 - 0
db/migrate/20140403043556_add_disabled_to_agent.rb

@@ -0,0 +1,5 @@
1
+class AddDisabledToAgent < ActiveRecord::Migration
2
+  def change
3
+    add_column :agents, :disabled, :boolean, :default => false, :null => false
4
+  end
5
+end

+ 1 - 0
db/schema.rb

@@ -41,6 +41,7 @@ ActiveRecord::Schema.define(:version => 20140408150825) do
41 41
     t.datetime "last_event_at"
42 42
     t.datetime "last_error_log_at"
43 43
     t.boolean  "propagate_immediately",                       :default => false, :null => false
44
+    t.boolean  "disabled",                                    :default => false, :null => false
44 45
   end
45 46
 
46 47
   add_index "agents", ["schedule"], :name => "index_agents_on_schedule"

+ 42 - 2
spec/models/agent_spec.rb

@@ -4,6 +4,23 @@ require 'models/concerns/working_helpers'
4 4
 describe Agent do
5 5
   it_behaves_like WorkingHelpers
6 6
 
7
+  describe ".bulk_check" do
8
+    before do
9
+      @weather_agent_count = Agents::WeatherAgent.where(:schedule => "midnight", :disabled => false).count
10
+    end
11
+
12
+    it "should run all Agents with the given schedule" do
13
+      mock(Agents::WeatherAgent).async_check(anything).times(@weather_agent_count)
14
+      Agents::WeatherAgent.bulk_check("midnight")
15
+    end
16
+
17
+    it "should skip disabled Agents" do
18
+      agents(:bob_weather_agent).update_attribute :disabled, true
19
+      mock(Agents::WeatherAgent).async_check(anything).times(@weather_agent_count - 1)
20
+      Agents::WeatherAgent.bulk_check("midnight")
21
+    end
22
+  end
23
+
7 24
   describe ".run_schedule" do
8 25
     before do
9 26
       Agents::WeatherAgent.count.should > 0
@@ -194,17 +211,31 @@ describe Agent do
194 211
         log.message.should =~ /Exception/
195 212
         log.level.should == 4
196 213
       end
214
+
215
+      it "should not run disabled Agents" do
216
+        mock(Agent).find(agents(:bob_weather_agent).id) { agents(:bob_weather_agent) }
217
+        do_not_allow(agents(:bob_weather_agent)).check
218
+        agents(:bob_weather_agent).update_attribute :disabled, true
219
+        Agent.async_check(agents(:bob_weather_agent).id)
220
+      end
197 221
     end
198 222
 
199
-    describe ".receive! and .async_receive" do
223
+    describe ".receive!" do
200 224
       before do
201 225
         stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
202 226
         stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
203 227
       end
204 228
 
205 229
       it "should use available events" do
206
-        mock.any_instance_of(Agents::TriggerAgent).receive(anything).once
207 230
         Agent.async_check(agents(:bob_weather_agent).id)
231
+        mock(Agent).async_receive(agents(:bob_rain_notifier_agent).id, anything).times(1)
232
+        Agent.receive!
233
+      end
234
+
235
+      it "should not propogate to disabled Agents" do
236
+        Agent.async_check(agents(:bob_weather_agent).id)
237
+        agents(:bob_rain_notifier_agent).update_attribute :disabled, true
238
+        mock(Agent).async_receive(agents(:bob_rain_notifier_agent).id, anything).times(0)
208 239
         Agent.receive!
209 240
       end
210 241
 
@@ -281,6 +312,15 @@ describe Agent do
281 312
       end
282 313
     end
283 314
 
315
+    describe ".async_receive" do
316
+      it "should not run disabled Agents" do
317
+        mock(Agent).find(agents(:bob_rain_notifier_agent).id) { agents(:bob_rain_notifier_agent) }
318
+        do_not_allow(agents(:bob_rain_notifier_agent)).receive
319
+        agents(:bob_rain_notifier_agent).update_attribute :disabled, true
320
+        Agent.async_receive(agents(:bob_rain_notifier_agent).id, [1, 2, 3])
321
+      end
322
+    end
323
+
284 324
     describe "creating a new agent and then calling .receive!" do
285 325
       it "should not backfill events for a newly created agent" do
286 326
         Event.delete_all