mikehale-daemons 1.0.12.1

Sign up to get free protection for your applications and to get access to all the features.
data/TODO ADDED
@@ -0,0 +1,6 @@
1
+ * write the README (2005-02-07) *DONE*
2
+ * write some real tests (2005-02-08)
3
+ * document the new options (2005-03-14) *DONE*
4
+ * start/stop with --force options (2005-04-05)
5
+ * option to give some console output on start/stop commands (2005-04-05)
6
+
@@ -0,0 +1,49 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "daemons"
3
+ s.version = "1.0.12.1"
4
+ s.date = "2009-02-24"
5
+ s.summary = "A toolkit to convert your script to a controllable daemon (with Chris Kline's fix)"
6
+ s.email = "mikehale@gmail.com"
7
+ s.homepage = "http://github.com/mikehale/daemons"
8
+ s.description =<<EOF
9
+ This is Daemons 1.0.10 with the addition of Chris Kline's fix from http://blog.rapleaf.com/dev/?p=19
10
+
11
+ Includes ability to change the process uid/gid. Also logdir can be specified seperate from piddir.
12
+
13
+ Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be run as a daemon and to be controlled by simple start/stop/restart commands.
14
+
15
+ If you want, you can also use daemons to run blocks of ruby code in a daemon process and to control these processes from the main application.
16
+
17
+ Besides this basic functionality, daemons offers many advanced features like exception backtracing and logging (in case your ruby script crashes) and monitoring and automatic restarting of your processes if they crash.
18
+
19
+ Daemons includes the daemonize.rb script written by Travis Whitton to do the daemonization process.
20
+ EOF
21
+ s.has_rdoc = false
22
+ s.authors = [
23
+ "Michael Hale",
24
+ "Thomas Uehlinger",
25
+ "Travis Whitton",
26
+ "Chris Kline"
27
+ ]
28
+ s.files = [
29
+ "daemons.gemspec",
30
+ "lib/daemons/application.rb",
31
+ "lib/daemons/application_group.rb",
32
+ "lib/daemons/cmdline.rb",
33
+ "lib/daemons/controller.rb",
34
+ "lib/daemons/daemonize.rb",
35
+ "lib/daemons/exceptions.rb",
36
+ "lib/daemons/monitor.rb",
37
+ "lib/daemons/pid.rb",
38
+ "lib/daemons/pidfile.rb",
39
+ "lib/daemons/pidmem.rb",
40
+ "lib/daemons/etc_extension.rb",
41
+ "lib/daemons/change_privilege.rb",
42
+ "lib/daemons.rb",
43
+ "LICENSE",
44
+ "Rakefile",
45
+ "README",
46
+ "Releases",
47
+ "TODO"
48
+ ]
49
+ end
@@ -0,0 +1,284 @@
1
+ require 'optparse'
2
+ require 'optparse/time'
3
+
4
+
5
+ require 'daemons/pidfile'
6
+ require 'daemons/cmdline'
7
+ require 'daemons/exceptions'
8
+ require 'daemons/monitor'
9
+
10
+
11
+ require 'daemons/application'
12
+ require 'daemons/application_group'
13
+ require 'daemons/controller'
14
+
15
+ require 'timeout'
16
+
17
+ # All functions and classes that Daemons provides reside in this module.
18
+ #
19
+ # Daemons is normally invoked by one of the following four ways:
20
+ #
21
+ # 1. <tt>Daemons.run(script, options)</tt>:
22
+ # This is used in wrapper-scripts that are supposed to control other ruby scripts or
23
+ # external applications. Control is completely passed to the daemons library.
24
+ # Such wrapper script need to be invoked with command line options like 'start' or 'stop'
25
+ # to do anything useful.
26
+ #
27
+ # 2. <tt>Daemons.run_proc(app_name, options) { (...) }</tt>:
28
+ # This is used in wrapper-scripts that are supposed to control a proc.
29
+ # Control is completely passed to the daemons library.
30
+ # Such wrapper script need to be invoked with command line options like 'start' or 'stop'
31
+ # to do anything useful.
32
+ #
33
+ # 3. <tt>Daemons.call(options) { block }</tt>:
34
+ # Execute the block in a new daemon. <tt>Daemons.call</tt> will return immediately
35
+ # after spawning the daemon with the new Application object as a return value.
36
+ #
37
+ # 4. <tt>Daemons.daemonize(options)</tt>:
38
+ # Daemonize the currently runnig process, i.e. the calling process will become a daemon.
39
+ #
40
+ # == What does daemons internally do with my daemons?
41
+ # *or*:: why do my daemons crash when they try to open a file?
42
+ # *or*:: why can I not see any output from the daemon on the console (when using for example +puts+)?
43
+ #
44
+ # From a technical aspect of view, daemons does the following when creating a daemon:
45
+ #
46
+ # 1. Forks a child (and exits the parent process, if needed)
47
+ # 2. Becomes a session leader (which detaches the program from
48
+ # the controlling terminal).
49
+ # 3. Forks another child process and exits first child. This prevents
50
+ # the potential of acquiring a controlling terminal.
51
+ # 4. Changes the current working directory to "/".
52
+ # 5. Clears the file creation mask (sets +umask+ to 0000).
53
+ # 6. Closes file descriptors (reopens +STDOUT+ and +STDERR+ to point to a logfile if
54
+ # possible).
55
+ #
56
+ # So what does this mean for your daemons:
57
+ # - the current directory is '/'
58
+ # - you cannot receive any input from the console (for example no +gets+)
59
+ # - you cannot output anything from the daemons with +puts+/+print+ unless a logfile is used
60
+ #
61
+ # == How do PidFiles work? Where are they stored?
62
+ #
63
+ # Also, you are maybe interested in reading the documentation for the class PidFile.
64
+ # There you can find out about how Daemons works internally and how and where the so
65
+ # called <i>PidFiles</i> are stored.
66
+ #
67
+ module Daemons
68
+
69
+ VERSION = "1.0.12.1"
70
+
71
+ require 'daemons/daemonize'
72
+
73
+
74
+ # Passes control to Daemons.
75
+ # This is used in wrapper-scripts that are supposed to control other ruby scripts or
76
+ # external applications. Control is completely passed to the daemons library.
77
+ # Such wrapper script should be invoked with command line options like 'start' or 'stop'
78
+ # to do anything useful.
79
+ #
80
+ # +script+:: This is the path to the script that should be run as a daemon.
81
+ # Please note that Daemons runs this script with <tt>load <script></tt>.
82
+ # Also note that Daemons cannot detect the directory in which the controlling
83
+ # script resides, so this has to be either an absolute path or you have to run
84
+ # the controlling script from the appropriate directory.
85
+ #
86
+ # +options+:: A hash that may contain one or more of the options listed below
87
+ #
88
+ # === Options:
89
+ # <tt>:app_name</tt>:: The name of the application. This will be
90
+ # used to contruct the name of the pid files
91
+ # and log files. Defaults to the basename of
92
+ # the script.
93
+ # <tt>:ARGV</tt>:: An array of strings containing parameters and switches for Daemons.
94
+ # This includes both parameters for Daemons itself and the controlled scripted.
95
+ # These are assumed to be separated by an array element '--', .e.g.
96
+ # ['start', 'f', '--', 'param1_for_script', 'param2_for_script'].
97
+ # If not given, ARGV (the parameters given to the Ruby process) will be used.
98
+ # <tt>:dir_mode</tt>:: Either <tt>:script</tt> (the directory for writing the pid files to
99
+ # given by <tt>:dir</tt> is interpreted relative
100
+ # to the script location given by +script+) or <tt>:normal</tt> (the directory given by
101
+ # <tt>:dir</tt> is interpreted as a (absolute or relative) path) or <tt>:system</tt>
102
+ # (<tt>/var/run</tt> is used as the pid file directory)
103
+ #
104
+ # <tt>:dir</tt>:: Used in combination with <tt>:dir_mode</tt> (description above)
105
+ # <tt>:multiple</tt>:: Specifies whether multiple instances of the same script are allowed to run at the
106
+ # same time
107
+ # <tt>:ontop</tt>:: When given (i.e. set to true), stay on top, i.e. do not daemonize the application
108
+ # (but the pid-file and other things are written as usual)
109
+ # <tt>:mode</tt>:: <tt>:load</tt> Load the script with <tt>Kernel.load</tt>;
110
+ # <tt>:exec</tt> Execute the script file with <tt>Kernel.exec</tt>
111
+ # <tt>:backtrace</tt>:: Write a backtrace of the last exceptions to the file '[app_name].log' in the
112
+ # pid-file directory if the application exits due to an uncaught exception
113
+ # <tt>:monitor</tt>:: Monitor the programs and restart crashed instances
114
+ # <tt>:log_output</tt>:: When given (i.e. set to true), redirect both STDOUT and STDERR to a logfile named '[app_name].output' in the pid-file directory
115
+ # <tt>:keep_pid_files</tt>:: When given do not delete lingering pid-files (files for which the process is no longer running).
116
+ # <tt>:hard_exit</tt>:: When given use exit! to end a daemons instead of exit (this will for example
117
+ # not call at_exit handlers).
118
+ # -----
119
+ #
120
+ # === Example:
121
+ # options = {
122
+ # :app_name => "my_app",
123
+ # :ARGV => ['start', '-f', '--', 'param_for_myscript']
124
+ # :dir_mode => :script,
125
+ # :dir => 'pids',
126
+ # :multiple => true,
127
+ # :ontop => true,
128
+ # :mode => :exec,
129
+ # :backtrace => true,
130
+ # :monitor => true
131
+ # }
132
+ #
133
+ # Daemons.run(File.join(File.dirname(__FILE__), 'myscript.rb'), options)
134
+ #
135
+ def run(script, options = {})
136
+ options[:script] = script
137
+ @controller = Controller.new(options, options[:ARGV] || ARGV)
138
+
139
+ @controller.catch_exceptions {
140
+ @controller.run
141
+ }
142
+
143
+ # I don't think anybody will ever use @group, as this location should not be reached under non-error conditions
144
+ @group = @controller.group
145
+ end
146
+ module_function :run
147
+
148
+
149
+ # Passes control to Daemons.
150
+ # This function does the same as Daemons.run except that not a script but a proc
151
+ # will be run as a daemon while this script provides command line options like 'start' or 'stop'
152
+ # and the whole pid-file management to control the proc.
153
+ #
154
+ # +app_name+:: The name of the application. This will be
155
+ # used to contruct the name of the pid files
156
+ # and log files. Defaults to the basename of
157
+ # the script.
158
+ #
159
+ # +options+:: A hash that may contain one or more of the options listed in the documentation for Daemons.run
160
+ #
161
+ # A block must be given to this function. The block will be used as the :proc entry in the options hash.
162
+ # -----
163
+ #
164
+ # === Example:
165
+ #
166
+ # Daemons.run_proc('myproc.rb') do
167
+ # loop do
168
+ # accept_connection()
169
+ # read_request()
170
+ # send_response()
171
+ # close_connection()
172
+ # end
173
+ # end
174
+ #
175
+ def run_proc(app_name, options = {}, &block)
176
+ options[:app_name] = app_name
177
+ options[:mode] = :proc
178
+ options[:proc] = block
179
+
180
+ # we do not have a script location so the the :script :dir_mode cannot be used, change it to :normal
181
+ if [nil, :script].include? options[:dir_mode]
182
+ options[:dir_mode] = :normal
183
+ options[:dir] = File.expand_path('.')
184
+ end
185
+
186
+ @controller = Controller.new(options, options[:ARGV] || ARGV)
187
+
188
+ @controller.catch_exceptions {
189
+ @controller.run
190
+ }
191
+
192
+ # I don't think anybody will ever use @group, as this location should not be reached under non-error conditions
193
+ @group = @controller.group
194
+ end
195
+ module_function :run_proc
196
+
197
+
198
+ # Execute the block in a new daemon. <tt>Daemons.call</tt> will return immediately
199
+ # after spawning the daemon with the new Application object as a return value.
200
+ #
201
+ # +options+:: A hash that may contain one or more of the options listed below
202
+ #
203
+ # +block+:: The block to call in the daemon.
204
+ #
205
+ # === Options:
206
+ # <tt>:multiple</tt>:: Specifies whether multiple instances of the same script are allowed to run at the
207
+ # same time
208
+ # <tt>:ontop</tt>:: When given, stay on top, i.e. do not daemonize the application
209
+ # <tt>:backtrace</tt>:: Write a backtrace of the last exceptions to the file '[app_name].log' in the
210
+ # pid-file directory if the application exits due to an uncaught exception
211
+ # -----
212
+ #
213
+ # === Example:
214
+ # options = {
215
+ # :backtrace => true,
216
+ # :monitor => true,
217
+ # :ontop => true
218
+ # }
219
+ #
220
+ # Daemons.call(options) begin
221
+ # # Server loop:
222
+ # loop {
223
+ # conn = accept_conn()
224
+ # serve(conn)
225
+ # }
226
+ # end
227
+ #
228
+ def call(options = {}, &block)
229
+ unless block_given?
230
+ raise "Daemons.call: no block given"
231
+ end
232
+
233
+ options[:proc] = block
234
+ options[:mode] = :proc
235
+
236
+ @group ||= ApplicationGroup.new('proc', options)
237
+
238
+ new_app = @group.new_application(options)
239
+ new_app.start
240
+
241
+ return new_app
242
+ end
243
+ module_function :call
244
+
245
+
246
+ # Daemonize the currently runnig process, i.e. the calling process will become a daemon.
247
+ #
248
+ # +options+:: A hash that may contain one or more of the options listed below
249
+ #
250
+ # === Options:
251
+ # <tt>:ontop</tt>:: When given, stay on top, i.e. do not daemonize the application
252
+ # <tt>:backtrace</tt>:: Write a backtrace of the last exceptions to the file '[app_name].log' in the
253
+ # pid-file directory if the application exits due to an uncaught exception
254
+ # -----
255
+ #
256
+ # === Example:
257
+ # options = {
258
+ # :backtrace => true,
259
+ # :ontop => true
260
+ # }
261
+ #
262
+ # Daemons.daemonize(options)
263
+ #
264
+ # # Server loop:
265
+ # loop {
266
+ # conn = accept_conn()
267
+ # serve(conn)
268
+ # }
269
+ #
270
+ def daemonize(options = {})
271
+ @group ||= ApplicationGroup.new('self', options)
272
+
273
+ @group.new_application(:mode => :none).start
274
+ end
275
+ module_function :daemonize
276
+
277
+ # Return the internal ApplicationGroup instance.
278
+ def group; @group; end
279
+ module_function :group
280
+
281
+ # Return the internal Controller instance.
282
+ def controller; @controller; end
283
+ module_function :controller
284
+ end
@@ -0,0 +1,387 @@
1
+ require 'daemons/pidfile'
2
+ require 'daemons/pidmem'
3
+ require 'daemons/change_privilege'
4
+
5
+ module Daemons
6
+
7
+ class Application
8
+
9
+ attr_accessor :app_argv
10
+ attr_accessor :controller_argv
11
+
12
+ # the Pid instance belonging to this application
13
+ attr_reader :pid
14
+
15
+ # the ApplicationGroup the application belongs to
16
+ attr_reader :group
17
+
18
+ # my private options
19
+ attr_reader :options
20
+
21
+
22
+ SIGNAL = (RUBY_PLATFORM =~ /win32/ ? 'KILL' : 'TERM')
23
+
24
+
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 @options[:no_pidfiles]
34
+ @pid = PidMem.new
35
+ elsif dir = pidfile_dir
36
+ @pid = PidFile.new(dir, @group.app_name, @group.multiple)
37
+ else
38
+ @pid = PidMem.new
39
+ end
40
+ end
41
+ change_privilege
42
+ end
43
+
44
+ def change_privilege
45
+ user = options[:user]
46
+ group = options[:group]
47
+ CurrentProcess.change_privilege(user, group) if user
48
+ end
49
+
50
+ def script
51
+ @script || @group.script
52
+ end
53
+
54
+ def pidfile_dir
55
+ Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script)
56
+ end
57
+
58
+ def logdir
59
+ logdir = options[:log_dir]
60
+ unless logdir
61
+ logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
62
+ end
63
+ logdir
64
+ end
65
+
66
+ def output_logfile
67
+ (options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.output') : nil
68
+ end
69
+
70
+ def logfile
71
+ logdir ? File.join(logdir, @group.app_name + '.log') : nil
72
+ end
73
+
74
+ # this function is only used to daemonize the currently running process (Daemons.daemonize)
75
+ def start_none
76
+ unless options[:ontop]
77
+ Daemonize.daemonize(nil, @group.app_name) #(logfile)
78
+ else
79
+ Daemonize.simulate
80
+ end
81
+
82
+ @pid.pid = Process.pid
83
+
84
+
85
+ # We need this to remove the pid-file if the applications exits by itself.
86
+ # Note that <tt>at_text</tt> will only be run if the applications exits by calling
87
+ # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
88
+ # in your application!
89
+ #
90
+ at_exit {
91
+ begin; @pid.cleanup; rescue ::Exception; end
92
+
93
+ # If the option <tt>:backtrace</tt> is used and the application did exit by itself
94
+ # create a exception log.
95
+ if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
96
+ begin; exception_log(); rescue ::Exception; end
97
+ end
98
+
99
+ }
100
+
101
+ # This part is needed to remove the pid-file if the application is killed by
102
+ # daemons or manually by the user.
103
+ # Note that the applications is not supposed to overwrite the signal handler for
104
+ # 'TERM'.
105
+ #
106
+ trap(SIGNAL) {
107
+ begin; @pid.cleanup; rescue ::Exception; end
108
+ $daemons_sigterm = true
109
+
110
+ if options[:hard_exit]
111
+ exit!
112
+ else
113
+ exit
114
+ end
115
+ }
116
+ end
117
+
118
+ def start_exec
119
+ if options[:backtrace]
120
+ puts "option :backtrace is not supported with :mode => :exec, ignoring"
121
+ end
122
+
123
+ unless options[:ontop]
124
+ Daemonize.daemonize(output_logfile, @group.app_name)
125
+ else
126
+ Daemonize.simulate(output_logfile)
127
+ end
128
+
129
+ # note that we cannot remove the pid file if we run in :ontop mode (i.e. 'ruby ctrl_exec.rb run')
130
+ @pid.pid = Process.pid
131
+
132
+ ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
133
+ # haven't tested yet if this is really passed to the exec'd process...
134
+
135
+
136
+
137
+ Kernel.exec(script(), *(@app_argv || []))
138
+ #Kernel.exec(script(), *ARGV)
139
+ end
140
+
141
+ def start_load
142
+ unless options[:ontop]
143
+ Daemonize.daemonize(output_logfile, @group.app_name)
144
+ else
145
+ Daemonize.simulate(output_logfile)
146
+ end
147
+
148
+ @pid.pid = Process.pid
149
+
150
+
151
+ # We need this to remove the pid-file if the applications exits by itself.
152
+ # Note that <tt>at_text</tt> will only be run if the applications exits by calling
153
+ # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
154
+ # in your application!
155
+ #
156
+ at_exit {
157
+ begin; @pid.cleanup; rescue ::Exception; end
158
+
159
+ # If the option <tt>:backtrace</tt> is used and the application did exit by itself
160
+ # create a exception log.
161
+ if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
162
+ begin; exception_log(); rescue ::Exception; end
163
+ end
164
+
165
+ }
166
+
167
+ # This part is needed to remove the pid-file if the application is killed by
168
+ # daemons or manually by the user.
169
+ # Note that the applications is not supposed to overwrite the signal handler for
170
+ # 'TERM'.
171
+ #
172
+ trap(SIGNAL) {
173
+ begin; @pid.cleanup; rescue ::Exception; end
174
+ $daemons_sigterm = true
175
+
176
+ if options[:hard_exit]
177
+ exit!
178
+ else
179
+ exit
180
+ end
181
+ }
182
+
183
+ # Now we really start the script...
184
+ $DAEMONS_ARGV = @controller_argv
185
+ ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
186
+
187
+ ARGV.clear
188
+ ARGV.concat @app_argv if @app_argv
189
+
190
+ # TODO: begin - rescue - end around this and exception logging
191
+ load script()
192
+ end
193
+
194
+ def start_proc
195
+ return unless p = options[:proc]
196
+
197
+ myproc = proc do
198
+ # We need this to remove the pid-file if the applications exits by itself.
199
+ # Note that <tt>at_text</tt> will only be run if the applications exits by calling
200
+ # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
201
+ # in your application!
202
+ #
203
+ at_exit {
204
+ begin; @pid.cleanup; rescue ::Exception; end
205
+
206
+ # If the option <tt>:backtrace</tt> is used and the application did exit by itself
207
+ # create a exception log.
208
+ if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
209
+ begin; exception_log(); rescue ::Exception; end
210
+ end
211
+
212
+ }
213
+
214
+ # This part is needed to remove the pid-file if the application is killed by
215
+ # daemons or manually by the user.
216
+ # Note that the applications is not supposed to overwrite the signal handler for
217
+ # 'TERM'.
218
+ #
219
+ trap(SIGNAL) {
220
+ begin; @pid.cleanup; rescue ::Exception; end
221
+ $daemons_sigterm = true
222
+
223
+ if options[:hard_exit]
224
+ exit!
225
+ else
226
+ exit
227
+ end
228
+ }
229
+
230
+ p.call()
231
+ end
232
+
233
+ unless options[:ontop]
234
+ @pid.pid = Daemonize.call_as_daemon(myproc, output_logfile, @group.app_name)
235
+ else
236
+ Daemonize.simulate(output_logfile)
237
+
238
+ @pid.pid = Process.pid
239
+
240
+ myproc.call
241
+
242
+ # why did we use this??
243
+ # Thread.new(&options[:proc])
244
+
245
+ # why did we use the code below??
246
+ # unless pid = Process.fork
247
+ # @pid.pid = pid
248
+ # Daemonize.simulate(logfile)
249
+ # options[:proc].call
250
+ # exit
251
+ # else
252
+ # Process.detach(@pid.pid)
253
+ # end
254
+ end
255
+ end
256
+
257
+
258
+ def start
259
+ @group.create_monitor(@group.applications[0] || self) unless options[:ontop] # we don't monitor applications in the foreground
260
+
261
+ case options[:mode]
262
+ when :none
263
+ # this is only used to daemonize the currently running process
264
+ start_none
265
+ when :exec
266
+ start_exec
267
+ when :load
268
+ start_load
269
+ when :proc
270
+ start_proc
271
+ else
272
+ start_load
273
+ end
274
+ end
275
+
276
+ # def run
277
+ # if @group.controller.options[:exec]
278
+ # run_via_exec()
279
+ # else
280
+ # run_via_load()
281
+ # end
282
+ # end
283
+ #
284
+ # def run_via_exec
285
+ #
286
+ # end
287
+ #
288
+ # def run_via_load
289
+ #
290
+ # end
291
+
292
+
293
+ # This is a nice little function for debugging purposes:
294
+ # In case a multi-threaded ruby script exits due to an uncaught exception
295
+ # it may be difficult to find out where the exception came from because
296
+ # one cannot catch exceptions that are thrown in threads other than the main
297
+ # thread.
298
+ #
299
+ # This function searches for all exceptions in memory and outputs them to STDERR
300
+ # (if it is connected) and to a log file in the pid-file directory.
301
+ #
302
+ def exception_log
303
+ return unless logfile
304
+
305
+ require 'logger'
306
+
307
+ l_file = Logger.new(logfile)
308
+
309
+ # the code below finds the last exception
310
+ e = nil
311
+
312
+ ObjectSpace.each_object {|o|
313
+ if ::Exception === o
314
+ e = o
315
+ end
316
+ }
317
+
318
+ 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 ***"
319
+ l_file.error e
320
+
321
+ 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 ***"
322
+
323
+ # this code logs every exception found in memory
324
+ ObjectSpace.each_object {|o|
325
+ if ::Exception === o
326
+ l_file.error o
327
+ end
328
+ }
329
+
330
+ l_file.close
331
+ end
332
+
333
+
334
+ def stop
335
+ if options[:force] and not running?
336
+ self.zap
337
+ return
338
+ end
339
+
340
+ # Catch errors when trying to kill a process that doesn't
341
+ # exist. This happens when the process quits and hasn't been
342
+ # restarted by the monitor yet. By catching the error, we allow the
343
+ # pid file clean-up to occur.
344
+ begin
345
+ Process.kill(SIGNAL, @pid.pid)
346
+ rescue Errno::ESRCH => e
347
+ puts "#{e} #{@pid.pid}"
348
+ puts "deleting pid-file."
349
+ end
350
+
351
+ # We try to remove the pid-files by ourselves, in case the application
352
+ # didn't clean it up.
353
+ begin; @pid.cleanup; rescue ::Exception; end
354
+
355
+ end
356
+
357
+ def zap
358
+ @pid.cleanup
359
+ end
360
+
361
+ def zap!
362
+ begin; @pid.cleanup; rescue ::Exception; end
363
+ end
364
+
365
+ def show_status
366
+ running = self.running?
367
+
368
+ 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 + ')' : ''}"
369
+ end
370
+
371
+ # This function implements a (probably too simle) method to detect
372
+ # whether the program with the pid found in the pid-file is still running.
373
+ # It just searches for the pid in the output of <tt>ps ax</tt>, which
374
+ # is probably not a good idea in some cases.
375
+ # Alternatives would be to use a direct access method the unix process control
376
+ # system.
377
+ #
378
+ def running?
379
+ if @pid.exist?
380
+ return Pid.running?(@pid.pid)
381
+ end
382
+
383
+ return false
384
+ end
385
+ end
386
+
387
+ end