module Celluloid

rubocop:enable Style/GlobalVars

collect together all instances of the `supervise` method

Constants

BARE_OBJECT_WARNING_MESSAGE

Warning message added to Celluloid objects accessed outside their actors

LINKING_TIMEOUT

Linking times out after 5 seconds

OWNER_IVAR
VERSION

Attributes

actor_system[W]
group_class[RW]
log_actor_crashes[RW]
logger[RW]
shutdown_timeout[RW]
task_class[RW]

Public Class Methods

actor?() click to toggle source

Are we currently inside of an actor?

# File lib/celluloid.rb, line 91
def actor?
  !!Thread.current[:celluloid_actor]
end
actor_system() click to toggle source
# File lib/celluloid.rb, line 34
def actor_system
  if Thread.current.celluloid?
    Thread.current[:celluloid_actor_system] || raise(Error, "actor system not running")
  else
    Thread.current[:celluloid_actor_system] ||
      @actor_system ||
      raise(Error, "Celluloid is not yet started; use Celluloid.boot")
  end
end
boot() click to toggle source
# File lib/celluloid.rb, line 155
def boot
  init
  start
end
cores() click to toggle source

Obtain the number of CPUs in the system

# File lib/celluloid.rb, line 106
def cores
  Internals::CPUCounter.cores
end
Also aliased as: cpus, ncpus
cpus()
Alias for: cores
detect_recursion() click to toggle source

Detect if a particular call is recursing through multiple actors

# File lib/celluloid.rb, line 129
def detect_recursion
  actor = Thread.current[:celluloid_actor]
  return unless actor

  task = Thread.current[:celluloid_task]
  return unless task

  chain_id = Internals::CallChain.current_id
  actor.tasks.to_a.any? { |t| t != task && t.chain_id == chain_id }
end
dump(output = STDERR)
Alias for: stack_dump
exception_handler(&block) click to toggle source

Define an exception handler for actor crashes

# File lib/celluloid.rb, line 141
def exception_handler(&block)
  Internals::Logger.exception_handler(&block)
end
included(klass) click to toggle source
# File lib/celluloid.rb, line 44
def included(klass)
  klass.send :extend,  ClassMethods
  klass.send :include, InstanceMethods

  klass.send :extend, Internals::Properties

  klass.property :mailbox_class, default: Celluloid::Mailbox
  klass.property :proxy_class,   default: Celluloid::Proxy::Cell
  klass.property :task_class,    default: Celluloid.task_class
  klass.property :group_class,   default: Celluloid.group_class
  klass.property :mailbox_size

  klass.property :exclusive_actor, default: false
  klass.property :exclusive_methods, multi: true
  klass.property :execute_block_on_receiver,
                 default: %i[after every receive],
                 multi: true

  klass.property :finalizer
  klass.property :exit_handler_name

  singleton = class << klass; self; end
  begin
    singleton.send(:remove_method, :trap_exit)
  rescue
    nil
  end
  begin
    singleton.send(:remove_method, :exclusive)
  rescue
    nil
  end

  singleton.send(:define_method, :trap_exit) do |*args|
    exit_handler_name(*args)
  end

  singleton.send(:define_method, :exclusive) do |*args|
    if args.any?
      exclusive_methods(*exclusive_methods, *args)
    else
      exclusive_actor true
    end
  end
end
init() click to toggle source
# File lib/celluloid.rb, line 160
def init
  @actor_system ||= Actor::System.new
end
mailbox() click to toggle source

Retrieve the mailbox for the current thread or lazily initialize it

# File lib/celluloid.rb, line 96
def mailbox
  Thread.current[:celluloid_mailbox] ||= Celluloid::Mailbox.new
end
ncpus()
Alias for: cores
public_registry() click to toggle source
# File lib/celluloid.rb, line 124
def public_registry
  actor_system.public_registry
end
publish(*args) click to toggle source
# File lib/celluloid/notifications.rb, line 92
def self.publish(*args)
  Notifications.publish(*args)
end
register_shutdown() click to toggle source

de TODO Anticipate outside process finalizer that would by-pass this.

# File lib/celluloid.rb, line 175
def register_shutdown
  return if defined?(@shutdown_registered) && @shutdown_registered
  # Terminate all actors at exit, unless the exit is abnormal.
  at_exit do
    Celluloid.shutdown unless $ERROR_INFO
  end
  @shutdown_registered = true
end
running?() click to toggle source
# File lib/celluloid.rb, line 168
def running?
  actor_system && actor_system.running?
rescue Error
  false
end
shutdown() click to toggle source

Shut down all running actors

# File lib/celluloid.rb, line 185
def shutdown
  actor_system.shutdown
  @actor_system = nil
end
stack_dump(output = STDERR) click to toggle source

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

# File lib/celluloid.rb, line 113
def stack_dump(output = STDERR)
  actor_system.stack_dump.print(output)
end
Also aliased as: dump
stack_summary(output = STDERR) click to toggle source

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

