class Roby::Interface::Async::ActionMonitor

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.

Attributes

action_name[R]

The action name

arguments[R]

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

async[R]

The underlying {JobMonitor} object if we're tracking a job

interface[R]

The underlying Async::Interface

static_arguments[R]

The arguments that are part of the job definition itself

Public Class Methods

new(interface, action_name, static_arguments = Hash.new) click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 114
def initialize(interface, action_name, static_arguments = Hash.new)
    @interface, @action_name, @static_arguments =
        interface, action_name, static_arguments
    @arguments = Hash.new

    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

Public Instance Methods

action_arguments() click to toggle source

The set of arguments that should be passed to the action

It is basically the merged {#static_arguments} and {#arguments}

# File lib/roby/interface/async/action_monitor.rb, line 51
def action_arguments
    static_arguments.merge(arguments)
end
async=(async) click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 175
def async=(async)
    @async = async
    async.on_progress do
        if self.async == async
            run_hook :on_progress
        end
    end
    run_hook :on_progress
end
drop(batch: nil) click to toggle source

Drop this job

@param [Client::BatchContext] batch if given, the restart

commands will be added to this batch. Otherwise, a new batch
is created and {Client::BatchContext#__process} is called.
# File lib/roby/interface/async/action_monitor.rb, line 73
def drop(batch: nil)
    handle_batch_argument(batch) do |b|
        b.drop_job(async.job_id)
    end
end
exists?() click to toggle source

Whether there is a job matching this action monitor running

# File lib/roby/interface/async/action_monitor.rb, line 33
def exists?
    !!async
end
failed?() click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 146
def failed?
    async && async.failed?
end
finished?() click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 150
def finished?
    async && async.finished?
end
handle_batch_argument(batch) { |batch| ... } click to toggle source

@api private

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

# File lib/roby/interface/async/action_monitor.rb, line 59
def handle_batch_argument(batch)
    if batch
        yield(batch)
    else
        yield(batch = interface.create_batch)
        batch.__process
    end
end
job_id() click to toggle source

The job ID of the last job that ran

# File lib/roby/interface/async/action_monitor.rb, line 43
def job_id
    async && async.job_id
end
kill(batch: nil) click to toggle source

Kill this job

@param [Client::BatchContext] batch if given, the restart

commands will be added to this batch. Otherwise, a new batch
is created and {Client::BatchContext#__process} is called.
# File lib/roby/interface/async/action_monitor.rb, line 84
def kill(batch: nil)
    if !running?
        raise InvalidState, "cannot kill a non-running action"
    end

    handle_batch_argument(batch) do |b|
        b.kill_job(async.job_id)
    end
end
restart(arguments = self.action_arguments, batch: nil, lazy: false) click to toggle source

Start or restart a job based on this action

@param [Hash] arguments the arguments that should be used

instead of {#action_arguments}

@param [Client::BatchContext] batch if given, the restart

commands will be added to this batch. Otherwise, a new batch
is created and {Client::BatchContext#__process} is called.
# File lib/roby/interface/async/action_monitor.rb, line 101
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|
        if running?
            kill(batch: b)
        end
        b.start_job(action_name, arguments)
    end
end
running?() click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 138
def running?
    async && async.running?
end
state() click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 158
def state
    if interface.reachable?
        if !async
            :reachable
        else
            async.state
        end
    else
        :unreachable
    end
end
success?() click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 142
def success?
    async && async.success?
end
terminated?() click to toggle source

If at least one job ran and is terminated

# File lib/roby/interface/async/action_monitor.rb, line 38
def terminated?
    async && async.terminated?
end
unreachable!() click to toggle source
# File lib/roby/interface/async/action_monitor.rb, line 170
def unreachable!
    @async = nil
    run_hook :on_progress
end