Permalink
Browse files

first

  • Loading branch information...
0 parents commit 9a09b1416e74b8fdf4eeab1b1a95c9ccb694b62b @darkleaf committed May 22, 2012
Showing with 92 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +8 −0 run.rb
  3. +80 −0 test.rb
4 .gitignore
@@ -0,0 +1,4 @@
+supervisor.log
+*.txt
+*.st*
+*~
8 run.rb
@@ -0,0 +1,8 @@
+load 'test.rb'
+
+s = Supervisor.new
+s.daemons << Daemon.new
+s.daemons << Daemon.new
+s.daemons << Daemon.new
+
+s.run
80 test.rb
@@ -0,0 +1,80 @@
+class BaseDaemon
+ attr_accessor :pid
+
+ def initialize(options = {})
+ @options = options
+ end
+
+ def started?
+ return false unless @pid
+
+ Process.getpgid( @pid )
+ true
+ rescue Errno::ESRCH
+ false
+ end
+
+ def action
+ raise 'Not Implemented'
+ end
+
+ def perform
+ while true do
+ action
+ end
+ end
+
+ def run(do_exit = true)
+ return if started?
+ @started = true
+ @pid = fork do
+ @pid = Process.pid
+
+ #STDIN.reopen File.open("#{@pid}.stdin", 'w')
+ STDOUT.reopen File.open("#{@pid}.stout", 'a')
+ STDERR.reopen File.open("#{@pid}.stderr", 'a')
+
+ perform
+ end
+ puts "#{@pid} start"
+ Process.detach(@pid)
+ end
+
+ def stop
+ return unless started?
+ @started = false
+ Process.kill 'QUIT', pid
+ end
+end
+
+class Supervisor < BaseDaemon
+ attr_accessor :daemons
+
+ def initialize(options = {})
+ super
+ @daemons = []
+ end
+
+ def action
+ @daemons.each do |daemon|
+ file_name = "supervisor.log"
+ unless daemon.started?
+ old_pid = daemon.pid
+ File.open file_name, 'a' do |f|
+ daemon.run false
+ f.write "daemon died! pid: #{old_pid}\n"
+ f.write "daemon started! pid: #{daemon.pid}\n"
+ end
+ end
+ end
+ sleep 2
+ end
+end
+
+
+class Daemon < BaseDaemon
+ def action
+ exit!1 if rand(5) == 0
+ sleep 3
+ end
+end

0 comments on commit 9a09b14

Please sign in to comment.