Module: FireAndForget::Launcher

Included in:
FireAndForget
Defined in:
lib/fire_and_forget/launcher.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)

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


122
123
124
125
126
127
128
# 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

Instance Method Details

#[](task_name) ⇒ Object

Gets the TaskDescription of a task

Parameters:

  • task_name (Symbol)

    the name of the task to get



42
43
44
# File 'lib/fire_and_forget/launcher.rb', line 42

def [](task_name)
  tasks[task_name]
end

#add_task(task_name, path_to_binary, niceness = 0, default_params = {}, env = {}) ⇒ Object

Registers a task and makes it available for easy launching using #fire

Parameters:

  • task_name (Symbol)

    the name for the task. This should be unique

  • path_to_binary (String)

    the path to the executable that should be run when this task is launched

  • niceness (Fixnum) (defaults to: 0)

    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

  • default_params (Hash) (defaults to: {})

    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

  • env (Hash) (defaults to: {})

    A Hash of values to add to the task’s ENV settings



27
28
29
# 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

#binary(task_name) ⇒ String

Returns the path to the binary for the given task

Parameters:

  • task_name (Symbol)

    the name of the task

Returns:

  • (String)

    the path of the task’s binary



35
36
37
# File 'lib/fire_and_forget/launcher.rb', line 35

def binary(task_name)
  tasks[task_name].binary
end

#fire(task_name, params = {}) ⇒ Object

Launches the given task

Parameters:

  • task_name (Symbol)

    the name of the task to launch

  • params (Hash) (defaults to: {})

    parameters to pass to the executable



54
55
56
57
58
# 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

#get_pid(task_name) ⇒ Object Also known as: pid

Retrieve the PID of the running task given by task_name



87
88
89
90
# 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_status(task_name) ⇒ String

Get the status for the given task

Parameters:

  • task_name (Symbol)

    the name of the task

Returns:

  • (String)

    the current status of the task

See Also:



74
75
76
77
# File 'lib/fire_and_forget/launcher.rb', line 74

def get_status(task_name)
  command = Command::GetStatus.new(task_name)
  Client.run(command)
end

#int(task_name) ⇒ Object

Sends a running task the INT signal



99
100
101
# File 'lib/fire_and_forget/launcher.rb', line 99

def int(task_name)
  kill(task_name, "INT")
end

#kill(task_name, signal = "TERM") ⇒ Object

Sends a running task an arbitrary signal

Parameters:

  • task_name (Symbol)

    the name of the task to send the signal

  • signal (String) (defaults to: "TERM")

    the signal to send

See Also:

  • for a full list of signals available


109
110
111
112
# 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

#map_pid(task_name, pid) ⇒ Object Also known as: set_pid

Used by the Daemon module to set the correct PID for a given task



80
81
82
83
# 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

#set_status(task_name, status) ⇒ Object

Sets the status of the given task enabling simple interprocess communication through string messages

Parameters:

  • task_name (String)

    the name of the task to set the status for

  • status (#to_s)

    the setting for the given task’s status



64
65
66
67
# 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

#tasksObject



46
47
48
# File 'lib/fire_and_forget/launcher.rb', line 46

def tasks
  @tasks ||= {}
end

#term(task_name) ⇒ Object

Sends a running task the TERM signal



94
95
96
# File 'lib/fire_and_forget/launcher.rb', line 94

def term(task_name)
  kill(task_name, "TERM")
end