Skip to content

Commit

Permalink
Added method 'run' besides 'start' so daemonization is not mandatory.
Browse files Browse the repository at this point in the history
Removed exit call on successful daemonization so that the launcher process doesn't die.
  • Loading branch information
dcadenas committed Apr 11, 2010
1 parent 0970128 commit 49c4f3e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
8 changes: 7 additions & 1 deletion README.rdoc
Expand Up @@ -30,9 +30,15 @@ Let's see an example using the Mac 'say' command.

#here we can do whatever we want to exit gracefully
`say bye`


#here we are using #start to daemonize. We could have used #run to just block and then send the INT signal with ctrl-c to stop
end.start

To kill the server just run:
puts "I'm the launcher that forked off master, bye bye"
puts "To kill the server just do: kill -s QUIT `cat preforker.pid`

Remember that to kill the server you need to:

kill -s QUIT `cat preforker.pid`

Expand Down
9 changes: 5 additions & 4 deletions examples/ping_pong.rb
Expand Up @@ -7,7 +7,7 @@
Preforker.new(:timeout => 10) do |master|
#this block is only run from each worker (10 by default)

#here you should write the code that is needed to be ran each time a fork is created, initializations, etc.
#here you should write the code that is needed to be ran once each time a fork is created, initializations, etc.
`say hi, I\\'m a worker`

#here you could IO.select a socket, run an EventMachine service (see example), or just run worker loop
Expand All @@ -19,8 +19,9 @@

#here we can do whatever we want when exiting gracefully
`say bye`
end.start

puts "This will never run"
#we can use run instead of start to run the server without daemonizing
end.start

#to kill the server just do: kill -s QUIT `cat preforker.pid`
puts "I'm the launching process that forked to create master, I did my job. Enjoy the noisy ping pong championship, bye bye!"
puts "To kill the server just do: kill -s QUIT `cat preforker.pid`
35 changes: 20 additions & 15 deletions lib/preforker.rb
Expand Up @@ -30,27 +30,33 @@ def initialize(options = {}, &worker_block)
$0 = "#@app_name Master"
end

def start
launch do |ready_write|
$stdin.reopen("/dev/null")
set_stdout_path(@options[:stdout_path])
set_stderr_path(@options[:stderr_path])
def run(ready_write = nil)
$stdin.reopen("/dev/null")
set_stdout_path(@options[:stdout_path])
set_stderr_path(@options[:stderr_path])

logger.info "Master started"
logger.info "Master started"

pid_path = @options[:pid_path] || "./#{@app_name.downcase}.pid"
@pid_manager = PidManager.new(pid_path)
@signal_processor = SignalProcessor.new(self)
pid_path = @options[:pid_path] || "./#{@app_name.downcase}.pid"
@pid_manager = PidManager.new(pid_path)
@signal_processor = SignalProcessor.new(self)

spawn_missing_workers do
ready_write.close
end
spawn_missing_workers do
ready_write.close if ready_write
end

#tell parent we are ready
#tell parent we are ready
if ready_write
ready_write.syswrite($$.to_s)
ready_write.close rescue nil
end

@signal_processor.start_signal_loop
@signal_processor.start_signal_loop
end

def start
launch do |ready_write|
run(ready_write)
end
end

Expand All @@ -77,7 +83,6 @@ def launch(&block)
exit!(1)
else
puts "Server started successfuly"
exit(0)
end
end

Expand Down
6 changes: 4 additions & 2 deletions preforker.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{preforker}
s.version = "0.1.0"
s.version = "0.1.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Daniel Cadenas"]
s.date = %q{2010-04-02}
s.date = %q{2010-04-11}
s.description = %q{A gem to easily create prefork servers.}
s.email = %q{dcadenas@gmail.com}
s.extra_rdoc_files = [
Expand All @@ -25,6 +25,7 @@ Gem::Specification.new do |s|
"VERSION",
"examples/amqp.rb",
"examples/amqp_client.rb",
"examples/em_echo_server.rb",
"examples/ping_pong.rb",
"lib/preforker.rb",
"lib/preforker/pid_manager.rb",
Expand Down Expand Up @@ -52,6 +53,7 @@ Gem::Specification.new do |s|
"spec/support/integration.rb",
"examples/amqp.rb",
"examples/amqp_client.rb",
"examples/em_echo_server.rb",
"examples/ping_pong.rb"
]

Expand Down

0 comments on commit 49c4f3e

Please sign in to comment.