sonian/sensu — pull request #146 by joemiller (2011-12-21 18:07:28+00:00)
Title: Daemonize - initial commit

@portertech - for further review. here is the initial commit for 'daemonize' patch.

Goals and requirements:
---------------------------------------
- Make the sensu components more initscript friendly for non-upstart systems, by:
- Providing a command line switch (-b) to fork/deamonize the processes. Default is false. When invoked, the process should fork itself safely into the background.
- Writing a pid file upon startup so that task management tools/scripts that rely on pid files have something to work with.
- Capture stdout/stderr and log it to a file so that we don't miss any important exceptions anymore. This is actually done whether -b is invoked or not, so that this data is captured even in the current init and upstart scripts.

Misc notes:
-----------------
- Although sensu already includes the Daemons gem (via thin), I could not use the Daemonize.daemonize() method as planned. Instead, I copied/modified it in Sensu.daemonize() in `lib/sensu.rb`.
- I modified the existing calls to `$0.split("\n").last` -> `File.basename($0)`. I believe this may be the more correct and portable way.

Concerns/Questions:
------------------------------
- Capturing stdout/stderr was a bit tricky. It is currently implemented in Config by redirecting STDOUT to the log_file, then opening Logger on STDOUT. This approach is working, but I'm not sure if it's the best approach.
- The key with sharing a logfile between STDOUT-reopen'ing and Logger is that STDOUT needs to be redirected to the logfile before Logger opens the file, otherwise logger will fail the first time a log write is attempted.
- Other possible approaches for stdout/err logging would be to log to a separate file altogether. maybe a `sensu-server_error.log` ?

TODOs:
------------
- Implement for sensu-dashboard as well.
- Update init.d scripts for CentOS to leverage -b and -p
- Update upstart init .confs to leverage -b and -p (maybe??)