Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow que to be started and create pid file #404

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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
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
Loading