# File lib/celluloid.rb, line 119
def stack_summary(output = STDERR)
  actor_system.stack_summary.print(output)
end
Also aliased as: summarize
start() click to toggle source
# File lib/celluloid.rb, line 164
def start
  actor_system.start
end
summarize(output = STDERR)
Alias for: stack_summary
supervise(config = {}, &block) click to toggle source
# File lib/celluloid/supervision/supervise.rb, line 4
def supervise(config = {}, &block)
  supervisor = Supervision.router(config)
  supervisor.supervise(config, &block)
end
suspend(status, waiter) click to toggle source
# File lib/celluloid.rb, line 145
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
uuid() click to toggle source

Generate a Universally Unique Identifier

# File lib/celluloid.rb, line 101
def uuid
  Internals::UUID.generate
end
version() click to toggle source
# File lib/celluloid.rb, line 190
def version
  VERSION
end

Public Instance Methods

abort(cause) click to toggle source

Raise an exception in sender context, but stay running

# File lib/celluloid.rb, line 320
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, cause
end
after(interval, &block) click to toggle source

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

# File lib/celluloid.rb, line 435
def after(interval, &block)
  Thread.current[:celluloid_actor].after(interval, &block)
end
async(meth = nil, *args, &block) click to toggle source

Handle async calls within an actor itself

# File lib/celluloid.rb, line 454
def async(meth = nil, *args, &block)
  Thread.current[:celluloid_actor].behavior_proxy.async meth, *args, &block
end
call_chain_id() click to toggle source

Obtain the UUID of the current call chain

# File lib/celluloid.rb, line 351
def call_chain_id
  Internals::CallChain.current_id
end
current_actor() click to toggle source

Obtain the current_actor

# File lib/celluloid.rb, line 346
def current_actor
  Actor.current
end
defer(&block) click to toggle source

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

# File lib/celluloid.rb, line 447
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
every(interval, &block) click to toggle source

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

# File lib/celluloid.rb, line 440
def every(interval, &block)
  Thread.current[:celluloid_actor].every(interval, &block)
end
exclusive(&block) click to toggle source

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

# File lib/celluloid.rb, line 424
def exclusive(&block)
  Thread.current[:celluloid_task].exclusive(&block)
end
exclusive?() click to toggle source

Are we currently exclusive

# File lib/celluloid.rb, line 429
def exclusive?
  task = Thread.current[:celluloid_task]
  task && task.exclusive?
end
future(meth = nil, *args, &block) click to toggle source

Handle calls to future within an actor itself

# File lib/celluloid.rb, line 459
def future(meth = nil, *args, &block)
  Thread.current[:celluloid_actor].behavior_proxy.future meth, *args, &block
end
linked_to?(actor) click to toggle source

Is this actor linked to another?

# File lib/celluloid.rb, line 391
def linked_to?(actor)
  Actor.linked_to?(actor)
end
monitor(actor) click to toggle source

Watch for exit events from another actor

# File lib/celluloid.rb, line 366
def monitor(actor)
  Actor.monitor(actor)
end
monitoring?(actor) click to toggle source

Are we monitoring another actor?

# File lib/celluloid.rb, line 386
def monitoring?(actor)
  Actor.monitoring?(actor)
end
receive(timeout = nil, &block) click to toggle source

Receive an asynchronous message via the actor protocol

# File lib/celluloid.rb, line 396
def receive(timeout = nil, &block)
  actor = Thread.current[:celluloid_actor]
  if actor
    actor.receive(timeout, &block)
  else
    Celluloid.mailbox.receive(timeout, &block)
  end
end
signal(name, value = nil) click to toggle source

Send a signal with the given name to all waiting methods

# File lib/celluloid.rb, line 336
def signal(name, value = nil)
  Thread.current[:celluloid_actor].signal name, value
end
sleep(interval) click to toggle source

Sleep letting the actor continue processing messages

# File lib/celluloid.rb, line 406
def sleep(interval)
  actor = Thread.current[:celluloid_actor]
  if actor
    actor.sleep(interval)
  else
    Kernel.sleep interval
  end
end
tasks() click to toggle source

Obtain the running tasks for this actor

# File lib/celluloid.rb, line 356
def tasks
  Thread.current[:celluloid_actor].tasks.to_a
end
terminate() click to toggle source

Terminate this actor

# File lib/celluloid.rb, line 331
def terminate
  Thread.current[:celluloid_actor].behavior_proxy.terminate!
end
timeout(duration) { || ... } click to toggle source

Timeout on task suspension (eg Sync calls to other actors)

# File lib/celluloid.rb, line 416
def timeout(duration)
  Thread.current[:celluloid_actor].timeout(duration) do
    yield
  end
end
unmonitor(actor) click to toggle source

Stop waiting for exit events from another actor

# File lib/celluloid.rb, line 371
def unmonitor(actor)
  Actor.unmonitor(actor)
end
wait(name) click to toggle source

Wait for the given signal

# File lib/celluloid.rb, line 341
def wait(name)
  Thread.current[:celluloid_actor].wait name
end