Class: Theatre::Theatre

Inherits:
Object show all
Defined in:
lib/theatre.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • thread_count (Fixnum) (defaults to: 6)

    Number of Threads to spawn when started.



22
23
24
25
26
27
28
29
# 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
  @loader_mixins     = []
end

Instance Attribute Details

#namespace_managerObject (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.



126
127
128
129
# File 'lib/theatre.rb', line 126

def graceful_stop!
  @thread_count.times { @master_queue << :THEATRE_SHUTDOWN! }
  @started_time = nil
end

#joinObject



98
99
100
101
102
103
104
105
106
# File 'lib/theatre.rb', line 98

def join
  @thread_group.list.each do |thread|
    begin
      thread.join
    rescue
      # Ignore any exceptions
    end
  end
end

#load_events_code(code, *args) ⇒ Object



76
77
78
79
# File 'lib/theatre.rb', line 76

def load_events_code(code, *args)
  loader = CallbackDefinitionLoader.new(self, *args)
  loader.load_events_code code
end

#load_events_file(file, *args) ⇒ Object



81
82
83
84
# File 'lib/theatre.rb', line 81

def load_events_file(file, *args)
  loader = CallbackDefinitionLoader.new(self, *args)
  loader.load_events_file file
end

#register_callback_at_namespace(*args) ⇒ Object



90
91
92
# File 'lib/theatre.rb', line 90

def register_callback_at_namespace(*args)
  @namespace_manager.register_callback_at_namespace(*args)
end

#register_loader_mixin(mod) ⇒ Object



94
95
96
# File 'lib/theatre.rb', line 94

def register_loader_mixin(mod)
  @loader_mixins << mod
end

#register_namespace_name(*args) ⇒ Object



86
87
88
# File 'lib/theatre.rb', line 86

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.



113
114
115
116
117
118
119
# File 'lib/theatre.rb', line 113

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.

Parameters:

  • namespace (String)

    The namespace to which the payload should be sent

  • payload (Object) (defaults to: :argument_undefined)

    The actual content to be sent to the callback. Optional.

Returns:

Raises:

  • Theatre::NamespaceNotFound Raised when told to enqueue an unrecognized namespace



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/theatre.rb', line 39

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.

Parameters:

  • namespace (String)

    The namespace to which the payload should be sent

  • payload (Object) (defaults to: :argument_undefined)

    The actual content to be sent to the callback. Optional.

Returns:

  • (Array)

    An Array containing each callback’s return value (or Exception raised, if any) when given the payload

Raises:

  • Theatre::NamespaceNotFound Raised when told to enqueue an unrecognized namespace



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/theatre.rb', line 62

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