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

monitor children and re-fork when they exit #615

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 45 additions & 1 deletion lib/delayed/tasks.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
namespace :jobs do
def fork_delayed(worker_index)
worker_name = "delayed_job.#{worker_index}"

worker_pid = fork do
Delayed::Worker.after_fork
worker = Delayed::Worker.new(@worker_options)
worker.name_prefix = worker_name
worker.start
end

puts "started #{worker_name} with pid=#{worker_pid}"

return worker_name, worker_pid
end

desc "Clear the delayed_job queue."
task :clear => :environment do
Delayed::Job.delete_all
end

desc "Start a delayed_job worker."
task :work => :environment_options do
Delayed::Worker.new(@worker_options).start
if @worker_options[:num_processes] == 1
Delayed::Worker.new(@worker_options).start
else
stop = false

Signal.trap 'TERM' do
stop = true
end

Delayed::Worker.before_fork

workers = {}
@worker_options[:num_processes].times do |worker_index|
worker_name, worker_pid = fork_delayed(worker_index)
workers[worker_pid] = worker_name
end

worker_index = @worker_options[:num_processes]

while true
worker_pid = Process.wait()
puts "worker #{workers[worker_pid]} exited - #{$?.to_s }"
break if stop

worker_name, worker_pid = fork_delayed(worker_index)
workers[worker_pid] = worker_name

worker_index = worker_index + 1
end
end
end

desc "Start a delayed_job worker and exit when all available jobs are complete."
Expand Down