Module: Celluloid

Included in:
Application, Future::Runner, Supervisor
Defined in:
lib/vendor/celluloid/lib/celluloid.rb,
lib/vendor/celluloid/lib/celluloid/fsm.rb,
lib/vendor/celluloid/lib/celluloid/task.rb,
lib/vendor/celluloid/lib/celluloid/actor.rb,
lib/vendor/celluloid/lib/celluloid/calls.rb,
lib/vendor/celluloid/lib/celluloid/links.rb,
lib/vendor/celluloid/lib/celluloid/events.rb,
lib/vendor/celluloid/lib/celluloid/future.rb,
lib/vendor/celluloid/lib/celluloid/logger.rb,
lib/vendor/celluloid/lib/celluloid/timers.rb,
lib/vendor/celluloid/lib/celluloid/mailbox.rb,
lib/vendor/celluloid/lib/celluloid/signals.rb,
lib/vendor/celluloid/lib/celluloid/version.rb,
lib/vendor/celluloid/lib/celluloid/registry.rb,
lib/vendor/celluloid/lib/celluloid/receivers.rb,
lib/vendor/celluloid/lib/celluloid/responses.rb,
lib/vendor/celluloid/lib/celluloid/actor_pool.rb,
lib/vendor/celluloid/lib/celluloid/supervisor.rb,
lib/vendor/celluloid/lib/celluloid/tcp_server.rb,
lib/vendor/celluloid/lib/celluloid/actor_proxy.rb,
lib/vendor/celluloid/lib/celluloid/application.rb

Defined Under Namespace

Modules: ClassMethods, FSM, Logger, Registry Classes: AbortError, Actor, ActorProxy, Application, AsyncCall, Call, DeadActorError, DeadTaskError, ErrorResponse, ExitEvent, Future, Links, Mailbox, MailboxError, MailboxShutdown, NotActorError, Receiver, Receivers, Response, Signals, SuccessResponse, Supervisor, SyncCall, SystemEvent, TCPServer, Task, Timer, Timers

Constant Summary collapse

VERSION =
'0.7.1'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Process async calls via method_missing



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 218

def method_missing(meth, *args, &block)
  # bang methods are async calls
  if meth.to_s.match(/!$/)
    unbanged_meth = meth.to_s.sub(/!$/, '')
    call = AsyncCall.new(@mailbox, unbanged_meth, args, block)

    begin
      Thread.current[:actor].mailbox << call
    rescue MailboxError
      # Silently swallow asynchronous calls to dead actors. There's no way
      # to reliably generate DeadActorErrors for async calls, so users of
      # async calls should find other ways to deal with actors dying
      # during an async call (i.e. linking/supervisors)
    end

    return # casts are async and return immediately
  end

  super
end

Class Attribute Details

.loggerObject

Thread-safe logger class



8
9
10
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 8

def logger
  @logger
end

Class Method Details

.actor?Boolean

Are we currently inside of an actor?

Returns:

  • (Boolean)


15
16
17
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 15

def actor?
  !!Thread.current[:actor]
end

.currentObject

Obtain the currently running actor (if one exists)

Raises:



25
26
27
28
29
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 25

def current_actor
  actor = Thread.current[:actor]
  raise NotActorError, "not in actor scope" unless actor
  actor.proxy
end

.current_actorObject

Obtain the currently running actor (if one exists)

Raises:



20
21
22
23
24
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 20

def current_actor
  actor = Thread.current[:actor]
  raise NotActorError, "not in actor scope" unless actor
  actor.proxy
end

.included(klass) ⇒ Object



10
11
12
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 10

def included(klass)
  klass.send :extend, ClassMethods
end

.receive(timeout = nil, &block) ⇒ Object

Receive an asynchronous message



28
29
30
31
32
33
34
35
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 28

def receive(timeout = nil, &block)
  actor = Thread.current[:actor]
  if actor
    actor.receive(timeout, &block)
  else
    Thread.mailbox.receive(timeout, &block)
  end
end

.sleep(interval) ⇒ Object

Sleep letting the actor continue processing messages



38
39
40
41
42
43
44
45
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 38

