Module: ICanDaemonize::ClassMethods
- Defined in:
- lib/i_can_daemonize.rb
Instance Method Summary collapse
- #after(&block) ⇒ Object
- #arg(*args, &block) ⇒ Object
- #before(&block) ⇒ Object
- #callback!(callback) ⇒ Object
- #callbacks ⇒ Object
- #config {|@@config| ... } ⇒ Object
-
#daemonize(opts = {}, &block) ⇒ Object
options may include:.
- #die_if(method = nil, &block) ⇒ Object
- #exit_if(method = nil, &block) ⇒ Object
- #extra_args ⇒ Object
- #initialize_options ⇒ Object
- #options ⇒ Object
- #parse_options ⇒ Object
- #sig(signal, &block) ⇒ Object
Instance Method Details
#after(&block) ⇒ Object
113 114 115 |
# File 'lib/i_can_daemonize.rb', line 113 def after(&block) callbacks[:after] = block end |
#arg(*args, &block) ⇒ Object
89 90 91 |
# File 'lib/i_can_daemonize.rb', line 89 def arg(*args, &block) self.extra_args << [args, block] end |
#before(&block) ⇒ Object
109 110 111 |
# File 'lib/i_can_daemonize.rb', line 109 def before(&block) callbacks[:before] = block end |
#callback!(callback) ⇒ Object
129 130 131 |
# File 'lib/i_can_daemonize.rb', line 129 def callback!(callback) callbacks[callback].call if callbacks[callback] end |
#callbacks ⇒ Object
97 98 99 |
# File 'lib/i_can_daemonize.rb', line 97 def callbacks @callbacks ||= {} end |
#config {|@@config| ... } ⇒ Object
105 106 107 |
# File 'lib/i_can_daemonize.rb', line 105 def config yield @@config end |
#daemonize(opts = {}, &block) ⇒ Object
options may include:
:loop_every Fixnum (DEFAULT 0)
How many seconds to sleep between calls to your block
:timeout Fixnum (DEFAULT 0)
Timeout in if block does not execute withing passed number of seconds
:die_on_timeout BOOL (DEFAULT False)
Should the daemon continue running if a block times out, or just run the block again
:ontop BOOL (DEFAULT False)
Do not daemonize. Run in current process
:before BLOCK
Run this block after daemonizing but before begining the daemonize loop.
You can also define the before block by putting a before do/end block in your class.
:after BLOCK
Run this block before program exists.
You can also define the after block by putting an after do/end block in your class.
:die_if BLOCK
Run this check after each iteration of the loop. If the block returns true, throw a DieTime exception and exit
You can also define the after block by putting an die_if do/end block in your class.
:exit_if BLOCK
Run this check after each iteration of the loop. If the block returns true, exit gracefully
You can also define the after block by putting an exit_if do/end block in your class.
:log_prefix BOOL (DEFAULT true)
Prefix log file entries with PID and
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/i_can_daemonize.rb', line 165 def daemonize(opts={}, &block) return unless ok_to_start? .merge!(opts) puts "Starting #{instances_to_start} #{script_name} #{pluralize('instance', instances_to_start)}..." puts "Logging to: #{log_file}" unless ontop? unless ontop? instances_to_start.times do safefork do open(pid_file, 'a+') {|f| f << Process.pid << "\n"} at_exit { remove_pid! } trap('TERM') { callback!(:sig_term) ; self.running = false } trap('INT') { callback!(:sig_int) ; Process.kill('TERM', $$) } trap('HUP') { callback!(:sig_hup) ; restart_self } sess_id = Process.setsid reopen_filehandes begin at_exit { callback!(:after) } callback!(:before) run_block(&block) rescue SystemExit rescue Exception => e $stdout.puts "Something bad happened #{e.inspect} #{e.backtrace.join("\n")}" end end end else begin callback!(:before) run_block(&block) rescue SystemExit, Interrupt callback!(:after) end end end |
#die_if(method = nil, &block) ⇒ Object
121 122 123 |
# File 'lib/i_can_daemonize.rb', line 121 def die_if(method=nil,&block) [:die_if] = method || block end |
#exit_if(method = nil, &block) ⇒ Object
125 126 127 |
# File 'lib/i_can_daemonize.rb', line 125 def exit_if(method=nil,&block) [:exit_if] = method || block end |
#extra_args ⇒ Object
93 94 95 |
# File 'lib/i_can_daemonize.rb', line 93 def extra_args @extra_args ||= [] end |
#initialize_options ⇒ Object
29 30 31 32 33 |
# File 'lib/i_can_daemonize.rb', line 29 def @@config = Config.new @@config.script_path = File.(File.dirname($0)) $0 = script_name end |
#options ⇒ Object
101 102 103 |
# File 'lib/i_can_daemonize.rb', line 101 def ||= {} end |
#parse_options ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/i_can_daemonize.rb', line 35 def opts = OptionParser.new do |opt| opt. = "Usage: #{script_name} [options] [start|stop]" opt.on_tail('-h', '--help', 'Show this message') do puts opt exit end opt.on('--loop-every=SECONDS', 'How long to sleep between each loop') do |value| [:loop_every] = value end opt.on('-t', '--ontop', 'Stay on top (does not daemonize)') do [:ontop] = true end opt.on('--instances=NUM', 'Allow multiple instances to run simultaneously? 0 for infinite. default: 1') do |value| self.instances = value.to_i end opt.on('--log-file=LOGFILE', 'Logfile to log to') do |value| [:log_file] = File.(value) end opt.on('--pid-file=PIDFILE', 'Location of pidfile') do |value| [:pid_file] = File.(value) end opt.on('--no-log-prefix', 'Do not prefix PID and date/time in log file.') do [:log_prefix] = false end end extra_args.each do |arg| opts.on(*arg.first) do |value| arg.last.call(value) if arg.last end end opts.parse! if ARGV.include?('stop') stop_daemons elsif ARGV.include?('restart') restart_daemons elsif ARGV.include?('start') or ontop? self.running = true self.restarted = true if ARGV.include?('HUP') else puts opts.help end end |
#sig(signal, &block) ⇒ Object
117 118 119 |
# File 'lib/i_can_daemonize.rb', line 117 def sig(signal, &block) callbacks["sig_#{signal}".to_sym] = block end |