Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
web: bundle exec thrust bin/start-app
redis: redis-server config/redis.conf
workers: FORK_PER_JOB=false INTERVAL=0.1 bundle exec resque-pool
workers: exec env FORK_PER_JOB=false INTERVAL=0.1 bundle exec resque-pool --term-graceful-wait
26 changes: 21 additions & 5 deletions bin/boot
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ require "yaml"
class ProcessMonitor
SIGNALS = %w[ INT TERM CLD ]

# The other processes use these, so they stop only once the others have exited
SERVICES = %w[ redis ]

def initialize(procfile)
@procs = process_list(procfile)
@services, @apps = @procs.partition { |process| SERVICES.include?(process.name) }
handle_signals

@procs.each &:start
@procs.each &:wait
@apps.each &:wait

@services.each &:terminate
@services.each &:wait
end

private
Expand All @@ -22,20 +29,29 @@ class ProcessMonitor

def handle_signals
SIGNALS.each do |signal|
Signal.trap(signal) do
@procs.each &:terminate
end
Signal.trap(signal) { stop }
end
end

# Only once: a second TERM tells resque-pool to stop waiting for its workers
def stop
unless @stopping
@stopping = true
@apps.each &:terminate
end
end
end

class MonitoredProcess
attr_reader :name

def initialize(name, cmd)
@name, @cmd = name, cmd
end

# In a process group of its own, so a Ctrl-C in a terminal reaches only bin/boot, which stops everything in order
def start
@pid = Process.spawn(@cmd)
@pid = Process.spawn(@cmd, pgroup: true)
end

def wait
Expand Down
2 changes: 1 addition & 1 deletion bin/start-app
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

rm -f tmp/pids/server.pid
./bin/rails db:prepare
./bin/rails server
exec ./bin/rails server
105 changes: 105 additions & 0 deletions test/bin/boot_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require "test_helper"

class BootScriptTest < ActiveSupport::TestCase
# Stands in for a Procfile process: records when it starts, when it's asked to stop,
# and how many TERMs it got by the time it exits. Exits on its own once exit-NAME exists.
FAKE_PROCESS = <<~RUBY
name, stop_delay = ARGV
terms = 0
trap("TERM") { terms += 1 }

record = ->(file, line) { File.open(file, "a") { |io| io.puts line } }
record.call "pids", Process.pid
record.call "events", "\#{name} started"

sleep 0.01 until terms > 0 || File.exist?("exit-\#{name}")

if terms > 0
record.call "events", "\#{name} stopping"
sleep stop_delay.to_f
end
record.call "events", "\#{name} stopped"
record.call "events", "\#{name} got \#{terms} TERM"
RUBY

test "stops redis once the processes that use it have exited" do
events = boot(redis: 0, web: 0.2, workers: 0.4) { |pid| Process.kill "TERM", pid }

assert_before events, "web stopped", "redis stopping"
assert_before events, "workers stopped", "redis stopping"
end

test "asks each process to stop only once while the others exit" do
events = boot(redis: 0, web: 0, workers: 0.5) { |pid| Process.kill "TERM", pid }

assert_includes events, "workers got 1 TERM"
assert_includes events, "redis got 1 TERM"
end

test "stops the others, redis last, when one process exits on its own" do
events = boot(redis: 0, web: 0, workers: 0.2) { |pid, dir| FileUtils.touch File.join(dir, "exit-web") }

assert_includes events, "web got 0 TERM"
assert_before events, "workers stopped", "redis stopping"
end

test "stops in the same order when a terminal's Ctrl-C reaches its whole process group" do
events = boot(redis: 0, web: 0.2, workers: 0.4) { |pid| Process.kill "INT", -pid }

assert_before events, "web stopped", "redis stopping"
assert_before events, "workers stopped", "redis stopping"
end

private
def assert_before(events, earlier, later)
assert_includes events, earlier
assert_includes events, later
assert_operator events.index(earlier), :<, events.index(later), "#{earlier.inspect} should come before #{later.inspect} in #{events}"
end

def boot(stop_delays)
Dir.mktmpdir do |dir|
File.write File.join(dir, "fake_process.rb"), FAKE_PROCESS
File.write File.join(dir, "Procfile"), stop_delays.map { |name, delay|
"#{name}: exec #{RbConfig.ruby} fake_process.rb #{name} #{delay}"
}.join("\n")

pid = Process.spawn(RbConfig.ruby, Rails.root.join("bin/boot").to_s, chdir: dir, pgroup: true)

exited = false
begin
assert wait_until { lines_in(dir, "events").count { |event| event.end_with?(" started") } == stop_delays.size }, "processes didn't start"
yield pid, dir
exited = wait_until { Process.wait(pid, Process::WNOHANG) }
assert exited, "bin/boot didn't exit"

lines_in(dir, "events")
ensure
# Once bin/boot has exited it has reaped every process; otherwise they're left to kill
kill_all [ pid, *lines_in(dir, "pids").map(&:to_i) ] unless exited
end
end
end

def lines_in(dir, name)
path = File.join(dir, name)
File.exist?(path) ? File.readlines(path, chomp: true) : []
end

def wait_until(timeout: 10)
deadline = Time.now + timeout
until (result = yield) || Time.now > deadline
sleep 0.01
end
result
end

def kill_all(pids)
pids.each do |pid|
Process.kill "KILL", pid
rescue Errno::ESRCH
end
Process.wait pids.first
rescue Errno::ECHILD
end
end
Loading