Skip to content

Commit

Permalink
Add pidfile params
Browse files Browse the repository at this point in the history
  • Loading branch information
jokius committed Oct 19, 2023
1 parent 4e08e84 commit 3966443
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/que/command_line_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ def parse(
) do |p|
options[:wait_period] = p
end

opts.on(
'--pidfile [PATH]',
String,
"Write the PID of this process to the specified file.",
) do |p|
options[:pidfile] = File.expand_path(p)
end
end

parser.parse!(args)
Expand Down
25 changes: 23 additions & 2 deletions lib/que/locker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def initialize(
wait_period: DEFAULT_WAIT_PERIOD,
maximum_buffer_size: DEFAULT_MAXIMUM_BUFFER_SIZE,
worker_priorities: DEFAULT_WORKER_PRIORITIES,
on_worker_start: nil
on_worker_start: nil,
pidfile: nil
)

# Sanity-check all our arguments, since some users may instantiate Locker
Expand Down Expand Up @@ -215,10 +216,16 @@ def initialize(
@connection.wrapped_connection.close
end
end

@pidfile = pidfile
at_exit { delete_pid }
write_pid
end

def stop!
stop; wait_for_stop
stop
wait_for_stop
delete_pid
end

def stop
Expand Down Expand Up @@ -512,5 +519,19 @@ def mark_id_as_locked(id)
"Tried to lock a job that was already locked: #{id}"
end
end

def write_pid
return unless @pidfile

File.open(@pidfile, "w+") do |f|
f.write(Process.pid.to_s)
end
end

def delete_pid
return unless @pidfile

File.delete(@pidfile) if File.exist?(@pidfile)
end
end
end
24 changes: 24 additions & 0 deletions spec/que/locker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ def assert_que_locker_insertion(
locker.stop!
end

describe "with pidfile" do
it "should create and delete pid file, if not exist" do
pidfile = "./spec/temp/pidfile_#{Time.new.to_i}.pid"
locker_settings[:pidfile] = pidfile
locker
assert File.exist?(pidfile)

locker.stop!
refute File.exist?(pidfile)
end

it "should create and delete pid file, if exist" do
pidfile = "./spec/temp/pidfile_#{Time.new.to_i}.pid"
File.open(pidfile, "w+") { |f| f.write "test" }

locker_settings[:pidfile] = pidfile
locker
assert File.exist?(pidfile)

locker.stop!
refute File.exist?(pidfile)
end
end

describe "on startup" do
it "should do batch polls for jobs in its specified queue" do
job1, job2 = BlockJob.enqueue, BlockJob.enqueue
Expand Down

0 comments on commit 3966443

Please sign in to comment.