seamusabshere-daemons 1.0.11.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,44 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "daemons"
3
+ s.version = "1.0.11.1"
4
+ s.date = "2009-01-07"
5
+ s.summary = "A toolkit to convert your script to a controllable daemon (with Chris Kline's fix)"
6
+ s.email = "seamus@abshere.net"
7
+ s.homepage = "http://github.com/seamusabshere/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
+ 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.
12
+
13
+ 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.
14
+
15
+ 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.
16
+
17
+ Daemons includes the daemonize.rb script written by Travis Whitton to do the daemonization process.
18
+ EOF
19
+ s.has_rdoc = false
20
+ s.authors = [
21
+ "Thomas Uehlinger",
22
+ "Travis Whitton",
23
+ "Chris Kline"
24
+ ]
25
+ s.files = [
26
+ "daemons.gemspec",
27
+ "lib/daemons/application.rb",
28
+ "lib/daemons/application_group.rb",
29
+ "lib/daemons/cmdline.rb",
30
+ "lib/daemons/controller.rb",
31
+ "lib/daemons/daemonize.rb",
32
+ "lib/daemons/exceptions.rb",
33
+ "lib/daemons/monitor.rb",
34
+ "lib/daemons/pid.rb",
35
+ "lib/daemons/pidfile.rb",
36
+ "lib/daemons/pidmem.rb",
37
+ "lib/daemons.rb",
38
+ "LICENSE",
39
+ "Rakefile",
40
+ "README",
41
+ "Releases",
42
+ "TODO"
43
+ ]
44
+ 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.10"
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,374 @@
1
+ require 'daemons/pidfile'
2
+ require 'daemons/pidmem'
3
+
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
+ end
42
+
43
+ def script
44
+ @script || @group.script
45
+ end
46
+
47
+ def pidfile_dir
48
+ Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script)
49
+ end
50
+
51
+ def output_logfile
52
+ logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
53
+ (options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.output') : nil
54
+ end
55
+
56
+ def logfile
57
+ logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
58
+ logdir ? File.join(logdir, @group.app_name + '.log') : nil
59
+ end
60
+
61
+ # this function is only used to daemonize the currently running process (Daemons.daemonize)
62
+ def start_none
63
+ unless options[:ontop]
64
+ Daemonize.daemonize(nil, @group.app_name) #(logfile)
65
+ else
66
+ Daemonize.simulate
67
+ end
68
+
69
+ @pid.pid = Process.pid
70
+
71
+
72
+ # We need this to remove the pid-file if the applications exits by itself.
73
+ # Note that <tt>at_text</tt> will only be run if the applications exits by calling
74
+ # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
75
+ # in your application!
76
+ #
77
+ at_exit {
78
+ begin; @pid.cleanup; rescue ::Exception; end
79
+
80
+ # If the option <tt>:backtrace</tt> is used and the application did exit by itself
81
+ # create a exception log.
82
+ if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
83
+ begin; exception_log(); rescue ::Exception; end
84
+ end
85
+
86
+ }
87
+
88
+ # This part is needed to remove the pid-file if the application is killed by
89
+ # daemons or manually by the user.
90
+ # Note that the applications is not supposed to overwrite the signal handler for
91
+ # 'TERM'.
92
+ #
93
+ trap(SIGNAL) {
94
+ begin; @pid.cleanup; rescue ::Exception; end
95
+ $daemons_sigterm = true
96
+
97
+ if options[:hard_exit]
98
+ exit!
99
+ else
100
+ exit
101
+ end
102
+ }
103
+ end
104
+
105
+ def start_exec
106
+ if options[:backtrace]
107
+ puts "option :backtrace is not supported with :mode => :exec, ignoring"
108
+ end
109
+
110
+ unless options[:ontop]
111
+ Daemonize.daemonize(output_logfile, @group.app_name)
112
+ else
113
+ Daemonize.simulate(output_logfile)
114
+ end
115
+
116
+ # note that we cannot remove the pid file if we run in :ontop mode (i.e. 'ruby ctrl_exec.rb run')
117
+ @pid.pid = Process.pid
118
+
119
+ ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
120
+ # haven't tested yet if this is really passed to the exec'd process...
121
+
122
+
123
+
124
+ Kernel.exec(script(), *(@app_argv || []))
125
+ #Kernel.exec(script(), *ARGV)
126
+ end
127
+
128
+ def start_load
129
+ unless options[:ontop]
130
+ Daemonize.daemonize(output_logfile, @group.app_name)
131
+ else
132
+ Daemonize.simulate(output_logfile)
133
+ end
134
+
135
+ @pid.pid = Process.pid
136
+
137
+
138
+ # We need this to remove the pid-file if the applications exits by itself.
139
+ # Note that <tt>at_text</tt> will only be run if the applications exits by calling
140
+ # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
141
+ # in your application!
142
+ #
143
+ at_exit {
144
+ begin; @pid.cleanup; rescue ::Exception; end
145
+
146
+ # If the option <tt>:backtrace</tt> is used and the application did exit by itself
147
+ # create a exception log.
148
+ if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
149
+ begin; exception_log(); rescue ::Exception; end
150
+ end
151
+
152
+ }
153
+
154
+ # This part is needed to remove the pid-file if the application is killed by
155
+ # daemons or manually by the user.
156
+ # Note that the applications is not supposed to overwrite the signal handler for
157
+ # 'TERM'.
158
+ #
159
+ trap(SIGNAL) {
160
+ begin; @pid.cleanup; rescue ::Exception; end
161
+ $daemons_sigterm = true
162
+
163
+ if options[:hard_exit]
164
+ exit!
165
+ else
166
+ exit
167
+ end
168
+ }
169
+
170
+ # Now we really start the script...
171
+ $DAEMONS_ARGV = @controller_argv
172
+ ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
173
+
174
+ ARGV.clear
175
+ ARGV.concat @app_argv if @app_argv
176
+
177
+ # TODO: begin - rescue - end around this and exception logging
178
+ load script()
179
+ end
180
+
181
+ def start_proc
182
+ return unless p = options[:proc]
183
+
184
+ myproc = proc do
185
+ # We need this to remove the pid-file if the applications exits by itself.
186
+ # Note that <tt>at_text</tt> will only be run if the applications exits by calling
187
+ # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
188
+ # in your application!
189
+ #
190
+ at_exit {
191
+ begin; @pid.cleanup; rescue ::Exception; end
192
+
193
+ # If the option <tt>:backtrace</tt> is used and the application did exit by itself
194
+ # create a exception log.
195
+ if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
196
+ begin; exception_log(); rescue ::Exception; end
197
+ end
198
+
199
+ }
200
+
201
+ # This part is needed to remove the pid-file if the application is killed by
202
+ # daemons or manually by the user.
203
+ # Note that the applications is not supposed to overwrite the signal handler for
204
+ # 'TERM'.
205
+ #
206
+ trap(SIGNAL) {
207
+ begin; @pid.cleanup; rescue ::Exception; end
208
+ $daemons_sigterm = true
209
+
210
+ if options[:hard_exit]
211
+ exit!
212
+ else
213
+ exit
214
+ end
215
+ }
216
+
217
+ p.call()
218
+ end
219
+
220
+ unless options[:ontop]
221
+ @pid.pid = Daemonize.call_as_daemon(myproc, output_logfile, @group.app_name)
222
+ else
223
+ Daemonize.simulate(output_logfile)
224
+
225
+ @pid.pid = Process.pid
226
+
227
+ myproc.call
228
+
229
+ # why did we use this??
230
+ # Thread.new(&options[:proc])
231
+
232
+ # why did we use the code below??
233
+ # unless pid = Process.fork
234
+ # @pid.pid = pid
235
+ # Daemonize.simulate(logfile)
236
+ # options[:proc].call
237
+ # exit
238
+ # else
239
+ # Process.detach(@pid.pid)
240
+ # end
241
+ end
242
+ end
243
+
244
+
245
+ def start
246
+ @group.create_monitor(@group.applications[0] || self) unless options[:ontop] # we don't monitor applications in the foreground
247
+
248
+ case options[:mode]
249
+ when :none
250
+ # this is only used to daemonize the currently running process
251
+ start_none
252
+ when :exec
253
+ start_exec
254
+ when :load
255
+ start_load
256
+ when :proc
257
+ start_proc
258
+ else
259
+ start_load
260
+ end
261
+ end
262
+
263
+ # def run
264
+ # if @group.controller.options[:exec]
265
+ # run_via_exec()
266
+ # else
267
+ # run_via_load()
268
+ # end
269
+ # end
270
+ #
271
+ # def run_via_exec
272
+ #
273
+ # end
274
+ #
275
+ # def run_via_load
276
+ #
277
+ # end
278
+
279
+
280
+ # This is a nice little function for debugging purposes:
281
+ # In case a multi-threaded ruby script exits due to an uncaught exception
282
+ # it may be difficult to find out where the exception came from because
283
+ # one cannot catch exceptions that are thrown in threads other than the main
284
+ # thread.
285
+ #
286
+ # This function searches for all exceptions in memory and outputs them to STDERR
287
+ # (if it is connected) and to a log file in the pid-file directory.
288
+ #
289
+ def exception_log
290
+ return unless logfile
291
+
292
+ require 'logger'
293
+
294
+ l_file = Logger.new(logfile)
295
+
296
+ # the code below finds the last exception
297
+ e = nil
298
+
299
+ ObjectSpace.each_object {|o|
300
+ if ::Exception === o
301
+ e = o
302
+ end
303
+ }
304
+
305
+ 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 ***"
306
+ l_file.error e
307
+
308
+ 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 ***"
309
+
310
+ # this code logs every exception found in memory
311
+ ObjectSpace.each_object {|o|
312
+ if ::Exception === o
313
+ l_file.error o
314
+ end
315
+ }
316
+
317
+ l_file.close
318
+ end
319
+
320
+
321
+ def stop
322
+ if options[:force] and not running?
323
+ self.zap
324
+ return
325
+ end
326
+
327
+ # Catch errors when trying to kill a process that doesn't
328
+ # exist. This happens when the process quits and hasn't been
329
+ # restarted by the monitor yet. By catching the error, we allow the
330
+ # pid file clean-up to occur.
331
+ begin
332
+ Process.kill(SIGNAL, @pid.pid)
333
+ rescue Errno::ESRCH => e
334
+ puts "#{e} #{@pid.pid}"
335
+ puts "deleting pid-file."
336
+ end
337
+
338
+ # We try to remove the pid-files by ourselves, in case the application
339
+ # didn't clean it up.
340
+ begin; @pid.cleanup; rescue ::Exception; end
341
+
342
+ end
343
+
344
+ def zap
345
+ @pid.cleanup
346
+ end
347
+
348
+ def zap!
349
+ begin; @pid.cleanup; rescue ::Exception; end
350
+ end
351
+
352
+ def show_status
353
+ running = self.running?
354
+
355
+ 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 + ')' : ''}"
356
+ end
357
+
358
+ # This function implements a (probably too simle) method to detect
359
+ # whether the program with the pid found in the pid-file is still running.
360
+ # It just searches for the pid in the output of <tt>ps ax</tt>, which
361
+ # is probably not a good idea in some cases.
362
+ # Alternatives would be to use a direct access method the unix process control
363
+ # system.
364
+ #
365
+ def running?
366
+ if @pid.exist?
367
+ return Pid.running?(@pid.pid)
368
+ end
369
+
370
+ return false
371
+ end
372
+ end
373
+
374
+ end