Skip to content

Instantly share code, notes, and snippets.

@svoop
Created May 18, 2023 17:32
Show Gist options
  • Save svoop/12de6e3f7a447dc0e036ce1ca35d11ba to your computer and use it in GitHub Desktop.
Save svoop/12de6e3f7a447dc0e036ce1ca35d11ba to your computer and use it in GitHub Desktop.
Rake task for Delayed::Job to launch multiple monitored workers in a container context
namespace :jobs do
desc "Start one or more monitored delayed_job workers (as per environment)"
task :workmulti => :environment do
daemons_count = Rails.env.production? ? 5 : 1
pids = []
loop do
pids.delete_if { !(Process.kill(0, _1) rescue false) }
(daemons_count - pids.count).times do
pids << fork { Delayed::Worker.new.start }
Process.detach pids.last
end
sleep 60
end
ensure
pids.each { Process.kill('TERM', _1) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment