Skip to content
Browse files

refactot

  • Loading branch information...
1 parent 554ce22 commit 1629a271d19fa77fe4c4f484ab55479f3df168f4 @darkleaf committed May 22, 2012
Showing with 14 additions and 14 deletions.
  1. +11 −11 classes.rb
  2. +3 −3 run.rb
View
22 classes.rb
@@ -1,14 +1,14 @@
class BaseDaemon
- attr_accessor :pid
+ attr_accessor :pid, :options
def initialize(options = {})
- @options = options
+ @options = options
end
def started?
return false unless @pid
- Process.getpgid( @pid )
+ Process.getpgid @pid
true
rescue Errno::ESRCH
false
@@ -24,13 +24,12 @@ def perform
end
end
- def run(do_exit = true)
+ def run
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')
@@ -49,21 +48,22 @@ def stop
class Supervisor < BaseDaemon
attr_accessor :daemons
+ LOG_FILE = "supervisor.log"
def initialize(options = {})
- super
+ super options
@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
+ File.open LOG_FILE, 'a' do |f|
+ daemon.run
+ f.write "damon name: #{daemon.options[:name] || 'Dmn'}\n"
f.write "daemon died! pid: #{old_pid}\n"
- f.write "daemon started! pid: #{daemon.pid}\n"
+ f.write "daemon started! pid: #{daemon.pid}\n\n"
end
end
end
@@ -74,7 +74,7 @@ def action
class Daemon < BaseDaemon
def action
- exit!1 if rand(5) == 0
+ exit! 1 if rand(5) == 0
sleep 3
end
end
View
6 run.rb
@@ -1,8 +1,8 @@
load 'classes.rb'
s = Supervisor.new
-s.daemons << Daemon.new
-s.daemons << Daemon.new
-s.daemons << Daemon.new
+s.daemons << Daemon.new(:name => 'First')
+s.daemons << Daemon.new(:name => 'Second')
+s.daemons << Daemon.new(:name => 'Kamikadze')
s.run

0 comments on commit 1629a27

Please sign in to comment.
Something went wrong with that request. Please try again.