Froze rails gems
[depot.git] / vendor / rails / railties / lib / commands / process / spinner.rb
1 require 'optparse'
2
3 def daemonize #:nodoc:
4 exit if fork # Parent exits, child continues.
5 Process.setsid # Become session leader.
6 exit if fork # Zap session leader. See [1].
7 Dir.chdir "/" # Release old working directory.
8 File.umask 0000 # Ensure sensible umask. Adjust as needed.
9 STDIN.reopen "/dev/null" # Free file descriptors and
10 STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
11 STDERR.reopen STDOUT # STDOUT/ERR should better go to a logfile.
12 end
13
14 OPTIONS = {
15 :interval => 5.0,
16 :command => File.expand_path(RAILS_ROOT + '/script/process/spawner'),
17 :daemon => false
18 }
19
20 ARGV.options do |opts|
21 opts.banner = "Usage: spinner [options]"
22
23 opts.separator ""
24
25 opts.on <<-EOF
26 Description:
27 The spinner is a protection loop for the spawner, which will attempt to restart any FCGI processes
28 that might have been exited or outright crashed. It's a brute-force attempt that'll just try
29 to run the spawner every X number of seconds, so it does pose a light load on the server.
30
31 Examples:
32 spinner # attempts to run the spawner with default settings every second with output on the terminal
33 spinner -i 3 -d # only run the spawner every 3 seconds and detach from the terminal to become a daemon
34 spinner -c '/path/to/app/script/process/spawner -p 9000 -i 10' -d # using custom spawner
35 EOF
36
37 opts.on(" Options:")
38
39 opts.on("-c", "--command=path", String) { |v| OPTIONS[:command] = v }
40 opts.on("-i", "--interval=seconds", Float) { |v| OPTIONS[:interval] = v }
41 opts.on("-d", "--daemon") { |v| OPTIONS[:daemon] = v }
42
43 opts.separator ""
44
45 opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
46
47 opts.parse!
48 end
49
50 daemonize if OPTIONS[:daemon]
51
52 trap(OPTIONS[:daemon] ? "TERM" : "INT") { exit }
53
54 loop do
55 system(OPTIONS[:command])
56 sleep(OPTIONS[:interval])
57 end