Using Delayed Job (DJ) to Handle Background Tasks

What is delayed_job?

Delayed_job lets you run background tasks with your Rails application. It uses your database as a queue to process background jobs. If your app has a high database load, using delayed_job may not be a good background queuing library for your project and you should have a look at Sidekiq or Resque.

Adding delayed_job to your Project

To get started using delayed_job you need to configure your application.

# With MySQL® or PostgreSQL®
gem 'delayed_job_active_record'

# OR if you're using MongoDB®
gem 'delayed_job_mongoid'

Then, run bundle install to install the gem.

If you are using MySQL® or PostgreSQL®, you also need to apply migrations to your database:

rails generate delayed_job:active_record
rake db:migrate

That’s it, your application is ready! You can find more information on their GitHub page.

Adding ‘delayedjob’ container type in your Procfile

Once delayed_job has been installed, you need to add a new type of containers in your application which will actually start delayed_job. Add the following line to the Procfile of your project. Create the file if it doesn’t exist.

delayedjob: bin/delayed_job run

All you have to do now is to write your jobs by following delayed_job documentation and deploy your code:

git add Gemfile Gemfile.lock Procfile
git commit -m "Add delayed job"
git push scalingo master

The process won’t be started directly, but by going to the dashboard of your application or by running the scalingo ps command, you’ll see that the new container type is present. Scale it to 1 to start it.

Suggest edits

Using Delayed Job (DJ) to Handle Background Tasks

©2024 Scalingo