lib/celluloid/actor.rb in celluloid-0.18.0.pre vs lib/celluloid/actor.rb in celluloid-0.18.0.pre2
- old
+ new
@@ -14,18 +14,18 @@
def_delegators :"Celluloid.actor_system", :[], :[]=, :delete, :registered, :clear_registry
# Obtain the current actor
def current
actor = Thread.current[:celluloid_actor]
- fail NotActorError, "not in actor scope" unless actor
+ raise NotActorError, "not in actor scope" unless actor
actor.behavior_proxy
end
# Obtain the name of the current actor
def registered_name
actor = Thread.current[:celluloid_actor]
- fail NotActorError, "not in actor scope" unless actor
+ raise NotActorError, "not in actor scope" unless actor
actor.name
end
# Invoke a method on the given actor via its mailbox
def call(mailbox, meth, *args, &block)
@@ -50,17 +50,17 @@
Celluloid.actor_system.running
end
# Watch for exit events from another actor
def monitor(actor)
- fail NotActorError, "can't link outside actor context" unless Celluloid.actor?
+ raise NotActorError, "can't link outside actor context" unless Celluloid.actor?
Thread.current[:celluloid_actor].linking_request(actor, :link)
end
# Stop waiting for exit events from another actor
def unmonitor(actor)
- fail NotActorError, "can't link outside actor context" unless Celluloid.actor?
+ raise NotActorError, "can't link outside actor context" unless Celluloid.actor?
Thread.current[:celluloid_actor].linking_request(actor, :unlink)
end
# Link to another actor
def link(actor)
@@ -130,12 +130,15 @@
setup_thread
run
end
@proxy = Proxy::Actor.new(@mailbox, @thread)
+
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
+ # rubocop:disable Style/GlobalVars
Celluloid::Probe.actor_created(self) if $CELLULOID_MONITORING
- Celluloid::Actor::Manager.actor_created(self) if $CELLULOID_MANAGED
+ # rubocop:enable Style/GlobalVars
end
def behavior_proxy
@behavior.proxy
end
@@ -185,29 +188,32 @@
Timers::Wait.for(LINKING_TIMEOUT) do |remaining|
begin
message = @mailbox.receive(remaining) do |msg|
msg.is_a?(LinkingResponse) &&
- msg.actor.mailbox.address == receiver.mailbox.address &&
- msg.type == type
+ msg.actor.mailbox.address == receiver.mailbox.address &&
+ msg.type == type
end
rescue TaskTimeout
next # IO reactor did something, no message in queue yet.
end
if message.instance_of? LinkingResponse
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
+ # rubocop:disable Style/GlobalVars
Celluloid::Probe.actors_linked(self, receiver) if $CELLULOID_MONITORING
+ # rubocop:enable Style/GlobalVars
system_events.each { |ev| @mailbox << ev }
return
elsif message.is_a? SystemEvent
# Queue up pending system events to be processed after we've successfully linked
system_events << message
- else fail "Unexpected message type: #{message.class}. Expected LinkingResponse, NilClass, SystemEvent."
+ else raise "Unexpected message type: #{message.class}. Expected LinkingResponse, NilClass, SystemEvent."
end
end
- fail TaskTimeout, "linking timeout of #{LINKING_TIMEOUT} seconds exceeded with receiver: #{receiver}"
+ raise TaskTimeout, "linking timeout of #{LINKING_TIMEOUT} seconds exceeded with receiver: #{receiver}"
end
end
# Send a signal with the given name to all waiting methods
def signal(name, value = nil)
@@ -216,19 +222,19 @@
# Wait for the given signal
def wait(name)
@signals.wait name
end
-
+
# Register a new handler for a given pattern
def handle(*patterns, &block)
@handlers.handle(*patterns, &block)
end
# Receive an asynchronous message
def receive(timeout = nil, &block)
- while true
+ loop do
message = @receivers.receive(timeout, &block)
return message unless message.is_a?(SystemEvent)
handle_system_event(message)
end
@@ -278,20 +284,20 @@
Celluloid.suspend(:sleeping, sleeper)
end
# Handle standard low-priority messages
def handle_message(message)
- unless @handlers.handle_message(message)
- unless @receivers.handle_message(message)
- Internals::Logger.debug "Discarded message (unhandled): #{message}" if $CELLULOID_DEBUG
- end
- end
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
+ # rubocop:disable Metrics/LineLength, Style/GlobalVars
+ Internals::Logger.debug "Discarded message (unhandled): #{message}" if !@handlers.handle_message(message) && !@receivers.handle_message(message) && $CELLULOID_DEBUG
+ # rubocop:enable Metrics/LineLength, Style/GlobalVars
+
message
end
def default_exit_handler(event)
- fail event.reason if event.reason
+ raise event.reason if event.reason
end
# Handle any exceptions that occur within a running actor
def handle_crash(exception)
# TODO: add meta info
@@ -310,10 +316,14 @@
Thread.current[:celluloid_mailbox] = nil
end
# Clean up after this actor
def cleanup(exit_event)
+ # !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
+ # rubocop:disable Style/GlobalVars
Celluloid::Probe.actor_died(self) if $CELLULOID_MONITORING
+ # rubocop:enable Style/GlobalVars
+
@mailbox.shutdown
@links.each do |actor|
actor.mailbox << exit_event if actor.mailbox.alive?
end