Class: Roby::Interface::V2::Async::JobMonitor
- Includes:
- Hooks, Hooks::InstanceHooks
- Defined in:
- lib/roby/interface/v2/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 ⇒ Protocol::Task
readonly
The job’s placeholder task.
-
#state ⇒ Symbol
readonly
The job’s current state.
-
#task ⇒ Protocol::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.
- #action_task? ⇒ Boolean
-
#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
- #job_name ⇒ Object
-
#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.
- #pretty_print(pp) ⇒ Object
-
#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.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 64 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.
50 51 52 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 50 def interface @interface end |
#job_id ⇒ Integer (readonly)
Returns the job ID.
53 54 55 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 53 def job_id @job_id end |
#placeholder_task ⇒ Protocol::Task (readonly)
Returns the job’s placeholder task.
59 60 61 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 59 def placeholder_task @placeholder_task end |
#state ⇒ Symbol (readonly)
Returns the job’s current state.
62 63 64 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 62 def state @state end |
#task ⇒ Protocol::Task (readonly)
Returns the job’s main task.
56 57 58 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 56 def task @task end |
Instance Method Details
#action_arguments ⇒ Object
Returns the arguments that were passed to the action
117 118 119 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 117 def action_arguments task.arguments[:action_arguments] if action_task? end |
#action_model ⇒ Roby::Actions::Model::Action?
The job’s action model
105 106 107 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 105 def action_model task.arguments[:action_model] if action_task? end |
#action_name ⇒ String?
Returns the job’s action name
112 113 114 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 112 def action_name action_model&.name end |
#action_task? ⇒ Boolean
96 97 98 99 100 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 96 def action_task? return unless task task.arguments[:action_model] end |
#active? ⇒ Boolean
Whether this job monitor is still active
199 200 201 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 199 def active? interface.active_job_monitor?(self) end |
#drop ⇒ Object
Send a command to drop this job
215 216 217 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 215 def drop interface.client.drop_job(job_id) end |
#failed? ⇒ Boolean
Tests whether this job ran and failed
179 180 181 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 179 def failed? @failed end |
#finalized? ⇒ Boolean
Tests whether this job has been finalized
194 195 196 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 194 def finalized? Roby::Interface.finalized_state?(state) end |
#finished? ⇒ Boolean
Tests whether this job ran and finished
184 185 186 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 184 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.
131 132 133 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 131 def inspect "#<JobMonitor #{interface} job_id=#{job_id} state=#{state} task=#{task}>" end |
#job_name ⇒ Object
90 91 92 93 94 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 90 def job_name return unless task task.arguments[:job_name] || action_name end |
#kill ⇒ Object
Send a command to kill this job
220 221 222 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 220 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
138 139 140 141 142 143 144 145 146 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 138 def notify_exception(kind, exception) if exception.exception.kind_of?(PlanningFailedError) job_id = exception.exception.planning_task.arguments[:job_id] if job_id && (job_id == self.job_id) run_hook :on_planning_failed, kind, exception 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
45 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 45 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
36 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 36 define_hooks :on_planning_failed |
#on_progress {|state| ... } ⇒ void
This method returns an undefined value.
Hook called when there is an upcoming notification
28 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 28 define_hooks :on_progress |
#planning_finished? ⇒ Boolean
Tests whether planning finished
164 165 166 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 164 def planning_finished? @planning_finished end |
#pretty_print(pp) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 224 def pretty_print(pp) pp.text "##{job_id} #{action_name}" unless action_arguments.empty? pp.nest(2) do action_arguments.each do |k, v| pp.breakable pp.text "#{k}: " pp.nest(2) { v.pretty_print(pp) } end end end pp.breakable pp.breakable placeholder_task.pretty_print(pp) 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
126 127 128 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 126 def replaced(new_task) @placeholder_task = new_task end |
#restart ⇒ JobMonitor
Kill this job and start an equivalent one
80 81 82 83 84 85 86 87 88 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 80 def restart batch = interface.client.create_batch unless 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
169 170 171 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 169 def running? Roby::Interface.running_state?(state) end |
#start ⇒ Object
Start monitoring this job’s state
204 205 206 207 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 204 def start update_state(state) interface.add_job_monitor(self) end |
#stop ⇒ Object
Stop monitoring this job’s state
210 211 212 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 210 def stop interface.remove_job_monitor(self) end |
#success? ⇒ Boolean
Tests whether this job ran successfully
174 175 176 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 174 def success? @success end |
#terminated? ⇒ Boolean
Tests whether this job is terminated
189 190 191 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 189 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
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 151 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 |