Tasks are interruptable/resumable execution contexts used to run methods
Obtain the current task
# File lib/celluloid/tasks.rb, line 13 def self.current Thread.current[:celluloid_task] or raise NotTaskError, "not within a task context" end
Create a new task
# File lib/celluloid/tasks.rb, line 25 def initialize(type) @type = type @status = :new actor = Thread.current[:celluloid_actor] chain_id = Thread.current[:celluloid_chain_id] raise NotActorError, "can't create tasks outside of actors" unless actor create do begin @status = :running actor.setup_thread Thread.current[:celluloid_task] = self Thread.current[:celluloid_chain_id] = chain_id actor.tasks << self yield rescue Task::TerminatedError # Task was explicitly terminated ensure @status = :dead actor.tasks.delete self end end end
Suspend the running task, deferring to the scheduler
# File lib/celluloid/tasks.rb, line 18 def self.suspend(status) Task.current.suspend(status) end
# File lib/celluloid/tasks.rb, line 78 def backtrace end
# File lib/celluloid/tasks.rb, line 52 def create(&block) raise "Implement #{self.class}#create" end
Nicer string inspect for tasks
# File lib/celluloid/tasks.rb, line 85 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} @type=#{@type.inspect}, @status=#{@status.inspect}>" end
Resume a suspended task, giving it a value to return if needed
# File lib/celluloid/tasks.rb, line 68 def resume(value = nil) deliver(value) nil end
Is the current task still running?
# File lib/celluloid/tasks.rb, line 82 def running?; @status != :dead; end
Suspend the current task, changing the status to the given argument
# File lib/celluloid/tasks.rb, line 57 def suspend(status) @status = status value = signal raise value if value.is_a?(Task::TerminatedError) @status = :running value end
Terminate this task
# File lib/celluloid/tasks.rb, line 74 def terminate resume Task::TerminatedError.new("task was terminated") if running? end