Module: ICanDaemonize::ClassMethods

Defined in:
lib/i_can_daemonize.rb

Instance Method Summary collapse

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

#callbacksObject



97
98
99
# File 'lib/i_can_daemonize.rb', line 97

def callbacks
  @callbacks ||= {}
end

#config {|@@config| ... } ⇒ Object

Yields:



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 timestamp


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)
  parse_options
  return unless ok_to_start?

  options.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)
  options[: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)
  options[:exit_if] = method || block
end

#extra_argsObject



93
94
95
# File 'lib/i_can_daemonize.rb', line 93

def extra_args
  @extra_args ||= [] 
end

#initialize_optionsObject



29
30
31
32
33
# File 'lib/i_can_daemonize.rb', line 29

def initialize_options    
  @@config             = Config.new
  @@config.script_path = File.expand_path(File.dirname($0))
  $0                   = script_name
end

#optionsObject



101
102
103
# File 'lib/i_can_daemonize.rb', line 101

def options
  @options ||= {}
end

#parse_optionsObject



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 parse_options
  opts = OptionParser.new do |opt|
    opt.banner = "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|
      options[:loop_every] = value 
    end

    opt.on('-t', '--ontop', 'Stay on top (does not daemonize)') do
      options[: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|
      options[:log_file] = File.expand_path(value)
    end

    opt.on('--pid-file=PIDFILE', 'Location of pidfile') do |value|
      options[:pid_file] = File.expand_path(value)
    end

    opt.on('--no-log-prefix', 'Do not prefix PID and date/time in log file.') do
      options[: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