mlanett-daemons 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +17 -0
  4. data/LICENSE +29 -0
  5. data/README +223 -0
  6. data/Rakefile +15 -0
  7. data/Releases +126 -0
  8. data/TODO +6 -0
  9. data/daemons.gemspec +27 -0
  10. data/daemons.tmproj +56 -0
  11. data/examples/call/call.rb +56 -0
  12. data/examples/call/call.rb.log +1 -0
  13. data/examples/call/call_monitor.rb +55 -0
  14. data/examples/daemonize/daemonize.rb +20 -0
  15. data/examples/run/ctrl_crash.rb +17 -0
  16. data/examples/run/ctrl_exec.rb +16 -0
  17. data/examples/run/ctrl_exit.rb +15 -0
  18. data/examples/run/ctrl_keep_pid_files.rb +17 -0
  19. data/examples/run/ctrl_monitor.rb +16 -0
  20. data/examples/run/ctrl_multiple.rb +16 -0
  21. data/examples/run/ctrl_normal.rb +12 -0
  22. data/examples/run/ctrl_ontop.rb +16 -0
  23. data/examples/run/ctrl_optionparser.rb +43 -0
  24. data/examples/run/ctrl_proc.rb +25 -0
  25. data/examples/run/ctrl_proc.rb.output +101 -0
  26. data/examples/run/ctrl_proc_multiple.rb +22 -0
  27. data/examples/run/ctrl_proc_multiple.rb.output +2 -0
  28. data/examples/run/ctrl_proc_simple.rb +17 -0
  29. data/examples/run/myserver.rb +12 -0
  30. data/examples/run/myserver_crashing.rb +14 -0
  31. data/examples/run/myserver_crashing.rb.output +30 -0
  32. data/examples/run/myserver_exiting.rb +8 -0
  33. data/html/classes/Daemonize.html +497 -0
  34. data/html/classes/Daemons.html +683 -0
  35. data/html/classes/Daemons/Application.html +836 -0
  36. data/html/classes/Daemons/ApplicationGroup.html +508 -0
  37. data/html/classes/Daemons/CmdException.html +113 -0
  38. data/html/classes/Daemons/Controller.html +429 -0
  39. data/html/classes/Daemons/Error.html +113 -0
  40. data/html/classes/Daemons/Exception.html +111 -0
  41. data/html/classes/Daemons/Monitor.html +263 -0
  42. data/html/classes/Daemons/Optparse.html +244 -0
  43. data/html/classes/Daemons/Pid.html +339 -0
  44. data/html/classes/Daemons/PidFile.html +441 -0
  45. data/html/classes/Daemons/PidMem.html +126 -0
  46. data/html/classes/Daemons/RuntimeException.html +113 -0
  47. data/html/classes/Daemons/SystemError.html +163 -0
  48. data/html/created.rid +1 -0
  49. data/html/files/README.html +377 -0
  50. data/html/files/Releases.html +342 -0
  51. data/html/files/TODO.html +121 -0
  52. data/html/files/lib/daemons/application_group_rb.html +101 -0
  53. data/html/files/lib/daemons/application_rb.html +110 -0
  54. data/html/files/lib/daemons/cmdline_rb.html +101 -0
  55. data/html/files/lib/daemons/controller_rb.html +101 -0
  56. data/html/files/lib/daemons/daemonize_rb.html +207 -0
  57. data/html/files/lib/daemons/exceptions_rb.html +101 -0
  58. data/html/files/lib/daemons/monitor_rb.html +108 -0
  59. data/html/files/lib/daemons/pid_rb.html +108 -0
  60. data/html/files/lib/daemons/pidfile_rb.html +108 -0
  61. data/html/files/lib/daemons/pidmem_rb.html +108 -0
  62. data/html/files/lib/daemons_rb.html +117 -0
  63. data/html/fr_class_index.html +41 -0
  64. data/html/fr_file_index.html +40 -0
  65. data/html/fr_method_index.html +91 -0
  66. data/html/index.html +24 -0
  67. data/html/rdoc-style.css +208 -0
  68. data/lib/daemons.rb +284 -0
  69. data/lib/daemons/application.rb +376 -0
  70. data/lib/daemons/application_group.rb +152 -0
  71. data/lib/daemons/cmdline.rb +117 -0
  72. data/lib/daemons/controller.rb +137 -0
  73. data/lib/daemons/daemonize.rb +263 -0
  74. data/lib/daemons/exceptions.rb +28 -0
  75. data/lib/daemons/monitor.rb +136 -0
  76. data/lib/daemons/pid.rb +115 -0
  77. data/lib/daemons/pidfile.rb +111 -0
  78. data/lib/daemons/pidmem.rb +10 -0
  79. data/lib/daemons/version.rb +3 -0
  80. data/setup.rb +1360 -0
  81. data/test/call_as_daemon.rb +12 -0
  82. data/test/tc_main.rb +24 -0
  83. data/test/test1.rb +19 -0
  84. data/test/testapp.rb +11 -0
  85. metadata +170 -0
