Module: RSMP::Task

Included in:
Node, Proxy
Defined in:
lib/rsmp/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#taskObject (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_taskObject



8
9
10
# File 'lib/rsmp/task.rb', line 8

def initialize_task
  @task = nil
end

#restartObject

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

#runObject

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

#startObject

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

#stopObject

stop our task



50
51
52
53
# File 'lib/rsmp/task.rb', line 50

def stop
  stop_subtasks
  stop_task if @task
end

#stop_subtasksObject



55
56
# File 'lib/rsmp/task.rb', line 55

def stop_subtasks
end

#stop_taskObject

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_statusObject

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

#waitObject

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