lib/celluloid.rb in celluloid-0.9.0 vs lib/celluloid.rb in celluloid-0.9.1

- old
+ new

@@ -1,11 +1,11 @@ require 'logger' require 'thread' require 'timeout' module Celluloid - SHUTDOWN_TIMEOUT = 60 # How long actors have to terminate + SHUTDOWN_TIMEOUT = 120 # How long actors have to terminate @logger = Logger.new STDERR class << self attr_accessor :logger # Thread-safe logger class @@ -49,33 +49,51 @@ else Kernel.sleep interval end end + # Generate a Universally Unique Identifier + def uuid + UUID.generate + end + + # Obtain the number of CPUs in the system + def cores + CPUCounter.cores + end + alias_method :cpus, :cores + alias_method :ncpus, :cores + # Define an exception handler for actor crashes def exception_handler(&block) Logger.exception_handler(&block) end # Shut down all running actors # FIXME: This should probably attempt a graceful shutdown of the supervision # tree before iterating through all actors and telling them to terminate. def shutdown Timeout.timeout(SHUTDOWN_TIMEOUT) do - futures = Actor.all.each do |actor| + actors = Actor.all + Logger.info "Terminating #{actors.size} actors..." if actors.size > 0 + + # Actors cannot self-terminate, you must do it for them + terminators = actors.each do |actor| begin actor.future(:terminate) rescue DeadActorError, MailboxError end end - futures.each do |future| + terminators.each do |terminator| begin - future.value + terminator.value rescue DeadActorError, MailboxError end end + + Logger.info "Shutdown completed cleanly" end end end # Terminate all actors at exit @@ -84,11 +102,11 @@ # Class methods added to classes which include Celluloid module ClassMethods # Create a new actor def new(*args, &block) proxy = Actor.new(allocate).proxy - proxy.send(:__send__, :initialize, *args, &block) + proxy._send_(:initialize, *args, &block) proxy end alias_method :spawn, :new # Create a new actor and link to the current one @@ -96,11 +114,11 @@ current_actor = Celluloid.current_actor raise NotActorError, "can't link outside actor context" unless current_actor proxy = Actor.new(allocate).proxy current_actor.link proxy - proxy.send(:__send__, :initialize, *args, &block) + proxy._send_(:initialize, *args, &block) proxy end alias_method :spawn_link, :new_link # Create a supervisor which ensures an instance of an actor will restart @@ -253,10 +271,15 @@ # Call a block after a given interval, returning a Celluloid::Timer object def after(interval, &block) Thread.current[:actor].after(interval, &block) end + # Call a block every given interval, returning a Celluloid::Timer object + def every(interval, &block) + Thread.current[:actor].every(interval, &block) + end + # 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 def defer(&block) # This implementation relies on the present implementation of @@ -278,21 +301,22 @@ # 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 + return end super end end require 'celluloid/version' require 'celluloid/actor_proxy' require 'celluloid/calls' require 'celluloid/core_ext' +require 'celluloid/cpu_counter' require 'celluloid/events' require 'celluloid/fiber' require 'celluloid/fsm' require 'celluloid/links' require 'celluloid/logger' @@ -303,9 +327,10 @@ require 'celluloid/responses' require 'celluloid/signals' require 'celluloid/task' require 'celluloid/timers' require 'celluloid/thread_pool' +require 'celluloid/uuid' require 'celluloid/actor' require 'celluloid/future' require 'celluloid/group' require 'celluloid/supervisor'