Class: Brown::Agent::Stimulus
- Inherits:
-
Object
- Object
- Brown::Agent::Stimulus
- Defined in:
- lib/brown/agent/stimulus.rb
Overview
A single stimulus group in a Brown Agent.
This is all the behind-the-scenes plumbing that's required to make the trickiness of stimuli work correctly. In general, if you're a Brown user and you ever need to do anything with anything in here, something has gone terribly, terribly wrong somewhere.
Instance Attribute Summary collapse
-
#agent_class ⇒ Class
readonly
The class to instantiate to process a stimulus.
-
#method_name ⇒ String
readonly
The name of the method to call when the stimulus is triggered.
-
#stimuli_proc ⇒ Proc
readonly
The chunk of code to call over and over again to listen for the stimulus.
Instance Method Summary collapse
-
#finish ⇒ Object
Stop the stimulus listener, and wait gracefull for all currently in-progress stimuli processing to finish before returning.
-
#initialize(method_name:, stimuli_proc:, agent_class:, logger: Logger.new("/dev/null")) ⇒ Stimulus
constructor
Create a new stimulus.
-
#run(once = nil) ⇒ Object
Fire off the stimulus listener.
-
#stop ⇒ Object
Signal the stimulus to immediately shut down everything.
Constructor Details
#initialize(method_name:, stimuli_proc:, agent_class:, logger: Logger.new("/dev/null")) ⇒ Stimulus
Create a new stimulus.
41 42 43 44 45 46 47 |
# File 'lib/brown/agent/stimulus.rb', line 41 def initialize(method_name:, stimuli_proc:, agent_class:, logger: Logger.new("/dev/null")) @method_name = method_name @stimuli_proc = stimuli_proc @agent_class = agent_class @thread_group = ThreadGroup.new @logger = logger end |
Instance Attribute Details
#agent_class ⇒ Class (readonly)
The class to instantiate to process a stimulus.
25 26 27 |
# File 'lib/brown/agent/stimulus.rb', line 25 def agent_class @agent_class end |
#method_name ⇒ String (readonly)
The name of the method to call when the stimulus is triggered.
13 14 15 |
# File 'lib/brown/agent/stimulus.rb', line 13 def method_name @method_name end |
#stimuli_proc ⇒ Proc (readonly)
The chunk of code to call over and over again to listen for the stimulus.
19 20 21 |
# File 'lib/brown/agent/stimulus.rb', line 19 def stimuli_proc @stimuli_proc end |
Instance Method Details
#finish ⇒ Object
Stop the stimulus listener, and wait gracefull for all currently in-progress stimuli processing to finish before returning.
97 98 99 100 101 102 103 104 |
# File 'lib/brown/agent/stimulus.rb', line 97 def finish if @runner_thread and @runner_thread != Thread.current @runner_thread.raise(Brown::StopSignal.new("stimulus loop")) end @runner_thread = nil @thread_group.list.each { |th| th.join } end |
#run(once = nil) ⇒ Object
Fire off the stimulus listener.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/brown/agent/stimulus.rb', line 56 def run(once = nil) if once == :once stimuli_proc.call(->(*args) { process(*args) }) else @runner_thread = Thread.current begin while @runner_thread begin stimuli_proc.call(method(:spawn_worker)) rescue Brown::StopSignal, Brown::FinishSignal raise rescue Exception => ex log_failure("Stimuli listener", ex) end end rescue Brown::StopSignal stop rescue Brown::FinishSignal finish rescue Exception => ex log_failure("Stimuli runner", ex) end end end |
#stop ⇒ Object
Signal the stimulus to immediately shut down everything.
This will cause all stimulus processing threads to be terminated immediately. You probably want to use #finish instead, normally.
86 87 88 89 90 91 92 |
# File 'lib/brown/agent/stimulus.rb', line 86 def stop @thread_group.list.each do |th| th.raise Brown::StopSignal.new("stimulus thread_group") end finish end |