Class: Celluloid::Task

Inherits:
Object show all
Defined in:
lib/vendor/celluloid/lib/celluloid/task.rb

Overview

Tasks are interruptable/resumable execution contexts used to run methods

Defined Under Namespace

Classes: TerminatedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Task

Run the given block within a task



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 26

def initialize(type)
  @type = type

  actor   = Thread.current[:actor]
  mailbox = Thread.current[:mailbox]

  @fiber = Fiber.new do
    Thread.current[:actor]   = actor
    Thread.current[:mailbox] = mailbox

    Fiber.current.task = self

    begin
      yield
    rescue TerminatedError
      # Task was explicitly terminated
    end
  end
end

Instance Attribute Details

#typeObject (readonly)

what type of task is this?



9
10
11
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 9

def type
  @type
end

Class Method Details

.currentObject

Obtain the current task



12
13
14
15
16
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 12

def self.current
  task = Fiber.current.task
  raise "not in task scope" unless task
  task
end

.suspend(value = nil) ⇒ Object

Suspend the running task, deferring to the scheduler

Raises:



19
20
21
22
23
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 19

def self.suspend(value = nil)
  result = Fiber.yield(value)
  raise TerminatedError, "task was terminated" if result == TerminatedError
  result
end

Instance Method Details

#inspectObject

Nicer string inspect for tasks



69
70
71
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 69

def inspect
  "<Celluloid::Task:0x#{object_id.to_s(16)} @type=#{@type.inspect}, @running=#{@fiber.alive?}>"
end

#resume(value = nil) ⇒ Object

Resume a suspended task, giving it a value to return if needed



47
48
49
50
51
52
53
54
55
56
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 47

def resume(value = nil)
  @fiber.resume value
  nil
rescue FiberError
  raise DeadTaskError, "cannot resume a dead task"
rescue RuntimeError => ex
  # These occur spuriously on 1.9.3 if we shut down an actor with running tasks
  return if ex.message == ""
  raise
end

#running?Boolean

Is the current task still running?

Returns:

  • (Boolean)


66
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 66

def running?; @fiber.alive?; end

#terminateObject

Terminate this task



59
60
61
62
63
# File 'lib/vendor/celluloid/lib/celluloid/task.rb', line 59

def terminate
  resume TerminatedError
rescue FiberError
  # If we're getting this the task should already be dead
end