Class: Theatre::Theatre
- Inherits:
-
Object
- Object
- Theatre::Theatre
- Defined in:
- lib/theatre.rb
Instance Attribute Summary collapse
-
#namespace_manager ⇒ Object
readonly
Returns the value of attribute namespace_manager.
Instance Method Summary collapse
-
#graceful_stop! ⇒ Object
Notifies all Threads for this Theatre to stop by sending them special messages.
-
#initialize(thread_count = 6) ⇒ Theatre
constructor
Creates a new stopped Theatre.
- #join ⇒ Object
- #load_events_code(code, *args) ⇒ Object
- #load_events_file(file, *args) ⇒ Object
- #register_callback_at_namespace(*args) ⇒ Object
- #register_namespace_name(*args) ⇒ Object
-
#start! ⇒ Object
Starts this Theatre.
-
#trigger(namespace, payload = :argument_undefined) ⇒ Array<Theatre::Invocation>
Send a message to this Theatre for asynchronous processing.
-
#trigger_immediately(namespace, payload = :argument_undefined) ⇒ Array
Send a message to this Theatre for synchronous processing.
Constructor Details
#initialize(thread_count = 6) ⇒ Theatre
Creates a new stopped Theatre. You must call start!() after you instantiate this for it to begin processing events.
22 23 24 25 26 27 28 |
# File 'lib/theatre.rb', line 22 def initialize(thread_count=6) @thread_count = thread_count @started = false @namespace_manager = ActorNamespaceManager.new @thread_group = ThreadGroup.new @master_queue = Queue.new end |
Instance Attribute Details
#namespace_manager ⇒ Object (readonly)
Returns the value of attribute namespace_manager.
15 16 17 |
# File 'lib/theatre.rb', line 15 def namespace_manager @namespace_manager end |
Instance Method Details
#graceful_stop! ⇒ Object
Notifies all Threads for this Theatre to stop by sending them special messages. Any messages which were queued and untriggered when this method is received will still be processed. Note: you may start this Theatre again later once it has been stopped.
119 120 121 122 |
# File 'lib/theatre.rb', line 119 def graceful_stop! @thread_count.times { @master_queue << :THEATRE_SHUTDOWN! } @started_time = nil end |
#join ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/theatre.rb', line 91 def join @thread_group.list.each do |thread| begin thread.join rescue # Ignore any exceptions end end end |
#load_events_code(code, *args) ⇒ Object
75 76 77 |
# File 'lib/theatre.rb', line 75 def load_events_code(code, *args) CallbackDefinitionLoader.new(self, *args).load_events_code(code) end |
#load_events_file(file, *args) ⇒ Object
79 80 81 |
# File 'lib/theatre.rb', line 79 def load_events_file(file, *args) CallbackDefinitionLoader.new(self, *args).load_events_file(file) end |
#register_callback_at_namespace(*args) ⇒ Object
87 88 89 |
# File 'lib/theatre.rb', line 87 def register_callback_at_namespace(*args) @namespace_manager.register_callback_at_namespace(*args) end |
#register_namespace_name(*args) ⇒ Object
83 84 85 |
# File 'lib/theatre.rb', line 83 def register_namespace_name(*args) @namespace_manager.register_namespace_name(*args) end |
#start! ⇒ Object
Starts this Theatre.
When this method is called, the Threads are spawned and begin pulling messages off this Theatre’s master queue.
106 107 108 109 110 111 112 |
# File 'lib/theatre.rb', line 106 def start! return false if @thread_group.list.any? # Already started @started_time = Time.now @thread_count.times do @thread_group.add Thread.new(&method(:thread_loop)) end end |
#trigger(namespace, payload = :argument_undefined) ⇒ Array<Theatre::Invocation>
Send a message to this Theatre for asynchronous processing.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/theatre.rb', line 38 def trigger(namespace, payload=:argument_undefined) @namespace_manager.callbacks_for_namespaces(namespace).map do |callback| invocation = if payload.equal?(:argument_undefined) Invocation.new(namespace, callback) else Invocation.new(namespace, callback, payload) end invocation.queued @master_queue << invocation invocation end end |
#trigger_immediately(namespace, payload = :argument_undefined) ⇒ Array
Send a message to this Theatre for synchronous processing. The execution of this will not go through this Theatre’s Thread pool. If an error occurred in any of callbacks, the Exception object will be placed in the returned Array instead for you to act upon.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/theatre.rb', line 61 def trigger_immediately(namespace, payload=:argument_undefined) @namespace_manager.callbacks_for_namespaces(namespace).map do |callback| begin invocation = if payload.equal?(:argument_undefined) callback.call else callback.call payload end rescue => captured_error_to_be_returned captured_error_to_be_returned end end end |