Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 81 lines (66 sloc) 1.367 kb
9a09b14 @darkleaf first
authored
1 class BaseDaemon
1629a27 @darkleaf refactot
authored
2 attr_accessor :pid, :options
9a09b14 @darkleaf first
authored
3
4 def initialize(options = {})
1629a27 @darkleaf refactot
authored
5 @options = options
9a09b14 @darkleaf first
authored
6 end
7
8 def started?
9 return false unless @pid
10
1629a27 @darkleaf refactot
authored
11 Process.getpgid @pid
9a09b14 @darkleaf first
authored
12 true
13 rescue Errno::ESRCH
14 false
15 end
16
17 def action
18 raise 'Not Implemented'
19 end
20
21 def perform
22 while true do
23 action
24 end
25 end
26
1629a27 @darkleaf refactot
authored
27 def run
9a09b14 @darkleaf first
authored
28 return if started?
29 @started = true
30 @pid = fork do
31 @pid = Process.pid
32
33 STDOUT.reopen File.open("#{@pid}.stout", 'a')
34 STDERR.reopen File.open("#{@pid}.stderr", 'a')
35
36 perform
37 end
38 puts "#{@pid} start"
39 Process.detach(@pid)
40 end
41
42 def stop
43 return unless started?
44 @started = false
45 Process.kill 'QUIT', pid
46 end
47 end
48
49 class Supervisor < BaseDaemon
50 attr_accessor :daemons
1629a27 @darkleaf refactot
authored
51 LOG_FILE = "supervisor.log"
9a09b14 @darkleaf first
authored
52
53 def initialize(options = {})
1629a27 @darkleaf refactot
authored
54 super options
9a09b14 @darkleaf first
authored
55 @daemons = []
56 end
57
58 def action
59 @daemons.each do |daemon|
60 unless daemon.started?
61 old_pid = daemon.pid
1629a27 @darkleaf refactot
authored
62 File.open LOG_FILE, 'a' do |f|
63 daemon.run
64 f.write "damon name: #{daemon.options[:name] || 'Dmn'}\n"
9a09b14 @darkleaf first
authored
65 f.write "daemon died! pid: #{old_pid}\n"
1629a27 @darkleaf refactot
authored
66 f.write "daemon started! pid: #{daemon.pid}\n\n"
9a09b14 @darkleaf first
authored
67 end
68 end
69 end
70 sleep 2
71 end
72 end
73
74
75 class Daemon < BaseDaemon
76 def action
1629a27 @darkleaf refactot
authored
77 exit! 1 if rand(5) == 0
9a09b14 @darkleaf first
authored
78 sleep 3
79 end
80 end
Something went wrong with that request. Please try again.