Skip to content

Instantly share code, notes, and snippets.

@contentfree
Last active December 16, 2015 16:20
Show Gist options
  • Save contentfree/5462799 to your computer and use it in GitHub Desktop.
Save contentfree/5462799 to your computer and use it in GitHub Desktop.
Daemonizing DelayedJob from a rake task. Useful for using DelayedJob outside of Rails (Hizzah Padrino!) Note: Requires rake >= 10.1 for TaskArguments#extras
namespace :jobs do
desc <<-EOD
Start delayed_job worker daemon. Requires passing start|stop|restart|reload|run|zap|status
to rake via `rake jobs:daemon[start]`. To pass arguments to Delayed::Command, call rake
like `rake jobs:daemon["--queue=something start"]` (Note the quotes wrapping the arguments!).
Available flags are: queues queue min_priority max_priority number_of_workers pid-dir
identifier monitor sleep-delay read-ahead prefix exit-on-complete
EOD
task :daemon, [:argv] => :environment do |t, args|
# If we have anything in args.extras we need to combined it with args[:argv]
# This lets us handle comma-separated values for --queues.
fake_argv = [args[:argv], args.extras].flatten.compact.join(',')
require 'delayed/command'
Delayed::Command.new(Shellwords.shellwords fake_argv).daemonize
end
end
@contentfree
Copy link
Author

You have to do a few other things to get it to work with Padrino. Namely, you have to fake Rails.root: Put module Rails; def self.root; Padrino.root; end; end in Padrino's boot.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment