Class: Roby::Interface::V2::Async::ActionMonitor

Inherits:
Object
  • Object
show all
Includes:
Hooks, Hooks::InstanceHooks
Defined in:
lib/roby/interface/v2/async/action_monitor.rb

Overview

An action definition

While JobMonitor represents a running / instanciated job, this represents just an action with a set of argument. It binds automatically to a matching running job if there is one.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hooks

included

Constructor Details

#initialize(interface, action_name, static_arguments = {}) ⇒ ActionMonitor

Returns a new instance of ActionMonitor.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 112

def initialize(interface, action_name, static_arguments = {})
    @interface, @action_name, @static_arguments =
        interface, action_name, static_arguments
    @arguments = {}

    interface.on_reachable do
        run_hook :on_progress
    end
    interface.on_unreachable do
        unreachable!
    end
    interface.on_job(action_name: action_name) do |job|
        if !self.async || self.job_id != job.job_id || terminated?
            matching = static_arguments.all? do |arg_name, arg_val|
                job.action_arguments[arg_name.to_sym] == arg_val
            end
            if matching
                self.async = job
                job.start
            end
        end
    end
end

Instance Attribute Details

#action_nameObject (readonly)

The action name



16
17
18
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 16

def action_name
  @action_name
end

#argumentsObject (readonly)

The arguments that have been set to override the static arguments, or set arguments not yet set in #static_arguments



21
22
23
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 21

def arguments
  @arguments
end

#asyncObject

The underlying JobMonitor object if we’re tracking a job



23
24
25
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 23

def async
  @async
end

#interfaceObject (readonly)

The underlying Async::Interface



14
15
16
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 14

def interface
  @interface
end

#static_argumentsObject (readonly)

The arguments that are part of the job definition itself



18
19
20
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 18

def static_arguments
  @static_arguments
end

Instance Method Details

#action_argumentsObject

The set of arguments that should be passed to the action

It is basically the merged #static_arguments and #arguments



49
50
51
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 49

def action_arguments
    static_arguments.merge(arguments)
end

#drop(batch: nil) ⇒ Object

Drop this job

Parameters:



71
72
73
74
75
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 71

def drop(batch: nil)
    handle_batch_argument(batch) do |b|
        b.drop_job(async.job_id)
    end
end

#exists?Boolean

Whether there is a job matching this action monitor running

Returns:

  • (Boolean)


36
37
38
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 36

def exists?
    !!async
end

#failed?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 144

def failed?
    async&.failed?
end

#finished?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 148

def finished?
    async&.finished?
end

#handle_batch_argument(batch) ⇒ 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.

Helper to handle the batch argument in e.g. #kill and #restart



57
58
59
60
61
62
63
64
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 57

def handle_batch_argument(batch)
    if batch
        yield(batch)
    else
        yield(batch = interface.create_batch)
        batch.__process
    end
end

#job_idObject

The job ID of the last job that ran



41
42
43
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 41

def job_id
    async&.job_id
end

#kill(batch: nil) ⇒ Object

Kill this job

Parameters:



82
83
84
85
86
87
88
89
90
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 82

def kill(batch: nil)
    unless running?
        raise InvalidState, "cannot kill a non-running action"
    end

    handle_batch_argument(batch) do |b|
        b.kill_job(async.job_id)
    end
end

#on_progressvoid

This method returns an undefined value.

Hooks called when self got updated



33
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 33

define_hooks :on_progress

#restart(arguments = self.action_arguments, batch: nil, lazy: false) ⇒ Object

Start or restart a job based on this action

Parameters:



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 99

def restart(
    arguments = self.action_arguments, batch: nil, lazy: false
)
    if lazy && running? && (arguments == async.action_arguments)
        return
    end

    handle_batch_argument(batch) do |b|
        kill(batch: b) if running?
        b.start_job(action_name, **arguments)
    end
end

#running?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 136

def running?
    async&.running?
end

#stateObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 156

def state
    if interface.reachable?
        if !async
            :reachable
        else
            async.state
        end
    else
        :unreachable
    end
end

#success?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 140

def success?
    async&.success?
end

#terminated?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/roby/interface/v2/async/action_monitor.rb', line 152

def terminated?
    async&.terminated?
end

#unreachable!Object



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

def unreachable!
    @async = nil
    run_hook :on_progress
end