| Class | Daemons::Application |
| In: |
lib/daemons/application.rb
|
| Parent: | Object |
| SIGNAL | = | (RUBY_PLATFORM =~ /win32/ ? 'KILL' : 'TERM') |
| app_argv | [RW] | |
| controller_argv | [RW] | |
| group | [R] | the ApplicationGroup the application belongs to |
| options | [R] | my private options |
| pid | [R] | the Pid instance belonging to this application |
# File lib/daemons/application.rb, line 25
25: def initialize(group, add_options = {}, pid = nil)
26: @group = group
27: @options = group.options.dup
28: @options.update(add_options)
29:
30: @dir_mode = @dir = @script = nil
31:
32: unless @pid = pid
33: if dir = pidfile_dir
34: @pid = PidFile.new(dir, @group.app_name, @group.multiple)
35: else
36: @pid = PidMem.new
37: end
38: end
39: end
This is a nice little function for debugging purposes: In case a multi-threaded ruby script exits due to an uncaught exception it may be difficult to find out where the exception came from because one cannot catch exceptions that are thrown in threads other than the main thread.
This function searches for all exceptions in memory and outputs them to STDERR (if it is connected) and to a log file in the pid-file directory.
# File lib/daemons/application.rb, line 287
287: def exception_log
288: return unless logfile
289:
290: require 'logger'
291:
292: l_file = Logger.new(logfile)
293:
294: # the code below finds the last exception
295: e = nil
296:
297: ObjectSpace.each_object {|o|
298: if ::Exception === o
299: e = o
300: end
301: }
302:
303: l_file.info "*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***"
304: l_file.error e
305:
306: l_file.info "*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***"
307:
308: # this code logs every exception found in memory
309: ObjectSpace.each_object {|o|
310: if ::Exception === o
311: l_file.error o
312: end
313: }
314:
315: l_file.close
316: end
# File lib/daemons/application.rb, line 54
54: def logfile
55: logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
56: logdir ? File.join(logdir, @group.app_name + '.log') : nil
57: end
# File lib/daemons/application.rb, line 49
49: def output_logfile
50: logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
51: (options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.output') : nil
52: end
# File lib/daemons/application.rb, line 45
45: def pidfile_dir
46: Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script)
47: end
This function implements a (probably too simle) method to detect whether the program with the pid found in the pid-file is still running. It just searches for the pid in the output of ps ax, which is probably not a good idea in some cases. Alternatives would be to use a direct access method the unix process control system.
# File lib/daemons/application.rb, line 363
363: def running?
364: if @pid.exist?
365: return Pid.running?(@pid.pid)
366: end
367:
368: return false
369: end
# File lib/daemons/application.rb, line 350
350: def show_status
351: running = self.running?
352:
353: puts "#{self.group.app_name}: #{running ? '' : 'not '}running#{(running and @pid.exist?) ? ' [pid ' + @pid.pid.to_s + ']' : ''}#{(@pid.exist? and not running) ? ' (but pid-file exists: ' + @pid.pid.to_s + ')' : ''}"
354: end
# File lib/daemons/application.rb, line 243
243: def start
244: @group.create_monitor(@group.applications[0] || self) unless options[:ontop] # we don't monitor applications in the foreground
245:
246: case options[:mode]
247: when :none
248: # this is only used to daemonize the currently running process
249: start_none
250: when :exec
251: start_exec
252: when :load
253: start_load
254: when :proc
255: start_proc
256: else
257: start_load
258: end
259: end
# File lib/daemons/application.rb, line 103
103: def start_exec
104: if options[:backtrace]
105: puts "option :backtrace is not supported with :mode => :exec, ignoring"
106: end
107:
108: unless options[:ontop]
109: Daemonize.daemonize(output_logfile, @group.app_name)
110: else
111: Daemonize.simulate(output_logfile)
112: end
113:
114: # note that we cannot remove the pid file if we run in :ontop mode (i.e. 'ruby ctrl_exec.rb run')
115: @pid.pid = Process.pid
116:
117: ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
118: # haven't tested yet if this is really passed to the exec'd process...
119:
120:
121:
122: Kernel.exec(script(), *(@app_argv || []))
123: #Kernel.exec(script(), *ARGV)
124: end
# File lib/daemons/application.rb, line 126
126: def start_load
127: unless options[:ontop]
128: Daemonize.daemonize(output_logfile, @group.app_name)
129: else
130: Daemonize.simulate(output_logfile)
131: end
132:
133: @pid.pid = Process.pid
134:
135:
136: # We need this to remove the pid-file if the applications exits by itself.
137: # Note that <tt>at_text</tt> will only be run if the applications exits by calling
138: # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
139: # in your application!
140: #
141: at_exit {
142: begin; @pid.cleanup; rescue ::Exception; end
143:
144: # If the option <tt>:backtrace</tt> is used and the application did exit by itself
145: # create a exception log.
146: if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
147: begin; exception_log(); rescue ::Exception; end
148: end
149:
150: }
151:
152: # This part is needed to remove the pid-file if the application is killed by
153: # daemons or manually by the user.
154: # Note that the applications is not supposed to overwrite the signal handler for
155: # 'TERM'.
156: #
157: trap(SIGNAL) {
158: begin; @pid.cleanup; rescue ::Exception; end
159: $daemons_sigterm = true
160:
161: if options[:hard_exit]
162: exit!
163: else
164: exit
165: end
166: }
167:
168: # Now we really start the script...
169: $DAEMONS_ARGV = @controller_argv
170: ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
171:
172: ARGV.clear
173: ARGV.concat @app_argv if @app_argv
174:
175: # TODO: begin - rescue - end around this and exception logging
176: load script()
177: end
this function is only used to daemonize the currently running process (Daemons.daemonize)
# File lib/daemons/application.rb, line 60
60: def start_none
61: unless options[:ontop]
62: Daemonize.daemonize(nil, @group.app_name) #(logfile)
63: else
64: Daemonize.simulate
65: end
66:
67: @pid.pid = Process.pid
68:
69:
70: # We need this to remove the pid-file if the applications exits by itself.
71: # Note that <tt>at_text</tt> will only be run if the applications exits by calling
72: # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
73: # in your application!
74: #
75: at_exit {
76: begin; @pid.cleanup; rescue ::Exception; end
77:
78: # If the option <tt>:backtrace</tt> is used and the application did exit by itself
79: # create a exception log.
80: if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
81: begin; exception_log(); rescue ::Exception; end
82: end
83:
84: }
85:
86: # This part is needed to remove the pid-file if the application is killed by
87: # daemons or manually by the user.
88: # Note that the applications is not supposed to overwrite the signal handler for
89: # 'TERM'.
90: #
91: trap(SIGNAL) {
92: begin; @pid.cleanup; rescue ::Exception; end
93: $daemons_sigterm = true
94:
95: if options[:hard_exit]
96: exit!
97: else
98: exit
99: end
100: }
101: end
# File lib/daemons/application.rb, line 179
179: def start_proc
180: return unless p = options[:proc]
181:
182: myproc = proc do
183: # We need this to remove the pid-file if the applications exits by itself.
184: # Note that <tt>at_text</tt> will only be run if the applications exits by calling
185: # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
186: # in your application!
187: #
188: at_exit {
189: begin; @pid.cleanup; rescue ::Exception; end
190:
191: # If the option <tt>:backtrace</tt> is used and the application did exit by itself
192: # create a exception log.
193: if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
194: begin; exception_log(); rescue ::Exception; end
195: end
196:
197: }
198:
199: # This part is needed to remove the pid-file if the application is killed by
200: # daemons or manually by the user.
201: # Note that the applications is not supposed to overwrite the signal handler for
202: # 'TERM'.
203: #
204: trap(SIGNAL) {
205: begin; @pid.cleanup; rescue ::Exception; end
206: $daemons_sigterm = true
207:
208: if options[:hard_exit]
209: exit!
210: else
211: exit
212: end
213: }
214:
215: p.call()
216: end
217:
218: unless options[:ontop]
219: @pid.pid = Daemonize.call_as_daemon(myproc, output_logfile, @group.app_name)
220: else
221: Daemonize.simulate(output_logfile)
222:
223: @pid.pid = Process.pid
224:
225: myproc.call
226:
227: # why did we use this??
228: # Thread.new(&options[:proc])
229:
230: # why did we use the code below??
231: # unless pid = Process.fork
232: # @pid.pid = pid
233: # Daemonize.simulate(logfile)
234: # options[:proc].call
235: # exit
236: # else
237: # Process.detach(@pid.pid)
238: # end
239: end
240: end
# File lib/daemons/application.rb, line 319
319: def stop
320: if options[:force] and not running?
321: self.zap
322: return
323: end
324:
325: # Catch errors when trying to kill a process that doesn't
326: # exist. This happens when the process quits and hasn't been
327: # restarted by the monitor yet. By catching the error, we allow the
328: # pid file clean-up to occur.
329: begin
330: Process.kill(SIGNAL, @pid.pid)
331: rescue Errno::ESRCH => e
332: puts "#{e} #{@pid.pid}"
333: puts "deleting pid-file."
334: end
335:
336: # We try to remove the pid-files by ourselves, in case the application
337: # didn't clean it up.
338: begin; @pid.cleanup; rescue ::Exception; end
339:
340: end