Class: Async::Background::Runtime::Task
- Inherits:
-
Object
- Object
- Async::Background::Runtime::Task
- Defined in:
- lib/async/background/runtime/task.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#fiber ⇒ Object
readonly
Returns the value of attribute fiber.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #cancelled? ⇒ Boolean
- #enter_block(waiter) ⇒ Object
- #exit_block ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(name: nil, group: nil, on_error: UNSET) ⇒ Task
constructor
A new instance of Task.
- #raise_if_cancelled! ⇒ Object
- #start(&block) ⇒ Object
- #stop ⇒ Object
- #wait(timeout = nil) ⇒ Object
- #waiting ⇒ Object
- #with_timeout(duration, &block) ⇒ Object
Constructor Details
#initialize(name: nil, group: nil, on_error: UNSET) ⇒ Task
Returns a new instance of Task.
15 16 17 18 19 20 21 22 23 |
# File 'lib/async/background/runtime/task.rb', line 15 def initialize(name: nil, group: nil, on_error: UNSET) @fiber = @scheduler = @blocker = @result = @error = nil @done = @cancelled = false @name = name @group = group @on_error = on_error @waiters = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/async/background/runtime/task.rb', line 7 def error @error end |
#fiber ⇒ Object (readonly)
Returns the value of attribute fiber.
7 8 9 |
# File 'lib/async/background/runtime/task.rb', line 7 def fiber @fiber end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/async/background/runtime/task.rb', line 7 def name @name end |
Class Method Details
.spawn(name: nil, on_error: UNSET, &block) ⇒ Object
9 10 11 12 13 |
# File 'lib/async/background/runtime/task.rb', line 9 def self.spawn(name: nil, on_error: UNSET, &block) task = new(name: name, on_error: on_error) task.start(&block) task end |
Instance Method Details
#cancelled? ⇒ Boolean
77 |
# File 'lib/async/background/runtime/task.rb', line 77 def cancelled? = @cancelled |
#enter_block(waiter) ⇒ Object
85 86 87 |
# File 'lib/async/background/runtime/task.rb', line 85 def enter_block(waiter) @blocker = waiter end |
#exit_block ⇒ Object
89 90 91 |
# File 'lib/async/background/runtime/task.rb', line 89 def exit_block @blocker = nil end |
#finished? ⇒ Boolean
78 |
# File 'lib/async/background/runtime/task.rb', line 78 def finished? = @done |
#raise_if_cancelled! ⇒ Object
81 82 83 |
# File 'lib/async/background/runtime/task.rb', line 81 def raise_if_cancelled! raise Cancel, 'task stopped' if @cancelled end |
#start(&block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/async/background/runtime/task.rb', line 25 def start(&block) raise ArgumentError, 'block required' unless block raise Error, 'task already started' if @fiber || @done @scheduler = Runtime.scheduler! takes_task = !block.arity.zero? scheduled = Fiber.schedule do adopt_fiber(Fiber.current) previous = Runtime.current_task Runtime.current_task = self begin complete(takes_task ? block.call(self) : block.call, nil) rescue Cancel complete(nil, nil) rescue Exception => e # rubocop:disable Lint/RescueException complete(nil, e) ensure Runtime.current_task = previous end end @fiber ||= scheduled if scheduled.is_a?(Fiber) self end |
#stop ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/async/background/runtime/task.rb', line 66 def stop return false if @done @cancelled = true return true if interrupt_fiber return true if release_blocker false end |
#wait(timeout = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/async/background/runtime/task.rb', line 51 def wait(timeout = nil) unless @done deadline = Runtime.deadline_for(timeout) raise TimeoutError, 'task did not finish in time' unless join(deadline) end raise @error if @error @result end |
#waiting ⇒ Object
79 |
# File 'lib/async/background/runtime/task.rb', line 79 def waiting = @waiters ? @waiters.size : 0 |
#with_timeout(duration, &block) ⇒ Object
62 63 64 |
# File 'lib/async/background/runtime/task.rb', line 62 def with_timeout(duration, &block) Runtime.with_timeout(duration, &block) end |