# File lib/daemons/monitor.rb, line 8
8: def self.find(dir, app_name)
9: pid = PidFile.find_files(dir, app_name, false)[0]
10:
11: if pid
12: pid = PidFile.existing(pid)
13:
14: unless PidFile.running?(pid.pid)
15: begin; pid.cleanup; rescue ::Exception; end
16: return
17: end
18:
19: monitor = self.allocate
20:
21: monitor.instance_variable_set(:@pid, pid)
22:
23: return monitor
24: end
25:
26: return nil
27: end
# File lib/daemons/monitor.rb, line 30
30: def initialize(an_app)
31: @app = an_app
32: @app_name = an_app.group.app_name + '_monitor'
33:
34: if an_app.pidfile_dir
35: @pid = PidFile.new(an_app.pidfile_dir, @app_name, false)
36: else
37: @pid = PidMem.new
38: end
39: end
# File lib/daemons/monitor.rb, line 107
107: def start(applications)
108: return if applications.empty?
109:
110: if @pid.kind_of?(PidFile)
111: start_with_pidfile(applications)
112: else
113: start_without_pidfile(applications)
114: end
115: end
# File lib/daemons/monitor.rb, line 118
118: def stop
119: begin
120: pid = @pid.pid
121: Process.kill(Application::SIGNAL, pid)
122: Timeout::timeout(5) {
123: while Pid.running?(pid)
124: sleep(0.1)
125: end
126: }
127: rescue ::Exception => e
128: puts "#{e} #{pid}"
129: puts "deleting pid-file."
130: end
131:
132: # We try to remove the pid-files by ourselves, in case the application
133: # didn't clean it up.
134: begin; @pid.cleanup; rescue ::Exception; end
135: end
# File lib/daemons/monitor.rb, line 63
63: def start_with_pidfile(applications)
64: fork do
65: Daemonize.daemonize(nil, @app_name)
66:
67: begin
68: @pid.pid = Process.pid
69:
70: # at_exit {
71: # begin; @pid.cleanup; rescue ::Exception; end
72: # }
73:
74: # This part is needed to remove the pid-file if the application is killed by
75: # daemons or manually by the user.
76: # Note that the applications is not supposed to overwrite the signal handler for
77: # 'TERM'.
78: #
79: # trap('TERM') {
80: # begin; @pid.cleanup; rescue ::Exception; end
81: # exit
82: # }
83:
84: watch(applications)
85: rescue ::Exception => e
86: begin
87: File.open(@app.logfile, 'a') {|f|
88: f.puts Time.now
89: f.puts e
90: f.puts e.backtrace.inspect
91: }
92: ensure
93: begin; @pid.cleanup; rescue ::Exception; end
94: exit!
95: end
96: end
97: end
98: end
# File lib/daemons/monitor.rb, line 101
101: def start_without_pidfile(applications)
102: Thread.new { watch(applications) }
103: end
# File lib/daemons/monitor.rb, line 41
41: def watch(applications)
42: sleep(30)
43:
44: loop do
45: applications.each {|a|
46: sleep(10)
47:
48: unless a.running?
49: a.zap!
50:
51: Process.detach(fork { a.start })
52:
53: sleep(10)
54: end
55: }
56:
57: sleep(30)
58: end
59: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.