-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a9132b
commit e9be407
Showing
5 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module GoodJob | ||
# | ||
# Manages daemonization of the current process. | ||
# | ||
class Daemon | ||
# The path of the generated pidfile. | ||
# @return [Pathname,String] | ||
attr_reader :pidfile | ||
|
||
# @param pidfile [Pathname,String] Pidfile path | ||
def initialize(pidfile:) | ||
@pidfile = pidfile | ||
end | ||
|
||
# Daemonizes the current process and writes out a pidfile. | ||
def daemonize | ||
check_pid | ||
Process.daemon | ||
write_pid | ||
end | ||
|
||
private | ||
|
||
def write_pid | ||
File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) { |f| f.write(Process.pid.to_s) } | ||
at_exit { File.delete(pidfile) if File.exist?(pidfile) } | ||
rescue Errno::EEXIST | ||
check_pid | ||
retry | ||
end | ||
|
||
def delete_pid | ||
File.delete(pidfile) if File.exist?(pidfile) | ||
end | ||
|
||
def check_pid | ||
case pid_status(pidfile) | ||
when :running, :not_owned | ||
abort "A server is already running. Check #{pidfile}" | ||
when :dead | ||
File.delete(pidfile) | ||
end | ||
end | ||
|
||
def pid_status(pidfile) | ||
return :exited unless File.exist?(pidfile) | ||
|
||
pid = ::File.read(pidfile).to_i | ||
return :dead if pid.zero? | ||
|
||
Process.kill(0, pid) # check process status | ||
:running | ||
rescue Errno::ESRCH | ||
:dead | ||
rescue Errno::EPERM | ||
:not_owned | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe GoodJob::Daemon, skip_if_java: true do | ||
let(:pidfile) { Rails.application.root.join('tmp/pidfile.pid') } | ||
let(:daemon) { described_class.new(pidfile: pidfile) } | ||
|
||
before do | ||
FileUtils.mkdir_p Rails.application.root.join('tmp') | ||
allow(Process).to receive(:daemon) | ||
allow(daemon).to receive(:at_exit) | ||
end | ||
|
||
after do | ||
File.delete(pidfile) | ||
end | ||
|
||
describe '#daemonize' do | ||
it 'calls Process.daemon' do | ||
daemon.daemonize | ||
expect(Process).to have_received :daemon | ||
end | ||
|
||
it 'writes a pidfile' do | ||
expect do | ||
daemon.daemonize | ||
end.to change { Pathname.new(pidfile).exist? }.from(false).to(true) | ||
end | ||
|
||
context 'when a pidfile already exists' do | ||
before do | ||
File.open(pidfile, "w") { |f| f.write(Process.pid) } | ||
end | ||
|
||
it 'aborts with a message' do | ||
expect { daemon.daemonize }.to output("A server is already running. Check #{pidfile}\n").to_stderr.and raise_error SystemExit | ||
end | ||
end | ||
end | ||
end |