Class: Roby::Interface::V2::Async::JobMonitor

Inherits:
Object
  • Object
show all
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

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

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

#interfaceInterface (readonly)

Returns the async interface we are bound to.

Returns:

  • (Interface)

    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_idInteger (readonly)

Returns the job ID.

Returns:

  • (Integer)

    the job ID



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

def job_id
  @job_id
end

#placeholder_taskProtocol::Task (readonly)

Returns the job’s placeholder task.

Returns:



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

def placeholder_task
  @placeholder_task
end

#stateSymbol (readonly)

Returns the job’s current state.

Returns:

  • (Symbol)

    the job’s current state



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

def state
  @state
end

#taskProtocol::Task (readonly)

Returns the job’s main task.

Returns:



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

def task
  @task
end

Instance Method Details

#action_argumentsObject

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_modelRoby::Actions::Model::Action?

The job’s action model

Returns:

  • (Roby::Actions::Model::Action, nil)


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_nameString?

Returns the job’s action name

Returns:

  • (String, nil)


112
113
114
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 112

def action_name
    action_model&.name
end

#action_task?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


199
200
201
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 199

def active?
    interface.active_job_monitor?(self)
end

#dropObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


184
185
186
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 184

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.



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_nameObject



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

#killObject

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_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/v2/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/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

Yield Parameters:

  • state (Symbol)

    the new job state



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

define_hooks :on_progress

#planning_finished?Boolean

Tests whether planning finished

Returns:

  • (Boolean)


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

Parameters:



126
127
128
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 126

def replaced(new_task)
    @placeholder_task = new_task
end

#restartJobMonitor

Kill this job and start an equivalent one

Returns:

  • (JobMonitor)

    the monitor object for the new job. It is not listening to the new job yet, call #start for that



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

Returns:

  • (Boolean)


169
170
171
# File 'lib/roby/interface/v2/async/job_monitor.rb', line 169

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

#startObject

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

#stopObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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