Module: Celluloid

Extended by:
Celluloid
Included in:
Celluloid, IncidentReporter, Notifications::Fanout, PoolManager, SupervisionGroup
Defined in:
lib/celluloid.rb,
lib/celluloid/fsm.rb,
lib/celluloid/uuid.rb,
lib/celluloid/actor.rb,
lib/celluloid/tasks.rb,
lib/celluloid/calls.rb,
lib/celluloid/rspec.rb,
lib/celluloid/links.rb,
lib/celluloid/method.rb,
lib/celluloid/thread.rb,
lib/celluloid/legacy.rb,
lib/celluloid/future.rb,
lib/celluloid/logger.rb,
lib/celluloid/signals.rb,
lib/celluloid/version.rb,
lib/celluloid/mailbox.rb,
lib/celluloid/registry.rb,
lib/celluloid/receivers.rb,
lib/celluloid/condition.rb,
lib/celluloid/responses.rb,
lib/celluloid/supervisor.rb,
lib/celluloid/stack_dump.rb,
lib/celluloid/cpu_counter.rb,
lib/celluloid/pool_manager.rb,
lib/celluloid/notifications.rb,
lib/celluloid/thread_handle.rb,
lib/celluloid/system_events.rb,
lib/celluloid/internal_pool.rb,
lib/celluloid/evented_mailbox.rb,
lib/celluloid/logging/incident.rb,
lib/celluloid/tasks/task_fiber.rb,
lib/celluloid/supervision_group.rb,
lib/celluloid/logging/log_event.rb,
lib/celluloid/tasks/task_thread.rb,
lib/celluloid/proxies/sync_proxy.rb,
lib/celluloid/logging/ring_buffer.rb,
lib/celluloid/proxies/actor_proxy.rb,
lib/celluloid/proxies/async_proxy.rb,
lib/celluloid/proxies/block_proxy.rb,
lib/celluloid/proxies/future_proxy.rb,
lib/celluloid/proxies/abstract_proxy.rb,
lib/celluloid/logging/incident_logger.rb,
lib/celluloid/logging/incident_reporter.rb

Defined Under Namespace

Modules: CPUCounter, ClassMethods, FSM, InstanceMethods, Logger, Notifications, UUID Classes: AbortError, AbstractProxy, Actor, ActorProxy, AsyncCall, AsyncProxy, BlockCall, BlockProxy, BlockResponse, Call, Condition, ConditionError, DeadActorError, DeadTaskError, ErrorResponse, EventedMailbox, ExitEvent, FiberStackError, Future, FutureProxy, Incident, IncidentLogger, IncidentReporter, InternalPool, LinkingRequest, LinkingResponse, Links, LogEvent, Mailbox, MailboxError, MailboxShutdown, Method, NamingRequest, NotActorError, NotTaskError, PoolManager, Receiver, Receivers, Registry, Response, RingBuffer, SignalConditionRequest, Signals, StackDump, SuccessResponse, SupervisionGroup, Supervisor, SyncCall, SyncProxy, SystemEvent, Task, TaskFiber, TaskSet, TaskThread, TerminationRequest, Thread, ThreadHandle, TimeoutError

Constant Summary

BARE_OBJECT_WARNING_MESSAGE =

Warning message added to Celluloid objects accessed outside their actors

"WARNING: BARE CELLULOID OBJECT "
LINKING_TIMEOUT =

linking times out after 5 seconds

5
OWNER_IVAR =

reference to owning actor

:@celluloid_owner
TIMER_QUANTUM =

Timer accuracy enforced by the tests (50ms)

0.05
VERSION =
'0.14.0'

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Attribute Details

+ (Object) internal_pool

Returns the value of attribute internal_pool



13
14
15
# File 'lib/celluloid.rb', line 13

def internal_pool
  @internal_pool
end

+ (Object) logger

Returns the value of attribute logger



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

def logger
  @logger
end

+ (Object) shutdown_timeout

Internal thread pool Thread-safe logger class Default task type to use How long actors have to terminate



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

def shutdown_timeout
  @shutdown_timeout
end

+ (Object) task_class

Returns the value of attribute task_class



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

def task_class
  @task_class
end

Class Method Details

