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

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

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.



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

#interfaceInterface (readonly)

Returns the async interface we are bound to.

Returns:

  • (Interface)

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

Returns the job ID.

Returns:

  • (Integer)

    the job ID



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

def job_id
  @job_id
end

#placeholder_taskRoby::Task (readonly)

Returns the job’s placeholder task.

Returns:



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

def placeholder_task
  @placeholder_task
end

#stateSymbol (readonly)

Returns the job’s current state.

Returns:

  • (Symbol)

    the job’s current state



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

def state
  @state
end

#taskRoby::Task (readonly)

Returns the job’s main task.

Returns:



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

def task
  @task
end

Instance Method Details

#action_argumentsObject

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

The job’s action model

Returns:

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


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

def action_model
    task && task.action_model
end

#action_nameString?

Returns the job’s action name

Returns:

  • (String, nil)


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

Returns:

  • (Boolean)


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

def active?
    interface.active_job_monitor?(self)
end

#dropObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

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.



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

#killObject

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



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

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



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

Yield Parameters:

  • state (Symbol)

    the new job state



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

define_hooks :on_progress

#planning_finished?Boolean

Tests whether planning finished

Returns:

  • (Boolean)


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

Parameters:



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

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



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

Returns:

  • (Boolean)


155
156
157
# File 'lib/roby/interface/async/job_monitor.rb', line 155

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

#startObject

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

#stopObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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