Module: RSMP::Task
Instance Attribute Summary collapse
-
#task ⇒ Object
readonly
Returns the value of attribute task.
Instance Method Summary collapse
- #initialize_task ⇒ Object
-
#restart ⇒ Object
initiate restart by raising a Restart exception.
-
#run ⇒ Object
perform any long-running work the method will be called from an async task, and should not return if subtasks are needed, the method should call wait() on each of them once running, ready() must be called.
-
#start ⇒ Object
start our async tasks and return immediately run() will be called inside the task to perform actual long-running work.
-
#stop ⇒ Object
stop our task.
- #stop_subtasks ⇒ Object
-
#stop_task ⇒ Object
stop our task and any subtask.
-
#task_status ⇒ Object
get the status of our task, or nil of no task.
-
#wait ⇒ Object
wait for our task to complete.
-
#wait_for_condition(condition, timeout:, task: Async::Task.current, &block) ⇒ Object
wait for an async condition to signal, then yield to block if block returns true we’re done.
Instance Attribute Details
#task ⇒ Object (readonly)
Returns the value of attribute task.
6 7 8 |
# File 'lib/rsmp/node/task.rb', line 6 def task @task end |
Instance Method Details
#initialize_task ⇒ Object
8 9 10 |
# File 'lib/rsmp/node/task.rb', line 8 def initialize_task @task = nil end |
#restart ⇒ Object
initiate restart by raising a Restart exception
39 40 41 |
# File 'lib/rsmp/node/task.rb', line 39 def restart raise Restart, "restart initiated by #{self.class.name}:#{object_id}" end |
#run ⇒ Object
perform any long-running work the method will be called from an async task, and should not return if subtasks are needed, the method should call wait() on each of them once running, ready() must be called
52 53 54 |
# File 'lib/rsmp/node/task.rb', line 52 def run start_subtasks end |
#start ⇒ Object
start our async tasks and return immediately run() will be called inside the task to perform actual long-running work
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rsmp/node/task.rb', line 14 def start return if @task # Use current task context if available, otherwise create new reactor if Async::Task.current? Async::Task.current.async do |task| task.annotate "#{self.class.name} main task" @task = task run stop_subtasks @task = nil end else Async do |task| task.annotate "#{self.class.name} main task" @task = task run stop_subtasks @task = nil end end self end |
#stop ⇒ Object
stop our task
62 63 64 65 |
# File 'lib/rsmp/node/task.rb', line 62 def stop stop_subtasks stop_task if @task end |
#stop_subtasks ⇒ Object
67 |
# File 'lib/rsmp/node/task.rb', line 67 def stop_subtasks; end |
#stop_task ⇒ Object
stop our task and any subtask
70 71 72 73 |
# File 'lib/rsmp/node/task.rb', line 70 def stop_task @task.stop @task = nil end |
#task_status ⇒ Object
get the status of our task, or nil of no task
44 45 46 |
# File 'lib/rsmp/node/task.rb', line 44 def task_status @task&.status end |
#wait ⇒ Object
wait for our task to complete
57 58 59 |
# File 'lib/rsmp/node/task.rb', line 57 def wait @task&.wait end |
#wait_for_condition(condition, timeout:, task: Async::Task.current, &block) ⇒ Object
wait for an async condition to signal, then yield to block if block returns true we’re done. otherwise, wait again
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rsmp/node/task.rb', line 77 def wait_for_condition(condition, timeout:, task: Async::Task.current, &block) raise "Can't wait without a task" unless task task.with_timeout(timeout) do while task.running? value = condition.wait return value unless block result = yield value return result if result end raise "Can't wait for condition because task #{task.object_id} #{task.annotation} is not running" end rescue Async::TimeoutError raise RSMP::TimeoutError end |