def sleep(interval)
  actor = Thread.current[:actor]
  if actor
    actor.sleep(interval)
  else
    Kernel.sleep interval
  end
end

.tasksObject

Obtain a hash of active tasks to their current activities

Raises:



48
49
50
51
52
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 48

def tasks
  actor = Thread.current[:actor]
  raise NotActorError, "not in actor scope" unless actor
  actor.tasks
end

.versionObject



3
# File 'lib/vendor/celluloid/lib/celluloid/version.rb', line 3

def self.version; VERSION; end

Instance Method Details

#abort(cause) ⇒ Object

Raise an exception in caller context, but stay running

Raises:



117
118
119
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 117

def abort(cause)
  raise AbortError.new(cause)
end

#after(interval, &block) ⇒ Object

Call a block after a given interval



204
205
206
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 204

def after(interval, &block)
  Thread.current[:actor].after(interval, &block)
end

#alive?Boolean

Is this actor alive?

Returns:

  • (Boolean)


112
113
114
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 112

def alive?
  Thread.current[:actor].alive?
end

#async(&block) ⇒ Object

Perform a blocking or computationally intensive action inside an asynchronous thread pool, allowing the caller to continue processing other messages in its mailbox in the meantime



211
212
213
214
215
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 211

def async(&block)
  # This implementation relies on the present implementation of
  # Celluloid::Future, which uses an Actor to run the block
  Future.new(&block).value
end

#current_actorObject

Obtain the current_actor



147
148
149
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 147

def current_actor
  Celluloid.current_actor
end

#inspectObject



126
127
128
129
130
131
132
133
134
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 126

def inspect
  str = "#<Celluloid::Actor(#{self.class}:0x#{object_id.to_s(16)})"
  ivars = instance_variables.map do |ivar|
    "#{ivar}=#{instance_variable_get(ivar).inspect}"
  end

  str << " " << ivars.join(' ') unless ivars.empty?
  str << ">"
end

Link this actor to another, allowing it to crash or react to errors



169
170
171
172
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 169

def link(actor)
  actor.notify_link current_actor
  notify_link actor
end

#linked_to?(actor) ⇒ Boolean

Is this actor linked to another?

Returns:

  • (Boolean)


189
190
191
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 189

def linked_to?(actor)
  Thread.current[:actor].links.include? actor
end

Obtain the Celluloid::Links for this actor



164
165
166
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 164

def links
  Thread.current[:actor].links
end


180
181
182
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 180

def notify_link(actor)
  links << actor
end


184
185
186
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 184

def notify_unlink(actor)
  links.delete actor
end

#receive(timeout = nil, &block) ⇒ Object

Receive an asynchronous message via the actor protocol



194
195
196
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 194

def receive(timeout = nil, &block)
  Celluloid.receive(timeout, &block)
end

#signal(name, value = nil) ⇒ Object

Send a signal with the given name to all waiting methods



137
138
139
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 137

def signal(name, value = nil)
  Thread.current[:actor].signal name, value
end

#sleep(interval) ⇒ Object

Sleep while letting the actor continue to receive messages



199
200
201
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 199

def sleep(interval)
  Celluloid.sleep(interval)
end

#tasksObject

Obtain the running tasks for this actor



152
153
154
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 152

def tasks
  Celluloid.tasks
end

#terminateObject

Terminate this actor



122
123
124
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 122

def terminate
  Thread.current[:actor].terminate
end

Remove links to another actor



175
176
177
178
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 175

def unlink(actor)
  actor.notify_unlink current_actor
  notify_unlink actor
end

#wait(name) ⇒ Object

Wait for the given signal



142
143
144
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 142

def wait(name)
  Thread.current[:actor].wait name
end

#wrapped_objectObject

Obtain the Ruby object the actor is wrapping. This should ONLY be used for a limited set of use cases like runtime metaprogramming. Interacting directly with the wrapped object foregoes any kind of thread safety that Celluloid would ordinarily provide you, and the object is guaranteed to be shared with at least the actor thread. Tread carefully.



161
# File 'lib/vendor/celluloid/lib/celluloid.rb', line 161

def wrapped_object; self; end