+ (Boolean) actor?

Are we currently inside of an actor?

Returns:

  • (Boolean)


24
25
26
# File 'lib/celluloid.rb', line 24

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

+ (Object) boot

Launch default services FIXME: We should set up the supervision hierarchy here



68
69
70
71
72
# File 'lib/celluloid.rb', line 68

def boot
  internal_pool.reset
  Celluloid::Notifications::Fanout.supervise_as :notifications_fanout
  Celluloid::IncidentReporter.supervise_as :default_incident_reporter, STDERR
end

+ (Object) cores Also known as: cpus, ncpus

Obtain the number of CPUs in the system



39
40
41
# File 'lib/celluloid.rb', line 39

def cores
 CPUCounter.cores
end

+ (Object) exception_handler(&block)

Define an exception handler for actor crashes



52
53
54
# File 'lib/celluloid.rb', line 52

def exception_handler(&block)
  Logger.exception_handler(&block)
end

+ (Object) included(klass)



18
19
20
21
# File 'lib/celluloid.rb', line 18

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

+ (Object) mailbox

Retrieve the mailbox for the current thread or lazily initialize it



29
30
31
# File 'lib/celluloid.rb', line 29

def mailbox
  Thread.current[:celluloid_mailbox] ||= Celluloid::Mailbox.new
end

+ (Object) register_shutdown



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/celluloid.rb', line 74

def register_shutdown
  return if @shutdown_registered
  # Terminate all actors at exit
  at_exit do
    if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION >= "1.9"
      # workaround for MRI bug losing exit status in at_exit block
      # http://bugs.ruby-lang.org/issues/5218
      exit_status = $!.status if $!.is_a?(SystemExit)
      Celluloid.shutdown
      exit exit_status if exit_status
    else
      Celluloid.shutdown
    end
  end
  @shutdown_registered = true
end

+ (Object) shutdown

Shut down all running actors



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/celluloid.rb', line 92

def shutdown
  Timeout.timeout(shutdown_timeout) do
    internal_pool.shutdown

    actors = Actor.all
    Logger.debug "Terminating #{actors.size} actors..." if actors.size > 0

    # Attempt to shut down the supervision tree, if available
    Supervisor.root.terminate if Supervisor.root

    # Actors cannot self-terminate, you must do it for them
    actors.each do |actor|
      begin
        actor.terminate!
      rescue DeadActorError, MailboxError
      end
    end

    actors.each do |actor|
      begin
        Actor.join(actor)
      rescue DeadActorError, MailboxError
      end
    end

    Logger.debug "Shutdown completed cleanly"
  end
rescue Timeout::Error
  Logger.error("Couldn't cleanly terminate all actors in #{shutdown_timeout} seconds!")
end

+ (Object) stack_dump(output = STDERR) Also known as: dump

Perform a stack dump of all actors to the given output object



46
47
48
# File 'lib/celluloid.rb', line 46

def stack_dump(output = STDERR)
  Celluloid::StackDump.new.dump(output)
end

+ (Object) suspend(status, waiter)



56
57
58
59
60
61
62
63
64
# File 'lib/celluloid.rb', line 56

def suspend(status, waiter)
  task = Thread.current[:celluloid_task]
  if task && !Celluloid.exclusive?
    waiter.before_suspend(task) if waiter.respond_to?(:before_suspend)
    Task.suspend(status)
  else
    waiter.wait
  end
end

+ (Object) uuid

Generate a Universally Unique Identifier



34
35
36
# File 'lib/celluloid.rb', line 34

def uuid
  UUID.generate
end

+ (Object) version



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

def self.version; VERSION; end

Instance Method Details

- (Object) abort(cause)

Raise an exception in sender context, but stay running

Raises:



355
356
357
358
359
360
361
362
# File 'lib/celluloid.rb', line 355

def abort(cause)
  cause = case cause
    when String then RuntimeError.new(cause)
    when Exception then cause
    else raise TypeError, "Exception object/String expected, but #{cause.class} received"
  end
  raise AbortError.new(cause)
end

- (Object) after(interval, &block)

Call a block after a given interval, returning a Celluloid::Timer object



462
463
464
# File 'lib/celluloid.rb', line 462

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