data/lib/daemons.rb ADDED
@@ -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
+ require "daemons/version"
15
+
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.11"
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,376 @@
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 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
40
+
41
+ def script
42
+ @script || @group.script
43
+ end
44
+
45
+ def pidfile_dir
46
+ Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script)
47
+ end
48
+
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
53
+
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
58
+
59
+ # this function is only used to daemonize the currently running process (Daemons.daemonize)
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
102
+
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
125
+
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
178
+
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
241
+
242
+
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
260
+
261
+ # def run
262
+ # if @group.controller.options[:exec]
263
+ # run_via_exec()
264
+ # else
265
+ # run_via_load()
266
+ # end
267
+ # end
268
+ #
269
+ # def run_via_exec
270
+ #
271
+ # end
272
+ #
273
+ # def run_via_load
274
+ #
275
+ # end
276
+
277
+
278
+ # This is a nice little function for debugging purposes:
279
+ # In case a multi-threaded ruby script exits due to an uncaught exception
280
+ # it may be difficult to find out where the exception came from because
281
+ # one cannot catch exceptions that are thrown in threads other than the main
282
+ # thread.
283
+ #
284
+ # This function searches for all exceptions in memory and outputs them to STDERR
285
+ # (if it is connected) and to a log file in the pid-file directory.
286
+ #
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
317
+
318
+
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
+ pid = @pid.pid
330
+ begin
331
+ Process.kill(SIGNAL, pid)
332
+ while Pid.running?(pid)
333
+ sleep 0.1
334
+ end
335
+ rescue Errno::ESRCH => e
336
+ puts "#{e} #{@pid.pid}"
337
+ puts "deleting pid-file."
338
+ end
339
+
340
+ # We try to remove the pid-files by ourselves, in case the application
341
+ # didn't clean it up.
342
+ begin; @pid.cleanup; rescue ::Exception; end
343
+
344
+ end
345
+
346
+ def zap
347
+ @pid.cleanup
348
+ end
349
+
350
+ def zap!
351
+ begin; @pid.cleanup; rescue ::Exception; end
352
+ end
353
+
354
+ def show_status
355
+ running = self.running?
356
+
357
+ 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 + ')' : ''}"
358
+ end
359
+
360
+ # This function implements a (probably too simle) method to detect
361
+ # whether the program with the pid found in the pid-file is still running.
362
+ # It just searches for the pid in the output of <tt>ps ax</tt>, which
363
+ # is probably not a good idea in some cases.
364
+ # Alternatives would be to use a direct access method the unix process control
365
+ # system.
366
+ #
367
+ def running?
368
+ if @pid.exist?
369
+ return Pid.running?(@pid.pid)
370
+ end
371
+
372
+ return false
373
+ end
374
+ end
375
+
376
+ end