Delayed Jobs with Heroku

Ademar Tutor | August 06, 2016




There are instances where your application needs to process jobs on the background in order to give a quick response to the user. An example of which is when you are sending emails or accessing 3rd party APIs. If your app is deployed on Heroku, then you can use Delayed Jobs.

Use Puma

Make sure that you use Puma. To install Puma, add this to your Gemfile:

gem 'puma'

Setup a Profile on your application. Should be located on the same folder as your Gemfile. Add these configs to your Procfile:

web: bundle exec puma -C config/puma.rb
worker: rake jobs:work

Now you can 'bundle install'.

Install Delayed Job

On your Gemfile, add this:

gem 'delayed_job_active_record'

Run bundle install

The generate delayed jobs migration:

rails g delayed_job:active_record
rake db:migrate

After doing you can now use delayed jobs.

# without delayed_job
@user.activate!(@device)

# with delayed_job
@user.delay.activate!(@device)

Sending notifications:

# without delayed_job
Notifier.signup(@user).deliver

# with delayed_job
Notifier.delay.signup(@user)

# with delayed_job running at a specific time
Notifier.delay(run_at: 5.minutes.from_now).signup(@user)

Now you can use Delayed Jobs to any task by adding '.delay' to the method.

Good luck on building your app!