module FireAndForget::Launcher
Public Instance Methods
Gets the TaskDescription
of a task
@param [Symbol] task_name the name of the task to get
# File lib/fire_and_forget/launcher.rb, line 42 def [](task_name) tasks[task_name] end
Registers a task and makes it available for easy launching using fire
@param [Symbol] task_name
the name for the task. This should be unique
@param [String] path_to_binary
the path to the executable that should be run when this task is launched
@param [Fixnum] niceness
the niceness value of the process >= 0. The higher this value the 'nicer' the launched process will be (a high nice value results in a low priority task). On UNIX systems the max, nicest, value is 20
@param [Hash] default_params
A Hash of parameters that should be passed to every invocation of the task. These will be converted to command line parameters { "setting" => "value", "output" => "destination"} gives the parameters --setting=value --output=destination @see FireAndForget::Utilities#to_arguments
@param [Hash] env
A Hash of values to add to the task's ENV settings
# File lib/fire_and_forget/launcher.rb, line 27 def add_task(task_name, path_to_binary, niceness=0, default_params={}, env={}) tasks[task_name] = TaskDescription.new(task_name, path_to_binary, niceness, default_params, env) end
Returns the path to the binary for the given task
@param [Symbol] task_name the name of the task @return [String] the path of the task’s binary
# File lib/fire_and_forget/launcher.rb, line 35 def binary(task_name) tasks[task_name].binary end
Launches the given task
@param [Symbol] task_name the name of the task to launch @param [Hash] params parameters to pass to the executable
# File lib/fire_and_forget/launcher.rb, line 54 def fire(task_name, params={}) task = tasks[task_name] command = Command::Fire.new(task, params) Client.run(command) end
Retrieve the PID of the running task given by task_name
# File lib/fire_and_forget/launcher.rb, line 87 def get_pid(task_name) command = Command::GetPid.new(task_name) Client.run(command) end
Get the status for the given task @see set_status
@param [Symbol] task_name the name of the task @return [String] the current status of the task
# File lib/fire_and_forget/launcher.rb, line 74 def get_status(task_name) command = Command::GetStatus.new(task_name) Client.run(command) end
Sends a running task the INT signal
# File lib/fire_and_forget/launcher.rb, line 99 def int(task_name) kill(task_name, "INT") end
Sends a running task an arbitrary signal
@param [Symbol] task_name the name of the task to send the signal @param [String] signal the signal to send
@see Signal#list for a full list of signals available
# File lib/fire_and_forget/launcher.rb, line 109 def kill(task_name, signal="TERM") command = Command::Kill.new(task_name, signal) Client.run(command) end
Used by the {FireAndForget::Daemon} module to set the correct PID for a given task
# File lib/fire_and_forget/launcher.rb, line 80 def map_pid(task_name, pid) command = Command::SetPid.new(task_name, pid) Client.run(command) end
Sets the status of the given task enabling simple interprocess communication through string messages
@param [String] task_name the name of the task to set the status for @param [#to_s] status the setting for the given task’s status
# File lib/fire_and_forget/launcher.rb, line 64 def set_status(task_name, status) command = Command::SetStatus.new(task_name, status) Client.run(command) end
# File lib/fire_and_forget/launcher.rb, line 46 def tasks @tasks ||= {} end
Sends a running task the TERM signal
# File lib/fire_and_forget/launcher.rb, line 94 def term(task_name) kill(task_name, "TERM") end
Protected Instance Methods
Catch method missing to enable launching of tasks by direct name e.g.
FireAndForget.add_task(:process_things, "/usr/bin/process")
launch this task:
FireAndForget.process_things
# File lib/fire_and_forget/launcher.rb, line 122 def method_missing(method, *args, &block) if tasks.key?(method) fire(method, *args, &block) else super end end