Permalink
Browse files

Add restarting in cluster mode

  • Loading branch information...
1 parent 508b235 commit 221a26a20f53a93228cb5b4c6967e8782f501d9d @evanphx evanphx committed Aug 4, 2012
Showing with 60 additions and 21 deletions.
  1. +0 −1 bin/puma
  2. +37 −17 lib/puma/cluster_cli.rb
  3. +9 −3 lib/puma/configuration.rb
  4. +14 −0 lib/puma/events.rb
View
@@ -7,7 +7,6 @@ require 'puma/cli'
require 'puma/cluster_cli'
if ARGV[0] == "cluster"
- ARGV.shift
cli = Puma::ClusterCLI.new ARGV
else
cli = Puma::CLI.new ARGV
@@ -18,21 +18,13 @@ def setup_options
@parser.banner = "puma cluster <options> <rackup file>"
end
- class PidEvents < Events
- def log(str)
- super "[#{$$}] #{str}"
- end
-
- def write(str)
- super "[#{$$}] #{str}"
- end
-
- def error(str)
- super "[#{$$}] #{str}"
- end
+ def parse_options
+ @argv.shift if @argv.first == "cluster"
+ super
end
def worker
+ $0 = "puma cluster worker"
Signal.trap "SIGINT", "IGNORE"
@suicide_pipe.close
@@ -134,6 +126,25 @@ def run
write.write "!"
end
+ stop = false
+
+ begin
+ Signal.trap "SIGUSR2" do
+ @restart = true
+ stop = true
+ write.write "!"
+ end
+ rescue Exception
+ end
+
+ begin
+ Signal.trap "SIGTERM" do
+ stop = true
+ write.write "!"
+ end
+ rescue Exception
+ end
+
# Used by the workers to detect if the master process dies.
# If select says that @check_pipe is ready, it's because the
# master has exited and @suicide_pipe has been automatically
@@ -143,18 +154,27 @@ def run
spawn_workers
- log "Use Ctrl-C to stop"
+ log "* Use Ctrl-C to stop"
begin
- while true
- IO.select([read], nil, nil, 5)
- check_workers
+ while !stop
+ begin
+ IO.select([read], nil, nil, 5)
+ check_workers
+ rescue Interrupt
+ stop = true
+ end
end
- rescue Interrupt
+
stop_workers
ensure
delete_pidfile
end
+
+ if @restart
+ log "* Restarting..."
+ restart!
+ end
end
end
end
@@ -156,6 +156,11 @@ def bind(url)
@options[:binds] << url
end
+ # Set the environment in which the Rack's app will run.
+ def environment(environment)
+ @options[:environment] = environment
+ end
+
# Code to run before doing a restart. This code should
# close logfiles, database connections, etc.
#
@@ -210,9 +215,10 @@ def state_path(path)
@options[:state] = path.to_s
end
- # Set the environment in which the Rack's app will run.
- def environment(environment)
- @options[:environment] = environment
+ # *Cluster mode only* How many worker processes to run.
+ #
+ def workers(count)
+ @options[:workers] = count.to_i
end
end
end
View
@@ -68,4 +68,18 @@ def self.strings
Events.new StringIO.new, StringIO.new
end
end
+
+ class PidEvents < Events
+ def log(str)
+ super "[#{$$}] #{str}"
+ end
+
+ def write(str)
+ super "[#{$$}] #{str}"
+ end
+
+ def error(str)
+ super "[#{$$}] #{str}"
+ end
+ end
end

0 comments on commit 221a26a

Please sign in to comment.