Search code examples
ruby-on-railsherokudelayed-jobforemanprocfile

How do you run multiple DelayedJob workers on a single Heroku dyno?


I am having trouble getting my dynos to run multiple delayed job worker processes.

My Procfile looks like this:

worker: bundle exec script/delayed_job -n 3 start

and my delayed_job script is the default provided by the gem:

#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize

When I try to run this either locally or on a Heroku dyno it exits silently and I can't tell what is going on.

foreman start
16:09:09 worker.1 | started with pid 75417
16:09:15 worker.1 | exited with code 0
16:09:15 system   | sending SIGTERM to all processes
SIGTERM received

Any help with either how to debug the issue or suggestions about other ways to go about running multiple workers on a single dyno it would be greatly appreciated.


Solution

  • You can use foreman to start multiple processes on the same dyno.

    First, add foreman to your Gemfile.

    Then add a worker line to your Procfile:

    worker: bundle exec foreman start -f Procfile.workers
    

    Create a new file called Procfile.workers which contains:

    dj_worker: bundle exec rake jobs:work
    dj_worker: bundle exec rake jobs:work
    dj_worker: bundle exec rake jobs:work
    

    That will start 3 delayed_job workers on your worker dyno.