daemons 0.3.0 → 0.4.0
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.
- data/README +68 -2
- data/Rakefile +14 -1
- data/Releases +27 -11
- data/examples/call/call.rb +56 -0
- data/examples/call/call_monitor.rb +55 -0
- data/examples/daemonize/daemonize.rb +20 -0
- data/examples/{ctrl_crash.rb → run/ctrl_crash.rb} +1 -1
- data/examples/{ctrl_exec.rb → run/ctrl_exec.rb} +1 -1
- data/examples/{ctrl_exit.rb → run/ctrl_exit.rb} +1 -1
- data/examples/{ctrl_monitor.rb → run/ctrl_monitor.rb} +1 -1
- data/examples/{ctrl_multiple.rb → run/ctrl_multiple.rb} +1 -1
- data/examples/{ctrl_normal.rb → run/ctrl_normal.rb} +1 -1
- data/examples/{ctrl_ontop.rb → run/ctrl_ontop.rb} +1 -1
- data/examples/{myserver.rb → run/myserver.rb} +0 -0
- data/examples/{myserver_crashing.rb → run/myserver_crashing.rb} +0 -0
- data/examples/{myserver_crashing.rb.output → run/myserver_crashing.rb.output} +0 -0
- data/examples/{myserver_exiting.rb → run/myserver_exiting.rb} +0 -0
- data/lib/daemons.rb +151 -446
- data/lib/daemons/application.rb +289 -0
- data/lib/daemons/application_group.rb +152 -0
- data/lib/daemons/controller.rb +125 -0
- data/lib/daemons/daemonize.rb +98 -2
- data/lib/daemons/monitor.rb +58 -33
- data/lib/daemons/pid.rb +60 -0
- data/lib/daemons/pidfile.rb +7 -34
- data/lib/daemons/pidmem.rb +10 -0
- data/test/call_as_daemon.rb +12 -0
- metadata +40 -20
@@ -0,0 +1,289 @@
|
|
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
|
+
def initialize(group, add_options = {}, pid = nil)
|
23
|
+
@group = group
|
24
|
+
@options = group.options.dup
|
25
|
+
@options.update(add_options)
|
26
|
+
|
27
|
+
unless @pid = pid
|
28
|
+
if dir = pidfile_dir
|
29
|
+
@pid = PidFile.new(pidfile_dir(), @group.app_name, @group.multiple)
|
30
|
+
else
|
31
|
+
@pid = PidMem.new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def script
|
37
|
+
@script || @group.script
|
38
|
+
end
|
39
|
+
|
40
|
+
def pidfile_dir
|
41
|
+
Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script)
|
42
|
+
end
|
43
|
+
|
44
|
+
def logfile
|
45
|
+
(options[:log_output] && pidfile_dir()) ? File.join(pidfile_dir(), @group.app_name + '.output') : nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def start_none
|
49
|
+
unless options[:ontop]
|
50
|
+
Daemonize.daemonize #(logfile)
|
51
|
+
else
|
52
|
+
Daemonize.simulate
|
53
|
+
end
|
54
|
+
|
55
|
+
@pid.pid = Process.pid
|
56
|
+
|
57
|
+
|
58
|
+
# We need this to remove the pid-file if the applications exits by itself.
|
59
|
+
# Note that <tt>at_text</tt> will only be run if the applications exits by calling
|
60
|
+
# <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
|
61
|
+
# in your application!
|
62
|
+
#
|
63
|
+
at_exit {
|
64
|
+
@pid.cleanup rescue nil
|
65
|
+
|
66
|
+
# If the option <tt>:backtrace</tt> is used and the application did exit by itself
|
67
|
+
# create a exception log.
|
68
|
+
if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
|
69
|
+
exception_log() rescue nil
|
70
|
+
end
|
71
|
+
|
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
|
+
@pid.cleanup rescue nil
|
81
|
+
$daemons_sigterm = true
|
82
|
+
|
83
|
+
exit
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
def start_exec
|
88
|
+
unless options[:ontop]
|
89
|
+
Daemonize.daemonize(logfile)
|
90
|
+
else
|
91
|
+
Daemonize.simulate(logfile)
|
92
|
+
end
|
93
|
+
|
94
|
+
@pid.pid = Process.pid
|
95
|
+
|
96
|
+
ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
|
97
|
+
# haven't tested yet if this is really passed to the exec'd process...
|
98
|
+
|
99
|
+
Kernel.exec(script(), *ARGV)
|
100
|
+
end
|
101
|
+
|
102
|
+
def start_load
|
103
|
+
unless options[:ontop]
|
104
|
+
Daemonize.daemonize(logfile)
|
105
|
+
else
|
106
|
+
Daemonize.simulate(logfile)
|
107
|
+
end
|
108
|
+
|
109
|
+
@pid.pid = Process.pid
|
110
|
+
|
111
|
+
|
112
|
+
# We need this to remove the pid-file if the applications exits by itself.
|
113
|
+
# Note that <tt>at_text</tt> will only be run if the applications exits by calling
|
114
|
+
# <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt>
|
115
|
+
# in your application!
|
116
|
+
#
|
117
|
+
at_exit {
|
118
|
+
@pid.cleanup rescue nil
|
119
|
+
|
120
|
+
# If the option <tt>:backtrace</tt> is used and the application did exit by itself
|
121
|
+
# create a exception log.
|
122
|
+
if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
|
123
|
+
exception_log() rescue nil
|
124
|
+
end
|
125
|
+
|
126
|
+
}
|
127
|
+
|
128
|
+
# This part is needed to remove the pid-file if the application is killed by
|
129
|
+
# daemons or manually by the user.
|
130
|
+
# Note that the applications is not supposed to overwrite the signal handler for
|
131
|
+
# 'TERM'.
|
132
|
+
#
|
133
|
+
trap('TERM') {
|
134
|
+
@pid.cleanup rescue nil
|
135
|
+
$daemons_sigterm = true
|
136
|
+
|
137
|
+
exit
|
138
|
+
}
|
139
|
+
|
140
|
+
# Know we really start the script...
|
141
|
+
$DAEMONS_ARGV = @controller_argv
|
142
|
+
ENV['DAEMONS_ARGV'] = @controller_argv.join(' ')
|
143
|
+
|
144
|
+
ARGV.clear
|
145
|
+
ARGV.concat @app_argv if @app_argv
|
146
|
+
|
147
|
+
# TODO: begin - rescue - end around this and exception logging
|
148
|
+
load script()
|
149
|
+
end
|
150
|
+
|
151
|
+
def start_proc
|
152
|
+
return unless options[:proc]
|
153
|
+
|
154
|
+
unless options[:ontop]
|
155
|
+
@pid.pid = Daemonize.call_as_daemon(options[:proc], logfile)
|
156
|
+
else
|
157
|
+
# Daemonize.simulate(logfile)
|
158
|
+
#
|
159
|
+
# @pid.pid = Process.pid
|
160
|
+
#
|
161
|
+
# Thread.new(&options[:proc])
|
162
|
+
unless @pid.pid = Process.fork
|
163
|
+
Daemonize.simulate(logfile)
|
164
|
+
options[:proc].call
|
165
|
+
exit
|
166
|
+
else
|
167
|
+
Process.detach(@pid.pid)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
def start
|
174
|
+
@group.create_monitor(@group.applications[0] || self)
|
175
|
+
|
176
|
+
case options[:mode]
|
177
|
+
when :none
|
178
|
+
start_none
|
179
|
+
when :exec
|
180
|
+
start_exec
|
181
|
+
when :load
|
182
|
+
start_load
|
183
|
+
when :proc
|
184
|
+
start_proc
|
185
|
+
else
|
186
|
+
start_load
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# def run
|
191
|
+
# if @group.controller.options[:exec]
|
192
|
+
# run_via_exec()
|
193
|
+
# else
|
194
|
+
# run_via_load()
|
195
|
+
# end
|
196
|
+
# end
|
197
|
+
#
|
198
|
+
# def run_via_exec
|
199
|
+
#
|
200
|
+
# end
|
201
|
+
#
|
202
|
+
# def run_via_load
|
203
|
+
#
|
204
|
+
# end
|
205
|
+
|
206
|
+
|
207
|
+
# This is a nice little function for debugging purposes:
|
208
|
+
# In case a multi-threaded ruby script exits due to an uncaught exception
|
209
|
+
# it may be difficult to find out where the exception came from because
|
210
|
+
# one cannot catch exceptions that are thrown in threads other than the main
|
211
|
+
# thread.
|
212
|
+
#
|
213
|
+
# This function searches for all exceptions in memory and outputs them to STDERR
|
214
|
+
# (if it is connected) and to a log file in the pid-file directory.
|
215
|
+
#
|
216
|
+
def exception_log
|
217
|
+
require 'logger'
|
218
|
+
|
219
|
+
l_file = Logger.new(File.join(pidfile_dir(), @group.app_name + '.log'))
|
220
|
+
|
221
|
+
|
222
|
+
# the code below only logs the last exception
|
223
|
+
# e = nil
|
224
|
+
#
|
225
|
+
# ObjectSpace.each_object {|o|
|
226
|
+
# if ::Exception === o
|
227
|
+
# e = o
|
228
|
+
# end
|
229
|
+
# }
|
230
|
+
#
|
231
|
+
# l_file.error e
|
232
|
+
# l_file.close
|
233
|
+
|
234
|
+
# this code logs every exception found in memory
|
235
|
+
ObjectSpace.each_object {|o|
|
236
|
+
if ::Exception === o
|
237
|
+
l_file.error o
|
238
|
+
end
|
239
|
+
}
|
240
|
+
|
241
|
+
l_file.close
|
242
|
+
end
|
243
|
+
|
244
|
+
|
245
|
+
def stop
|
246
|
+
if options[:force] and not running?
|
247
|
+
self.zap
|
248
|
+
return
|
249
|
+
end
|
250
|
+
|
251
|
+
Process.kill('TERM', @pid.pid)
|
252
|
+
|
253
|
+
# We try to remove the pid-files by ourselves, in case the application
|
254
|
+
# didn't clean it up.
|
255
|
+
@pid.cleanup rescue nil
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
def zap
|
260
|
+
@pid.cleanup
|
261
|
+
end
|
262
|
+
|
263
|
+
def zap!
|
264
|
+
@pid.cleanup rescue nil
|
265
|
+
end
|
266
|
+
|
267
|
+
def show_status
|
268
|
+
running = self.running?
|
269
|
+
|
270
|
+
puts "#{self.group.app_name}: #{running ? '' : 'not '}running#{(running and @pid.exists?) ? ' [pid ' + @pid.pid.to_s + ']' : ''}#{(@pid.exists? and not running) ? ' (but pid-file exists: ' + @pid.pid.to_s + ')' : ''}"
|
271
|
+
end
|
272
|
+
|
273
|
+
# This function implements a (probably too simle) method to detect
|
274
|
+
# whether the program with the pid found in the pid-file is still running.
|
275
|
+
# It just searches for the pid in the output of <tt>ps ax</tt>, which
|
276
|
+
# is probably not a good idea in some cases.
|
277
|
+
# Alternatives would be to use a direct access method the unix process control
|
278
|
+
# system.
|
279
|
+
#
|
280
|
+
def running?
|
281
|
+
if @pid.exists?
|
282
|
+
return Pid.running?(@pid.pid)
|
283
|
+
end
|
284
|
+
|
285
|
+
return false
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
|
2
|
+
module Daemons
|
3
|
+
class ApplicationGroup
|
4
|
+
|
5
|
+
attr_reader :app_name
|
6
|
+
attr_reader :script
|
7
|
+
|
8
|
+
attr_reader :monitor
|
9
|
+
|
10
|
+
#attr_reader :controller
|
11
|
+
|
12
|
+
attr_reader :options
|
13
|
+
|
14
|
+
attr_reader :applications
|
15
|
+
|
16
|
+
attr_accessor :controller_argv
|
17
|
+
attr_accessor :app_argv
|
18
|
+
|
19
|
+
attr_accessor :dir_mode
|
20
|
+
attr_accessor :dir
|
21
|
+
|
22
|
+
# true if the application is supposed to run in multiple instances
|
23
|
+
attr_reader :multiple
|
24
|
+
|
25
|
+
|
26
|
+
def initialize(app_name, options = {})
|
27
|
+
@app_name = app_name
|
28
|
+
@options = options
|
29
|
+
|
30
|
+
if options[:script]
|
31
|
+
@script = File.expand_path(options[:script])
|
32
|
+
end
|
33
|
+
|
34
|
+
#@controller = controller
|
35
|
+
@monitor = nil
|
36
|
+
|
37
|
+
#options = controller.options
|
38
|
+
|
39
|
+
@multiple = options[:multiple] || false
|
40
|
+
|
41
|
+
@dir_mode = options[:dir_mode] || :script
|
42
|
+
@dir = options[:dir] || ''
|
43
|
+
|
44
|
+
#@applications = find_applications(pidfile_dir())
|
45
|
+
@applications = []
|
46
|
+
end
|
47
|
+
|
48
|
+
# Setup the application group.
|
49
|
+
# Currently this functions calls <tt>find_applications</tt> which finds
|
50
|
+
# all running instances of the application and populates the application array.
|
51
|
+
#
|
52
|
+
def setup
|
53
|
+
if script
|
54
|
+
@applications = find_applications(pidfile_dir())
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def pidfile_dir
|
59
|
+
PidFile.dir(@dir_mode, @dir, script)
|
60
|
+
end
|
61
|
+
|
62
|
+
def find_applications(dir)
|
63
|
+
pid_files = PidFile.find_files(dir, app_name)
|
64
|
+
|
65
|
+
#pp pid_files
|
66
|
+
|
67
|
+
@monitor = Monitor.find(dir, app_name + '_monitor')
|
68
|
+
|
69
|
+
pid_files.reject! {|f| f =~ /_monitor.pid$/}
|
70
|
+
|
71
|
+
return pid_files.map {|f|
|
72
|
+
app = Application.new(self, {}, PidFile.existing(f))
|
73
|
+
setup_app(app)
|
74
|
+
app
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def new_application(add_options = {})
|
79
|
+
if @applications.size > 0 and not @multiple
|
80
|
+
if options[:force]
|
81
|
+
@applications.delete_if {|a|
|
82
|
+
unless a.running?
|
83
|
+
a.zap
|
84
|
+
true
|
85
|
+
end
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
raise RuntimeException.new('there is already one or more instance(s) of the program running') unless @applications.empty?
|
90
|
+
end
|
91
|
+
|
92
|
+
app = Application.new(self, add_options)
|
93
|
+
|
94
|
+
setup_app(app)
|
95
|
+
|
96
|
+
@applications << app
|
97
|
+
|
98
|
+
return app
|
99
|
+
end
|
100
|
+
|
101
|
+
def setup_app(app)
|
102
|
+
app.controller_argv = @controller_argv
|
103
|
+
app.app_argv = @app_argv
|
104
|
+
end
|
105
|
+
private :setup_app
|
106
|
+
|
107
|
+
def create_monitor(an_app)
|
108
|
+
return if @monitor
|
109
|
+
|
110
|
+
if options[:monitor]
|
111
|
+
@monitor = Monitor.new(an_app)
|
112
|
+
|
113
|
+
@monitor.start(@applications)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def start_all
|
118
|
+
@monitor.stop if @monitor
|
119
|
+
@monitor = nil
|
120
|
+
|
121
|
+
@applications.each {|a|
|
122
|
+
fork {
|
123
|
+
a.start
|
124
|
+
}
|
125
|
+
}
|
126
|
+
end
|
127
|
+
|
128
|
+
def stop_all(force = false)
|
129
|
+
@monitor.stop if @monitor
|
130
|
+
|
131
|
+
@applications.each {|a|
|
132
|
+
if force
|
133
|
+
a.stop rescue nil
|
134
|
+
else
|
135
|
+
a.stop
|
136
|
+
end
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
140
|
+
def zap_all
|
141
|
+
@monitor.stop if @monitor
|
142
|
+
|
143
|
+
@applications.each {|a| a.zap}
|
144
|
+
end
|
145
|
+
|
146
|
+
def show_status
|
147
|
+
@applications.each {|a| a.show_status}
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
|
2
|
+
module Daemons
|
3
|
+
class Controller
|
4
|
+
|
5
|
+
attr_reader :app_name
|
6
|
+
|
7
|
+
attr_reader :group
|
8
|
+
|
9
|
+
attr_reader :options
|
10
|
+
|
11
|
+
|
12
|
+
COMMANDS = [
|
13
|
+
'start',
|
14
|
+
'stop',
|
15
|
+
'restart',
|
16
|
+
'run',
|
17
|
+
'zap',
|
18
|
+
'status'
|
19
|
+
]
|
20
|
+
|
21
|
+
def initialize(options = {}, argv = [])
|
22
|
+
@options = options
|
23
|
+
@argv = argv
|
24
|
+
|
25
|
+
if options[:script]
|
26
|
+
@script = File.expand_path(options[:script])
|
27
|
+
|
28
|
+
@app_name = File.split(@script)[1]
|
29
|
+
end
|
30
|
+
|
31
|
+
@command, @controller_part, @app_part = Controller.split_argv(argv)
|
32
|
+
|
33
|
+
#@options[:dir_mode] ||= :script
|
34
|
+
|
35
|
+
@optparse = Optparse.new(self)
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
# This function is used to do a final update of the options passed to the application
|
40
|
+
# before they are really used.
|
41
|
+
#
|
42
|
+
# Note that this function should only update <tt>@options</tt> and no other variables.
|
43
|
+
#
|
44
|
+
def setup_options
|
45
|
+
#@options[:ontop] ||= true
|
46
|
+
end
|
47
|
+
|
48
|
+
def run
|
49
|
+
@options.update @optparse.parse(@controller_part).delete_if {|k,v| !v}
|
50
|
+
|
51
|
+
setup_options()
|
52
|
+
|
53
|
+
#pp @options
|
54
|
+
|
55
|
+
@group = ApplicationGroup.new(@app_name, @options)
|
56
|
+
@group.controller_argv = @controller_part
|
57
|
+
@group.app_argv = @app_part
|
58
|
+
|
59
|
+
@group.setup
|
60
|
+
|
61
|
+
case @command
|
62
|
+
when 'start'
|
63
|
+
@group.new_application.start
|
64
|
+
when 'run'
|
65
|
+
@group.new_application.run
|
66
|
+
when 'stop'
|
67
|
+
@group.stop_all
|
68
|
+
when 'restart'
|
69
|
+
unless @group.applications.empty?
|
70
|
+
@group.stop_all
|
71
|
+
sleep 1
|
72
|
+
@group.start_all
|
73
|
+
end
|
74
|
+
when 'zap'
|
75
|
+
@group.zap_all
|
76
|
+
when 'status'
|
77
|
+
unless @group.applications.empty?
|
78
|
+
@group.show_status
|
79
|
+
else
|
80
|
+
puts "#{@group.app_name}: no instances running"
|
81
|
+
end
|
82
|
+
when nil
|
83
|
+
raise CmdException.new('no command given')
|
84
|
+
#puts "ERROR: No command given"; puts
|
85
|
+
|
86
|
+
#print_usage()
|
87
|
+
#raise('usage function not implemented')
|
88
|
+
else
|
89
|
+
raise Error.new("command '#{@command}' not implemented")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
# Split an _argv_ array.
|
95
|
+
# +argv+ is assumed to be in the following format:
|
96
|
+
# ['command', 'controller option 1', 'controller option 2', ..., '--', 'app option 1', ...]
|
97
|
+
#
|
98
|
+
# <tt>command</tt> must be one of the commands listed in <tt>COMMANDS</tt>
|
99
|
+
#
|
100
|
+
# *Returns*: the command as a string, the controller options as an array, the appliation options
|
101
|
+
# as an array
|
102
|
+
#
|
103
|
+
def Controller.split_argv(argv)
|
104
|
+
argv = argv.dup
|
105
|
+
|
106
|
+
command = nil
|
107
|
+
controller_part = []
|
108
|
+
app_part = []
|
109
|
+
|
110
|
+
if COMMANDS.include? argv[0]
|
111
|
+
command = argv.shift
|
112
|
+
end
|
113
|
+
|
114
|
+
if i = argv.index('--')
|
115
|
+
controller_part = argv[0..i-1]
|
116
|
+
app_part = argv[i+1..-1]
|
117
|
+
else
|
118
|
+
controller_part = argv[0..-1]
|
119
|
+
end
|
120
|
+
|
121
|
+
return command, controller_part, app_part
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|