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

Fix orphan workers on restart #1090

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
29 changes: 29 additions & 0 deletions lib/delayed/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def daemonize # rubocop:disable PerceivedComplexity
end
# rubocop:enable GuardClause
else
stop_start_if_restart
worker_count.times do |worker_index|
process_name = worker_count == 1 ? 'delayed_job' : "delayed_job.#{worker_index}"
run_process(process_name, @options)
Expand Down Expand Up @@ -144,6 +145,18 @@ def run(worker_name = nil, options = {})

private

def args_command
@args.find { |arg| args_command_options.include?(arg) }
end

def args_command_index
@args.index(args_command)
end

def args_command_options
%w[start stop restart run]
end

def parse_worker_pool(pool)
@worker_pools ||= []

Expand All @@ -165,6 +178,22 @@ def rails_logger_defined?
defined?(::Rails.logger)
end

def stop_all_workers
original_command = args_command
@args[args_command_index] = 'stop'
Dir.glob("#{@options[:pid_dir]}/delayed_job*").each do |file_path|
process_name = File.basename(file_path, '.*')
run_process(process_name, @options)
end
@args[args_command_index] = original_command
end

def stop_start_if_restart
return unless args_command == 'restart'
stop_all_workers
@args[args_command_index] = 'start'
end

def exit_with_error_status
exit 1
end
Expand Down
24 changes: 24 additions & 0 deletions spec/delayed/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@
end
end

describe 'restart' do
before(:each) do
allow(Dir).to receive(:glob).with("#{run_process_options[:pid_dir]}/delayed_job*").and_return([pid_filepath])
allow(FileUtils).to receive(:mkdir_p).with(run_process_options[:pid_dir]).once
end
let(:command) { Delayed::Command.new(['restart']) }
let(:pid_filepath) { "#{run_process_options[:pid_dir]}/delayed_job.orig.pid" }
let(:run_process_options) do
{:log_dir => './log', :pid_dir => './tmp/pids', :quiet => true}
end

it 'sends stop for all workers then start' do
expect(command).to receive(:run_process).with('delayed_job.orig', run_process_options) do
expect(command.instance_variable_get('@args')).to eql(['stop'])
end

expect(command).to receive(:run_process).with('delayed_job', run_process_options) do
expect(command.instance_variable_get('@args')).to eql(['start'])
end

command.daemonize
end
end

describe 'run' do
it 'sets the Delayed::Worker logger' do
expect(Delayed::Worker).to receive(:logger=).with(logger)
Expand Down