- (Object) async(meth = nil, *args, &block)

Handle async calls within an actor itself



481
482
483
# File 'lib/celluloid.rb', line 481

def async(meth = nil, *args, &block)
  Thread.current[:celluloid_actor].proxy.async meth, *args, &block
end

- (Object) call_chain_id

Obtain the UUID of the current call chain



385
386
387
# File 'lib/celluloid.rb', line 385

def call_chain_id
  Thread.current[:celluloid_chain_id]
end

- (Object) current_actor

Obtain the current_actor



380
381
382
# File 'lib/celluloid.rb', line 380

def current_actor
  Actor.current
end

- (Object) defer(&block)

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



474
475
476
477
478
# File 'lib/celluloid.rb', line 474

def defer(&block)
  # This implementation relies on the present implementation of
  # Celluloid::Future, which uses a thread from InternalPool to run the block
  Future.new(&block).value
end

- (Object) every(interval, &block)

Call a block every given interval, returning a Celluloid::Timer object



467
468
469
# File 'lib/celluloid.rb', line 467

def every(interval, &block)
  Thread.current[:celluloid_actor].every(interval, &block)
end

- (Object) exclusive(&block)

Run given block in an exclusive mode: all synchronous calls block the whole actor, not only current message processing.



451
452
453
# File 'lib/celluloid.rb', line 451

def exclusive(&block)
  Thread.current[:celluloid_actor].exclusive(&block)
end

- (Boolean) exclusive?

Are we currently exclusive

Returns:

  • (Boolean)


456
457
458
459
# File 'lib/celluloid.rb', line 456

def exclusive?
  actor = Thread.current[:celluloid_actor]
  actor && actor.exclusive?
end

- (Object) future(meth = nil, *args, &block)

Handle calls to future within an actor itself



486
487
488
# File 'lib/celluloid.rb', line 486

def future(meth = nil, *args, &block)
  Thread.current[:celluloid_actor].proxy.future meth, *args, &block
end

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



410
411
412
# File 'lib/celluloid.rb', line 410

def link(actor)
  Actor.link(actor)
end

- (Boolean) linked_to?(actor)

Is this actor linked to another?

Returns:

  • (Boolean)


425
426
427
# File 'lib/celluloid.rb', line 425

def linked_to?(actor)
  Actor.linked_to?(actor)
end

Obtain the Celluloid::Links for this actor



395
396
397
# File 'lib/celluloid.rb', line 395

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

- (Object) monitor(actor)

Watch for exit events from another actor



400
401
402
# File 'lib/celluloid.rb', line 400

def monitor(actor)
  Actor.monitor(actor)
end

- (Boolean) monitoring?(actor)

Are we monitoring another actor?

Returns:

  • (Boolean)


420
421
422
# File 'lib/celluloid.rb', line 420

def monitoring?(actor)
  Actor.monitoring?(actor)
end

- (Object) receive(timeout = nil, &block)

Receive an asynchronous message via the actor protocol



430
431
432
433
434
435
436
437
# File 'lib/celluloid.rb', line 430

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

- (Object) signal(name, value = nil)

Send a signal with the given name to all waiting methods



370
371
372
# File 'lib/celluloid.rb', line 370

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

- (Object) sleep(interval)

Sleep letting the actor continue processing messages



440
441
442
443
444
445
446
447
# File 'lib/celluloid.rb', line 440

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

- (Object) tasks

Obtain the running tasks for this actor



390
391
392
# File 'lib/celluloid.rb', line 390

def tasks
  Thread.current[:celluloid_actor].tasks.to_a
end

- (Object) terminate

Terminate this actor



365
366
367
# File 'lib/celluloid.rb', line 365

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

Remove links to another actor



415
416
417
# File 'lib/celluloid.rb', line 415

def unlink(actor)
  Actor.unlink(actor)
end

- (Object) unmonitor(actor)

Stop waiting for exit events from another actor



405
406
407
# File 'lib/celluloid.rb', line 405

def unmonitor(actor)
  Actor.unmonitor(actor)
end

- (Object) wait(name)

Wait for the given signal



375
376
377
# File 'lib/celluloid.rb', line 375

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