commander_agent.rb 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module Agents
  2. class CommanderAgent < Agent
  3. include AgentControllerConcern
  4. cannot_create_events!
  5. description <<-MD
  6. This agent is triggered by schedule or an incoming event and commands other agents ("targets") to run, disable or enable themselves.
  7. # Action types
  8. Set `action` to one of the action types below:
  9. * `run`: Target Agents are run when this agent is triggered.
  10. * `disable`: Target Agents are disabled (if not) when this agent is triggered.
  11. * `enable`: Target Agents are enabled (if not) when this agent is triggered.
  12. Here's a tip: you can use Liquid templating to dynamically determine the action type. For example:
  13. - 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 %}`
  14. - Likewise, if you have a scheduled agent flow specially crafted for rainy days, try this: `{% if conditions contains 'Rain' %}enable{% else %}disabled{% endif %}`
  15. # Targets
  16. Select Agents that you want to control from this CommanderAgent.
  17. MD
  18. def working?
  19. true
  20. end
  21. def check!
  22. control!
  23. end
  24. def receive(incoming_events)
  25. incoming_events.each do |event|
  26. interpolate_with(event) do
  27. control!
  28. end
  29. end
  30. end
  31. end
  32. end