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/task.rb', line 6 def task @task end |
Instance Method Details
#initialize_task ⇒ Object
8 9 10 |
# File 'lib/rsmp/task.rb', line 8 def initialize_task @task = nil end |
#restart ⇒ Object
initiate restart by raising a Restart exception
27 28 29 |
# File 'lib/rsmp/task.rb', line 27 def restart raise Restart.new "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
40 41 42 |
# File 'lib/rsmp/task.rb', line 40 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 |
# File 'lib/rsmp/task.rb', line 14 def start return if @task Async do |task| task.annotate "#{self.class.name} main task" @task = task run stop_subtasks @task = nil end self end |
#stop ⇒ Object
stop our task
50 51 52 53 |
# File 'lib/rsmp/task.rb', line 50 def stop stop_subtasks stop_task if @task end |
#stop_subtasks ⇒ Object
55 56 |
# File 'lib/rsmp/task.rb', line 55 def stop_subtasks end |
#stop_task ⇒ Object
stop our task and any subtask
59 60 61 62 |
# File 'lib/rsmp/task.rb', line 59 def stop_task @task.stop @task = nil end |
#task_status ⇒ Object
get the status of our task, or nil of no task
32 33 34 |
# File 'lib/rsmp/task.rb', line 32 def task_status @task.status if @task end |
#wait ⇒ Object
wait for our task to complete
45 46 47 |
# File 'lib/rsmp/task.rb', line 45 def wait @task.wait if @task 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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rsmp/task.rb', line 66 def wait_for_condition condition, timeout:, task:Async::Task.current, &block unless task raise RuntimeError.new("Can't wait without a task") end task.with_timeout(timeout) do while task.running? value = condition.wait return value unless block result = yield value return result if result end raise RuntimeError.new("Can't wait for condition because task #{task.object_id} #{task.annotation} is not running") end rescue Async::TimeoutError raise RSMP::TimeoutError.new end |