Search code examples
ruby-on-railsdelayed-job

Run delayed jobs after deployed on production server


With the delayed_jobs gem(https://github.com/collectiveidea/delayed_job) in rails, I am able to queue my notifications. But I don't quite understand how can I run the queued jobs on the production server. I knew I can just run

$ rake jobs:work

in the console for the local server. As the documentation said, You can then do the following:

RAILS_ENV=production script/delayed_job start
RAILS_ENV=production script/delayed_job stop

# Runs two workers in separate processes.
RAILS_ENV=production script/delayed_job -n 2 start
RAILS_ENV=production script/delayed_job stop

# Set the --queue or --queues option to work from a particular queue.
RAILS_ENV=production script/delayed_job --queue=tracking start
RAILS_ENV=production script/delayed_job --queues=mailers,tasks start

# Runs all available jobs and the exits
RAILS_ENV=production script/delayed_job start --exit-on-complete
# or to run in the foreground
RAILS_ENV=production script/delayed_job run --exit-on-complete

My question is how to integrate it with my Rails app?I was thinking to create a file called delayed_jobs.rb in config/initializers as:

# in config/initializers/delayed_jobs
script/delayed_job start if Rails.env.production?

But I am not sure if it is the right way to do with it. Thanks


Solution

  • The workers run as separate processes, not as part of your Rails application. The simplest way would be to run the rake task in a screen session to prevent it from quitting when you log out of the terminal session. But there are better ways:

    You would use a system such as monit or God or run the worker script provided by delayed_job. You'll find more information in the answers to this question.