Class: Roby::Interface::V1::Async::JobMonitor

Inherits:
Object
  • Object
show all
Includes:
Hooks, Hooks::InstanceHooks
Defined in:
lib/roby/interface/v1/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

Hooks collapse

Instance Method Summary collapse

Methods included from Hooks

included

Constructor Details

#initialize(interface, job_id, state: JOB_REACHABLE, task: nil, placeholder_task: task) ⇒ JobMonitor



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/roby/interface/v1/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

#interfaceInterface (readonly)



50
51
52
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 50

def interface
  @interface
end

#job_idInteger (readonly)



53
54
55
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 53

def job_id
  @job_id
end

#placeholder_taskRoby::Task (readonly)



59
60
61
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 59

def placeholder_task
  @placeholder_task
end

#stateSymbol (readonly)



62
63
64
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 62

def state
  @state
end

#taskRoby::Task (readonly)



56
57
58
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 56

def task
  @task
end

Instance Method Details

#action_argumentsObject

Returns the arguments that were passed to the action



114
115
116
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 114

def action_arguments
    task&.action_arguments if action_task?
end

#action_modelRoby::Actions::Model::Action?

The job’s action model



102
103
104
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 102

def action_model
    task&.action_model if action_task?
end

#action_nameString?

Returns the job’s action name



109
110
111
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 109

def action_name
    task&.action_model&.name if action_task?
end

#action_task?Boolean



95
96
97
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 95

def action_task?
    task&.respond_to?(:action_model)
end

#active?Boolean

Whether this job monitor is still active



196
197
198
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 196

def active?
    interface.active_job_monitor?(self)
end

#dropObject

Send a command to drop this job



212
213
214
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 212

def drop
    interface.client.drop_job(job_id)
end

#failed?Boolean

Tests whether this job ran and failed



176
177
178
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 176

def failed?
    @failed
end

#finalized?Boolean

Tests whether this job has been finalized



191
192
193
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 191

def finalized?
    Roby::Interface.finalized_state?(state)
end

#finished?Boolean

Tests whether this job ran and finished



181
182
183
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 181

def finished?
    @has_ran && terminated?
end

#inspectObject

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.



128
129
130
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 128

def inspect
    "#<JobMonitor #{interface} job_id=#{job_id} state=#{state} task=#{task}>"
end

#job_nameObject



90
91
92
93
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 90

def job_name
    args = task&.arguments
    (args[:job_name] if args) || action_name
end

#killObject

Send a command to kill this job



217
218
219
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 217

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



135
136
137
138
139
140
141
142
143
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 135

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_exceptionvoid

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/v1/async/job_monitor.rb', line 45

define_hooks :on_exception

#on_planning_failedvoid

This method returns an undefined value.

Hook called when we receive a planning failed exception for this job



36
# File 'lib/roby/interface/v1/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

Yield Parameters:

  • state (Symbol)

    the new job state



28
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 28

define_hooks :on_progress

#planning_finished?Boolean

Tests whether planning finished



161
162
163
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 161

def planning_finished?
    @planning_finished
end

#pretty_print(pp) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 221

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



123
124
125
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 123

def replaced(new_task)
    @placeholder_task = new_task
end

#restartJobMonitor

Kill this job and start an equivalent one



80
81
82
83
84
85
86
87
88
# File 'lib/roby/interface/v1/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



166
167
168
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 166

def running?
    Roby::Interface.running_state?(state)
end

#startObject

Start monitoring this job’s state



201
202
203
204
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 201

def start
    update_state(state)
    interface.add_job_monitor(self)
end

#stopObject

Stop monitoring this job’s state



207
208
209
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 207

def stop
    interface.remove_job_monitor(self)
end

#success?Boolean

Tests whether this job ran successfully



171
172
173
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 171

def success?
    @success
end

#terminated?Boolean

Tests whether this job is terminated



186
187
188
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 186

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



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/roby/interface/v1/async/job_monitor.rb', line 148

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