"yadaemon.rb" is my daemon wrapper class to protect the multiple process invocation.
Revisão | 1425596385054f3a66ec425c64bd057df793bc22 (tree) |
---|---|
Hora | 2011-01-29 01:25:53 |
Autor | Yasuhiro ABE <yasundial@user...> |
Commiter | Yasuhiro ABE |
Added force_stop method for emergency stop function.
@@ -19,6 +19,9 @@ daemon = YaDaemon.new(appname,"#{appname}.pid","/tmp", opts) | ||
19 | 19 | if ARGV[0] == "restart" |
20 | 20 | begin |
21 | 21 | daemon.stop |
22 | + while daemon.check_proc | |
23 | + sleep 1 | |
24 | + end | |
22 | 25 | puts "running process was terminated." |
23 | 26 | rescue |
24 | 27 | puts $! |
@@ -27,6 +30,9 @@ if ARGV[0] == "restart" | ||
27 | 30 | elsif ARGV[0] == "stop" |
28 | 31 | begin |
29 | 32 | daemon.stop |
33 | + while daemon.check_proc | |
34 | + sleep 1 | |
35 | + end | |
30 | 36 | puts "running process was terminated." |
31 | 37 | rescue |
32 | 38 | puts $! |
@@ -47,4 +53,4 @@ daemon.run do |pid| | ||
47 | 53 | sleep 5 |
48 | 54 | end |
49 | 55 | end |
50 | -exit(1) | |
56 | +puts "another daemon is already running" if daemon.running |
@@ -17,6 +17,9 @@ daemon = YaDaemon.new(appname,"#{appname}.pid","/tmp", opts) | ||
17 | 17 | if ARGV[0] == "restart" |
18 | 18 | begin |
19 | 19 | daemon.stop |
20 | + while daemon.check_proc | |
21 | + sleep 1 | |
22 | + end | |
20 | 23 | puts "running process was terminated." |
21 | 24 | rescue |
22 | 25 | puts $! |
@@ -25,6 +28,9 @@ if ARGV[0] == "restart" | ||
25 | 28 | elsif ARGV[0] == "stop" |
26 | 29 | begin |
27 | 30 | daemon.stop |
31 | + while daemon.check_proc | |
32 | + sleep 1 | |
33 | + end | |
28 | 34 | puts "running process was terminated." |
29 | 35 | rescue |
30 | 36 | puts $! |
@@ -45,3 +51,4 @@ daemon.run do |pid| | ||
45 | 51 | sleep 5 |
46 | 52 | end |
47 | 53 | end |
54 | +puts "another daemon is already running" if daemon.running |
@@ -133,7 +133,7 @@ | ||
133 | 133 | # See the License for the specific language governing permissions and |
134 | 134 | # limitations under the License. |
135 | 135 | # |
136 | -# | |
136 | + | |
137 | 137 | # It's a utility class to process common tasks. |
138 | 138 | # These methods should be tested by the unit test script. |
139 | 139 | # |
@@ -362,37 +362,40 @@ class YaDaemon | ||
362 | 362 | logit "run: return" |
363 | 363 | end |
364 | 364 | |
365 | + ## safelly | |
365 | 366 | def stop |
366 | 367 | logit "stop: called" |
367 | 368 | |
368 | - ## overwrite @stopfile | |
369 | 369 | create_stop_file() |
370 | - | |
370 | + end | |
371 | + | |
372 | + def force_stop | |
371 | 373 | if not check_proc |
372 | - logit "stop: this process has already stopped." | |
374 | + logit "force_stop: this process has already stopped." | |
373 | 375 | return |
374 | 376 | end |
375 | 377 | pid = get_pid |
376 | 378 | ## case 1 |
377 | 379 | if pid == $$ |
380 | + create_stop_file() | |
378 | 381 | exit(0) |
379 | 382 | end |
380 | 383 | ## case 2 |
381 | 384 | if pid > 0 |
382 | 385 | n = Process::kill(15, pid) |
383 | 386 | if n > 0 |
384 | - logit "stop: the terminate signal succesfully sent." | |
387 | + logit "force_stop: the terminate signal succesfully sent." | |
385 | 388 | else |
386 | - logit "stop: failed to sent the terminate signal." | |
389 | + logit "force_stop: failed to sent the terminate signal." | |
387 | 390 | end |
388 | 391 | while check_proc |
389 | 392 | logit "stop: waiting terminate process, pid=#{pid}." |
390 | 393 | sleep 3 |
391 | 394 | end |
392 | 395 | else |
393 | - logit "stop: failed to get my pid number" | |
396 | + logit "force_stop: failed to get my pid number" | |
394 | 397 | end |
395 | - logit "stop: return" | |
398 | + logit "force_stop: return" | |
396 | 399 | end |
397 | 400 | |
398 | 401 | ## change privileges using from run method, but place here for unit test. |