fire_and_forget 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,271 +0,0 @@
1
- #--
2
- ###############################################################################
3
- # daemonize.rb is a slightly modified version of daemonize.rb was #
4
- # from the Daemonize Library written by Travis Whitton #
5
- # for details, read the notice below #
6
- ###############################################################################
7
- #++
8
- #
9
- #
10
- # =Daemonize Library
11
- #
12
- # February. 4, 2005 Travis Whitton <whitton@atlantic.net>
13
- #
14
- # Daemonize allows you to easily modify any existing Ruby program to run
15
- # as a daemon. See README.rdoc for more details.
16
- #
17
- # == How to install
18
- # 1. su to root
19
- # 2. ruby install.rb
20
- # build the docs if you want to
21
- # 3. rdoc --main README.rdoc daemonize.rb README.rdoc
22
- #
23
- # == Copying
24
- # The Daemonize extension module is copywrited free software by Travis Whitton
25
- # <whitton@atlantic.net>. You can redistribute it under the terms specified in
26
- # the COPYING file of the Ruby distribution.
27
- #
28
- # == WARRANTY
29
- # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
30
- # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31
- # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
32
- # PURPOSE.
33
- #
34
- #
35
- # ----
36
- #
37
- # == Purpose
38
- #
39
- # Daemonize is a module derived from Perl's Proc::Daemon module. This module
40
- # allows you to easily modify any existing Ruby program to run as a daemon.
41
- # A daemon is a process that runs in the background with no controlling terminal.
42
- # Generally servers (like FTP and HTTP servers) run as daemon processes.
43
- # Note, do not make the mistake that a daemon == server. Converting a program
44
- # to a daemon by hand is a relatively simple process; however, this module will
45
- # save you the effort of repeatedly looking up the procedure, and it will also
46
- # insure that your programs are daemonized in the safest and most corrects
47
- # fashion possible.
48
- #
49
- # == Procedure
50
- #
51
- # The Daemonize module does the following:
52
- #
53
- # Forks a child and exits the parent process.
54
- #
55
- # Becomes a session leader (which detaches the program from
56
- # the controlling terminal).
57
- #
58
- # Forks another child process and exits first child. This prevents
59
- # the potential of acquiring a controlling terminal.
60
- #
61
- # Changes the current working directory to "/".
62
- #
63
- # Clears the file creation mask.
64
- #
65
- # Closes file descriptors.
66
- #
67
- # == Example usage
68
- #
69
- # Using the Daemonize module is extremely simple:
70
- #
71
- # require 'daemonize'
72
- #
73
- # class TestDaemon
74
- # include Daemonize
75
- #
76
- # def initialize
77
- # daemonize()
78
- # loop do
79
- # # do some work here
80
- # end
81
- # end
82
- # end
83
- #
84
- # == Credits
85
- #
86
- # Daemonize was written by Travis Whitton and is based on Perl's
87
- # Proc::Daemonize, which was written by Earl Hood. The above documentation
88
- # is also partially borrowed from the Proc::Daemonize POD documentation.
89
-
90
-
91
-
92
- module Daemonize
93
- VERSION = "0.1.1m"
94
-
95
- # Try to fork if at all possible retrying every 5 sec if the
96
- # maximum process limit for the system has been reached
97
- def safefork
98
- tryagain = true
99
-
100
- while tryagain
101
- tryagain = false
102
- begin
103
- if pid = fork
104
- return pid
105
- end
106
- rescue Errno::EWOULDBLOCK
107
- sleep 5
108
- tryagain = true
109
- end
110
- end
111
- end
112
- module_function :safefork
113
-
114
-
115
- def simulate(logfile_name = nil)
116
- # NOTE: STDOUT and STDERR will not be redirected to the logfile, because in :ontop mode, we normally want to see the output
117
-
118
- Dir.chdir "/" # Release old working directory
119
- File.umask 0000 # Insure sensible umask
120
-
121
- # Make sure all file descriptors are closed
122
- ObjectSpace.each_object(IO) do |io|
123
- unless [STDIN, STDOUT, STDERR].include?(io)
124
- begin
125
- unless io.closed?
126
- io.close
127
- end
128
- rescue ::Exception
129
- end
130
- end
131
- end
132
-
133
- # Free file descriptors and
134
- # point them somewhere sensible
135
- # STDOUT/STDERR should go to a logfile
136
-
137
- begin; STDIN.reopen "/dev/null"; rescue ::Exception; end
138
- end
139
- module_function :simulate
140
-
141
-
142
- def call_as_daemon(block, logfile_name = nil, app_name = nil)
143
- rd, wr = IO.pipe
144
-
145
- if tmppid = safefork
146
- # parent
147
- wr.close
148
- pid = rd.read.to_i
149
- rd.close
150
-
151
- Process.waitpid(tmppid)
152
-
153
- return pid
154
- else
155
- # child
156
-
157
- rd.close
158
-
159
- # Detach from the controlling terminal
160
- unless sess_id = Process.setsid
161
- raise Daemons.RuntimeException.new('cannot detach from controlling terminal')
162
- end
163
-
164
- # Prevent the possibility of acquiring a controlling terminal
165
- #if oldmode.zero?
166
- trap 'SIGHUP', 'IGNORE'
167
- exit if pid = safefork
168
- #end
169
-
170
- wr.write Process.pid
171
- wr.close
172
-
173
- $0 = app_name if app_name
174
-
175
- Dir.chdir "/" # Release old working directory
176
- File.umask 0000 # Insure sensible umask
177
-
178
- # Make sure all file descriptors are closed
179
- ObjectSpace.each_object(IO) do |io|
180
- unless [STDIN, STDOUT, STDERR].include?(io)
181
- begin
182
- unless io.closed?
183
- io.close
184
- end
185
- rescue ::Exception
186
- end
187
- end
188
- end
189
-
190
- ios = Array.new(8192){|i| IO.for_fd(i) rescue nil}.compact
191
- ios.each do |io|
192
- next if io.fileno < 3
193
- io.close
194
- end
195
-
196
-
197
- redirect_io(logfile_name)
198
-
199
- block.call
200
-
201
- exit
202
- end
203
- end
204
- module_function :call_as_daemon
205
-
206
-
207
- # This method causes the current running process to become a daemon
208
- def daemonize(logfile_name = nil, app_name = nil)
209
- srand # Split rand streams between spawning and daemonized process
210
- safefork and exit # Fork and exit from the parent
211
-
212
- # Detach from the controlling terminal
213
- unless sess_id = Process.setsid
214
- raise Daemons.RuntimeException.new('cannot detach from controlling terminal')
215
- end
216
-
217
- # Prevent the possibility of acquiring a controlling terminal
218
- #if oldmode.zero?
219
- trap 'SIGHUP', 'IGNORE'
220
- exit if pid = safefork
221
- #end
222
-
223
- $0 = app_name if app_name
224
-
225
- Dir.chdir "/" # Release old working directory
226
- File.umask 0000 # Insure sensible umask
227
-
228
- # Make sure all file descriptors are closed
229
- ObjectSpace.each_object(IO) do |io|
230
- unless [STDIN, STDOUT, STDERR].include?(io)
231
- begin
232
- unless io.closed?
233
- io.close
234
- end
235
- rescue ::Exception
236
- end
237
- end
238
- end
239
-
240
- redirect_io(logfile_name)
241
-
242
- #return oldmode ? sess_id : 0 # Return value is mostly irrelevant
243
- return sess_id
244
- end
245
- module_function :daemonize
246
-
247
-
248
- # Free file descriptors and
249
- # point them somewhere sensible
250
- # STDOUT/STDERR should go to a logfile
251
- def redirect_io(logfile_name)
252
- begin; STDIN.reopen "/dev/null"; rescue ::Exception; end
253
-
254
- if logfile_name
255
- begin
256
- STDOUT.reopen logfile_name, "a"
257
- File.chmod(0644, logfile_name)
258
- STDOUT.sync = true
259
- rescue ::Exception
260
- begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end
261
- end
262
- else
263
- begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end
264
- end
265
-
266
- begin; STDERR.reopen STDOUT; rescue ::Exception; end
267
- STDERR.sync = true
268
- end
269
- module_function :redirect_io
270
-
271
- end
@@ -1,12 +0,0 @@
1
- require 'etc'
2
-
3
- Etc.instance_eval do
4
- def groupname(gid)
5
- Etc.group {|e| return e.name if gid == e.gid }
6
- nil
7
- end
8
- def username(uid)
9
- Etc.passwd {|e| return e.name if uid == e.uid }
10
- nil
11
- end
12
- end
@@ -1,28 +0,0 @@
1
-
2
- module Daemons
3
-
4
- class Exception < ::RuntimeError
5
- end
6
-
7
- class RuntimeException < Exception
8
- end
9
-
10
- class CmdException < Exception
11
- end
12
-
13
- class Error < Exception
14
- end
15
-
16
- class SystemError < Error
17
-
18
- attr_reader :system_error
19
-
20
- def initialize(msg, system_error)
21
- super(msg)
22
-
23
- @system_error = system_error
24
- end
25
-
26
- end
27
-
28
- end
@@ -1,138 +0,0 @@
1
-
2
- module Daemons
3
-
4
- require 'daemons/daemonize'
5
-
6
- class Monitor
7
-
8
- def self.find(dir, app_name)
9
- pid = PidFile.find_files(dir, app_name, false)[0]
10
-
11
- if pid
12
- pid = PidFile.existing(pid)
13
-
14
- unless PidFile.running?(pid.pid)
15
- begin; pid.cleanup; rescue ::Exception; end
16
- return
17
- end
18
-
19
- monitor = self.allocate
20
-
21
- monitor.instance_variable_set(:@pid, pid)
22
-
23
- return monitor
24
- end
25
-
26
- return nil
27
- end
28
-
29
-
30
- def initialize(an_app)
31
- @app = an_app
32
- @app_name = an_app.group.app_name + '_monitor'
33
-
34
- if an_app.pidfile_dir
35
- @pid = PidFile.new(an_app.pidfile_dir, @app_name, false)
36
- else
37
- @pid = PidMem.new
38
- end
39
- end
40
-
41
- def watch(applications)
42
- sleep(30)
43
-
44
- loop do
45
- applications.each {|a|
46
- sleep(10)
47
-
48
- unless a.running?
49
- a.zap!
50
-
51
- Process.detach(fork { a.start })
52
-
53
- sleep(10)
54
- end
55
- }
56
-
57
- sleep(30)
58
- end
59
- end
60
- private :watch
61
-
62
-
63
- def start_with_pidfile(applications)
64
- fork do
65
- Daemonize.daemonize(nil, @app_name)
66
-
67
- begin
68
- @pid.pid = Process.pid
69
-
70
- # at_exit {
71
- # begin; @pid.cleanup; rescue ::Exception; end
72
- # }
73
-
74
- # This part is needed to remove the pid-file if the application is killed by
75
- # daemons or manually by the user.
76
- # Note that the applications is not supposed to overwrite the signal handler for
77
- # 'TERM'.
78
- #
79
- # trap('TERM') {
80
- # begin; @pid.cleanup; rescue ::Exception; end
81
- # exit
82
- # }
83
-
84
- watch(applications)
85
- rescue ::Exception => e
86
- begin
87
- File.open(@app.logfile, 'a') {|f|
88
- f.puts Time.now
89
- f.puts e
90
- f.puts e.backtrace.inspect
91
- }
92
- ensure
93
- begin; @pid.cleanup; rescue ::Exception; end
94
- exit!
95
- end
96
- end
97
- end
98
- end
99
- private :start_with_pidfile
100
-
101
- def start_without_pidfile(applications)
102
- Thread.new { watch(applications) }
103
- end
104
- private :start_without_pidfile
105
-
106
-
107
- def start(applications)
108
- return if applications.empty?
109
-
110
- if @pid.kind_of?(PidFile)
111
- start_with_pidfile(applications)
112
- else
113
- start_without_pidfile(applications)
114
- end
115
- end
116
-
117
-
118
- def stop
119
- begin
120
- pid = @pid.pid
121
- Process.kill(Application::SIGNAL, pid)
122
- Timeout::timeout(5) {
123
- while Pid.running?(pid)
124
- sleep(0.1)
125
- end
126
- }
127
- rescue ::Exception => e
128
- puts "#{e} #{pid}"
129
- puts "deleting pid-file."
130
- end
131
-
132
- # We try to remove the pid-files by ourselves, in case the application
133
- # didn't clean it up.
134
- begin; @pid.cleanup; rescue ::Exception; end
135
- end
136
-
137
- end
138
- end