@@ -0,0 +1,47 @@ |
||
1 |
+module Agents |
|
2 |
+ class CommanderAgent < Agent |
|
3 |
+ include AgentControllerConcern |
|
4 |
+ |
|
5 |
+ cannot_create_events! |
|
6 |
+ |
|
7 |
+ description <<-MD |
|
8 |
+ This agent is triggered by schedule or an incoming event and commands other agents ("targets") to run, disable or enable themselves. |
|
9 |
+ |
|
10 |
+ # Action types |
|
11 |
+ |
|
12 |
+ Set `action` to one of the action types below: |
|
13 |
+ |
|
14 |
+ * `run`: Target Agents are run when this agent is triggered. |
|
15 |
+ |
|
16 |
+ * `disable`: Target Agents are disabled (if not) when this agent is triggered. |
|
17 |
+ |
|
18 |
+ * `enable`: Target Agents are enabled (if not) when this agent is triggered. |
|
19 |
+ |
|
20 |
+ Here's a tip: you can use Liquid templating to dynamically determine the action type. For example: |
|
21 |
+ |
|
22 |
+ - To create a CommanderAgent that receives an event from WeatherAgent every morning to kick an agent flow that is only useful in a nice weather, try this: `{% if conditions contains 'Sunny' or conditions contains 'Cloudy' %}run{% endif %}` |
|
23 |
+ |
|
24 |
+ - Likewise, if you have a scheduled agent flow specially crafted for rainy days, try this: `{% if conditions contains 'Rain' %}enable{% else %}disabled{% endif %}` |
|
25 |
+ |
|
26 |
+ # Targets |
|
27 |
+ |
|
28 |
+ Select Agents that you want to control from this CommanderAgent. |
|
29 |
+ MD |
|
30 |
+ |
|
31 |
+ def working? |
|
32 |
+ true |
|
33 |
+ end |
|
34 |
+ |
|
35 |
+ def check! |
|
36 |
+ control! |
|
37 |
+ end |
|
38 |
+ |
|
39 |
+ def receive(incoming_events) |
|
40 |
+ incoming_events.each do |event| |
|
41 |
+ interpolate_with(event) do |
|
42 |
+ control! |
|
43 |
+ end |
|
44 |
+ end |
|
45 |
+ end |
|
46 |
+ end |
|
47 |
+end |
@@ -0,0 +1,42 @@ |
||
1 |
+require 'spec_helper' |
|
2 |
+ |
|
3 |
+describe Agents::CommanderAgent do |
|
4 |
+ let(:valid_params) { |
|
5 |
+ { |
|
6 |
+ name: 'Example', |
|
7 |
+ schedule: 'every_1h', |
|
8 |
+ options: { |
|
9 |
+ 'action' => 'run', |
|
10 |
+ }, |
|
11 |
+ } |
|
12 |
+ } |
|
13 |
+ |
|
14 |
+ let(:agent) { |
|
15 |
+ described_class.create!(valid_params) { |agent| |
|
16 |
+ agent.user = users(:bob) |
|
17 |
+ } |
|
18 |
+ } |
|
19 |
+ |
|
20 |
+ it_behaves_like AgentControllerConcern |
|
21 |
+ |
|
22 |
+ describe "check!" do |
|
23 |
+ it "should command targets" do |
|
24 |
+ stub(agent).control!.once { nil } |
|
25 |
+ agent.check! |
|
26 |
+ end |
|
27 |
+ end |
|
28 |
+ |
|
29 |
+ describe "receive_events" do |
|
30 |
+ it "should command targets" do |
|
31 |
+ stub(agent).control!.once { nil } |
|
32 |
+ |
|
33 |
+ event = Event.new |
|
34 |
+ event.agent = agents(:bob_rain_notifier_agent) |
|
35 |
+ event.payload = { |
|
36 |
+ 'url' => 'http://xkcd.com', |
|
37 |
+ 'link' => 'Random', |
|
38 |
+ } |
|
39 |
+ agent.receive([event]) |
|
40 |
+ end |
|
41 |
+ end |
|
42 |
+end |