= process_control

* http://cyberconnect.biz/opensource

== DESCRIPTION:

Process Control is a wrapper to allow easy control for any external daemon type process. The
Process Control app is minimal, such that there should never be any errors and the process can
run in a daemon state yielding a consistant means for controlling a child process. The child
process being controlled is done so via signals, QUIT, CONT, HUP, USR1 and TERM (see SIGNALS
section).

== FEATURES

* Monitor interval: Sleep time between each cycle.
* Monitor exec: Child command to execute. Command should be a standalone daemon
that doesn't not exit.
* Monitor exec_live_cfg, optional config file to be passed to the exec attribute if you have more
than one config file. If your application being controlled only requires one config file put
the config parameter in the exec string itself.
* Monitor exec_maintenance_cfg config file to use when your application is in maintenance mode
* Monitor kill_signal: Signal to use when stopping the child process, defalting to TERM.
* Monitor kill_children: If the child process defined by exec launches additional processes,
kill all children of the exec process.
* Monitor usr1: User defined Proc to execute. This is usefull for external system monitoring.
For example, the Proc could create a tmp file, and when USR1 is sent to the process_control pid
that tmp file would be created. Now the external monotor has an easy means to ensure that the
process_control process is functioning properly.
* Monitors the process launched defined by exec, and if not found running, starts it.

== SIGNALS

Signals need to be sent to the process_control process and not the child spawned. Process Control in turn will perform actions based on the signal provided.

* QUIT: Kill the child process, and puts the monitoring of the child process in a halt state. If exec_live_cfg and exec_maintenance_cfg are set, swap the config file from live to maintenance and start the process. Multiple config files are handy when you have down time to a web site and you need to display a certain page to the user while you upgrade your application.
* CONT: Start the child process, and enables the monitor to restart the child process if found not running. If exec_live_cfg and exec_maintenance_cfg are set the config file is set to live before the application is started.
* HUP: Restart the child process
* TERM: Stop both the child process and process_control itself
* USR1: Execute the Proc code block defined in the config

== SYNOPSIS:

/usr/bin/process_control --config <full path to config file>

Example config file for a web agent running as a daemon, outside of the web framework itself. If
the code below was saved in a file named /tmp/start_cert.rb you would launch cert.rb via process_control by:

process_control --config /tmp/start_cert.rb

ProcessControl.run { |monitor|
monitor.interval = 10
monitor.exec = "ruby /cyber_platform/vendor/plugins/phased_layer_tunnel/agents/cert.rb"
monitor.kill_children = true
monitor.kill_signal='TERM'
monitor.usr1 = Proc.new {
status_f = "/share/status/cyber_platform_cert.rb"
begin
FileUtils.rm_f status_f if File.exists?(status_f)
rescue => e
end
begin
fd = File.open(status_f, 'w+')
fd.close
rescue => e
end
}
monitor.monitor # required last line, or child process will not be launched
}

Example for nginx hosting a rails application given live and maintenance configurations. When QUIT is sent to process_control the maintenance config file will be utilized so that users will still be able to access the site, but given an explanation there's maintenance. And when CONT is sent, toggle the config back to live, restoring the full functionality of the application:

ProcessControl.run { |monitor|
monitor.interval = 10
monitor.exec = "/usr/local/nginx/sbin/nginx -c"
monitor.exec_live_cfg = "/cyber_platform/config/nginx.conf"
monitor.exec_maintenance_cfg = "/cyber_platform/config/nginx.conf.maintenance"
monitor.kill_children = false
monitor.kill_signal='QUIT'
monitor.monitor # required last line, or child process will not be launched
}


== REQUIREMENTS:

Tested on GNU/Linux, but should work on other variants of Unix type OS's as well.

== INSTALL:

* sudo gem install process_control

== LICENSE:

GPLv3: http://www.gnu.org/licenses/gpl.html