Class: Roby::Interface::Async::JobMonitor
- Includes:
- Hooks, Hooks::InstanceHooks
- Defined in:
- lib/roby/interface/async/job_monitor.rb
Overview
Asynchronous monitoring of a job
This is usually not created directly, but either by calling Interface#on_job or Interface#find_all_jobs. The jobs created by these two methods not listening for the job’s progress, you must call #start on them to start tracking the job progress.
Then call #stop to remove the monitor.
Constant Summary collapse
- JOB_REACHABLE =
:reachable
Instance Attribute Summary collapse
-
#interface ⇒ Interface
readonly
The async interface we are bound to.
-
#job_id ⇒ Integer
readonly
The job ID.
-
#placeholder_task ⇒ Roby::Task
readonly
The job’s placeholder task.
-
#state ⇒ Symbol
readonly
The job’s current state.
-
#task ⇒ Roby::Task
readonly
The job’s main task.
Hooks collapse
-
#on_exception ⇒ void
Hook called when we receive an exception involving this job Note that a planning failed exception is both received by this handler and by #on_planning_failed.
-
#on_planning_failed ⇒ void
Hook called when we receive a planning failed exception for this job.
-
#on_progress {|state| ... } ⇒ void
Hook called when there is an upcoming notification.
Instance Method Summary collapse
-
#action_arguments ⇒ Object
Returns the arguments that were passed to the action.
-
#action_model ⇒ Roby::Actions::Model::Action?
The job’s action model.
-
#action_name ⇒ String?
Returns the job’s action name.
-
#active? ⇒ Boolean
Whether this job monitor is still active.
-
#drop ⇒ Object
Send a command to drop this job.
-
#failed? ⇒ Boolean
Tests whether this job ran and failed.
-
#finalized? ⇒ Boolean
Tests whether this job has been finalized.
-
#finished? ⇒ Boolean
Tests whether this job ran and finished.
-
#initialize(interface, job_id, state: JOB_REACHABLE, task: nil, placeholder_task: task) ⇒ JobMonitor
constructor
A new instance of JobMonitor.
- #inspect ⇒ Object private
-
#kill ⇒ Object
Send a command to kill this job.
-
#notify_exception(kind, exception) ⇒ Object
private
Triggers #on_exception and #on_planning_failed hooks.
-
#planning_finished? ⇒ Boolean
Tests whether planning finished.
-
#replaced(new_task) ⇒ Object
private
Called when the placeholder task got replaced.
-
#restart ⇒ JobMonitor
Kill this job and start an equivalent one.
-
#running? ⇒ Boolean
Tests whether this job is running.
-
#start ⇒ Object
Start monitoring this job’s state.
-
#stop ⇒ Object
Stop monitoring this job’s state.
-
#success? ⇒ Boolean
Tests whether this job ran successfully.
-
#terminated? ⇒ Boolean
Tests whether this job is terminated.
-
#update_state(state) ⇒ Object
private
Called by Interface to update the job’s state.
Methods included from Hooks
Constructor Details
#initialize(interface, job_id, state: JOB_REACHABLE, task: nil, placeholder_task: task) ⇒ JobMonitor
Returns a new instance of JobMonitor.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/roby/interface/async/job_monitor.rb', line 61 def initialize(interface, job_id, state: JOB_REACHABLE, task: nil, placeholder_task: task) @interface = interface @job_id = job_id @success = false @failed = false @has_ran = false @planning_finished = false update_state(state) @task = task @placeholder_task = placeholder_task end |
Instance Attribute Details
#interface ⇒ Interface (readonly)
Returns the async interface we are bound to.
47 48 49 |
# File 'lib/roby/interface/async/job_monitor.rb', line 47 def interface @interface end |
#job_id ⇒ Integer (readonly)
Returns the job ID.
50 51 52 |
# File 'lib/roby/interface/async/job_monitor.rb', line 50 def job_id @job_id end |
#placeholder_task ⇒ Roby::Task (readonly)
Returns the job’s placeholder task.
56 57 58 |
# File 'lib/roby/interface/async/job_monitor.rb', line 56 def placeholder_task @placeholder_task end |
#state ⇒ Symbol (readonly)
Returns the job’s current state.
59 60 61 |
# File 'lib/roby/interface/async/job_monitor.rb', line 59 def state @state end |
#task ⇒ Roby::Task (readonly)
Returns the job’s main task.
53 54 55 |
# File 'lib/roby/interface/async/job_monitor.rb', line 53 def task @task end |
Instance Method Details
#action_arguments ⇒ Object
Returns the arguments that were passed to the action
102 103 104 |
# File 'lib/roby/interface/async/job_monitor.rb', line 102 def action_arguments task && task.action_arguments end |
#action_model ⇒ Roby::Actions::Model::Action?
The job’s action model
90 91 92 |
# File 'lib/roby/interface/async/job_monitor.rb', line 90 def action_model task && task.action_model end |
#action_name ⇒ String?
Returns the job’s action name
97 98 99 |
# File 'lib/roby/interface/async/job_monitor.rb', line 97 def action_name task && task.action_model.name end |
#active? ⇒ Boolean
Whether this job monitor is still active
185 186 187 |
# File 'lib/roby/interface/async/job_monitor.rb', line 185 def active? interface.active_job_monitor?(self) end |
#drop ⇒ Object
Send a command to drop this job
201 202 203 |
# File 'lib/roby/interface/async/job_monitor.rb', line 201 def drop interface.client.drop_job(job_id) end |
#failed? ⇒ Boolean
Tests whether this job ran and failed
165 166 167 |
# File 'lib/roby/interface/async/job_monitor.rb', line 165 def failed? @failed end |
#finalized? ⇒ Boolean
Tests whether this job has been finalized
180 181 182 |
# File 'lib/roby/interface/async/job_monitor.rb', line 180 def finalized? Roby::Interface.finalized_state?(state) end |
#finished? ⇒ Boolean
Tests whether this job ran and finished
170 171 172 |
# File 'lib/roby/interface/async/job_monitor.rb', line 170 def finished? @has_ran && terminated? end |
#inspect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
116 117 118 |
# File 'lib/roby/interface/async/job_monitor.rb', line 116 def inspect "#<JobMonitor #{interface} job_id=#{job_id} state=#{state} task=#{task}>" end |
#kill ⇒ Object
Send a command to kill this job
206 207 208 |
# File 'lib/roby/interface/async/job_monitor.rb', line 206 def kill interface.client.kill_job(job_id) end |
#notify_exception(kind, exception) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Triggers #on_exception and #on_planning_failed hooks
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/roby/interface/async/job_monitor.rb', line 123 def notify_exception(kind, exception) if exception.exception.kind_of?(PlanningFailedError) if job_id = exception.exception.planning_task.arguments[:job_id] if job_id == self.job_id run_hook :on_planning_failed, kind, exception end end end run_hook :on_exception, kind, exception end |
#on_exception ⇒ void
This method returns an undefined value.
Hook called when we receive an exception involving this job Note that a planning failed exception is both received by this handler and by #on_planning_failed
42 |
# File 'lib/roby/interface/async/job_monitor.rb', line 42 define_hooks :on_exception |
#on_planning_failed ⇒ void
This method returns an undefined value.
Hook called when we receive a planning failed exception for this job
33 |
# File 'lib/roby/interface/async/job_monitor.rb', line 33 define_hooks :on_planning_failed |
#on_progress {|state| ... } ⇒ void
This method returns an undefined value.
Hook called when there is an upcoming notification
25 |
# File 'lib/roby/interface/async/job_monitor.rb', line 25 define_hooks :on_progress |
#planning_finished? ⇒ Boolean
Tests whether planning finished
150 151 152 |
# File 'lib/roby/interface/async/job_monitor.rb', line 150 def planning_finished? @planning_finished end |
#replaced(new_task) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Called when the placeholder task got replaced
111 112 113 |
# File 'lib/roby/interface/async/job_monitor.rb', line 111 def replaced(new_task) @placeholder_task = new_task end |
#restart ⇒ JobMonitor
Kill this job and start an equivalent one
77 78 79 80 81 82 83 84 85 |
# File 'lib/roby/interface/async/job_monitor.rb', line 77 def restart batch = interface.client.create_batch if !terminated? batch.kill_job(job_id) end batch.start_job(action_name, action_arguments) job_id = batch.__process.started_jobs_id.first interface.monitor_job(job_id) end |
#running? ⇒ Boolean
Tests whether this job is running
155 156 157 |
# File 'lib/roby/interface/async/job_monitor.rb', line 155 def running? Roby::Interface.running_state?(state) end |
#start ⇒ Object
Start monitoring this job’s state
190 191 192 193 |
# File 'lib/roby/interface/async/job_monitor.rb', line 190 def start update_state(state) interface.add_job_monitor(self) end |
#stop ⇒ Object
Stop monitoring this job’s state
196 197 198 |
# File 'lib/roby/interface/async/job_monitor.rb', line 196 def stop interface.remove_job_monitor(self) end |
#success? ⇒ Boolean
Tests whether this job ran successfully
160 161 162 |
# File 'lib/roby/interface/async/job_monitor.rb', line 160 def success? @success end |
#terminated? ⇒ Boolean
Tests whether this job is terminated
175 176 177 |
# File 'lib/roby/interface/async/job_monitor.rb', line 175 def terminated? Roby::Interface.terminal_state?(state) end |
#update_state(state) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Called by Interface to update the job’s state
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/roby/interface/async/job_monitor.rb', line 137 def update_state(state) @state = state if state != JOB_REACHABLE @planning_finished ||= Roby::Interface.planning_finished_state?(state) @success ||= Roby::Interface.success_state?(state) @failed ||= Roby::Interface.error_state?(state) @has_ran ||= (Roby::Interface.planning_finished_state?(state) && state != JOB_READY && state != JOB_PLANNING_FAILED) end run_hook :on_progress, state end |