Search code examples
ruby-on-railsruby-on-rails-3herokudelayed-jobworker

Delay Job Worker keeps turning off?


New to Rails and very new to Delayed Jobs.

Got one that's supposed to be triggered after 5 minutes. I finally got it to work so that if I run

rake jobs:work

in my terminal, the job starts up and works correctly. If I CTRL-C and exit that action in my terminal, the delayed job stops working correctly. This is one thing on my local server and another on Heroku, where I have to start up the delayed job using

heroku run rake jobs:work

I looked into the new Heroku toolbelt and downloaded the gem they suggest for worker maintenance, foreman, but when I run "foreman start", I get this error

ERROR: procfile does not exist

I don't know what a procfile is, I'm afraid of breaking things after spending pretty much a day debugging my delayed_jobs actions, and I want to do this right to make sure it works instead of figuring out some hacky fix that breaks down later -- so I figured I should ask this question, however obnoxiously vague it may be.

Should I be using foreman for this? Or workless? (Saw that in another SO question). Where's my procfile? Should I do anything with it?

Thanks,

Sasha


Solution

  • You should be using a procfile to set up your Heroku processes, this is the standard method that Heroku uses to define and control the processes.

    If you haven't utilised a procfile to this point everything will probably still work as Heroku adds some default processes when you push a Rails app, including both the web and worker processes. The default worker process is set to delayed job.

    Foreman was developed in order to set up your local machine to use the same approach but, unlike the Heroku service, Foreman actually requires a procfile to be present to control the services that are started when Foreman is started as it doesn't know how to setup defaults.

    I would suggest creating a procfile, placed in the root directory of your project, to ensure that your processes are set up and operating in the same manner on your local machine as on Heroku. If you want to mimic what Heroku sets up automatically you add the following to the procfile depending on whether you are using the Thin web server (which Heroku recommends) or not.

    With Thin in your gemfile:

    web: bundle exec thin start -R config.ru -e $RACK_ENV -p $PORT
    worker: bundle exec rake jobs:work
    

    Without a special web server (eg you are using webrick, the rails default):

    web: bundle exec rails server -p $PORT
    worker: bundle exec rake jobs:work
    

    Once this file is in place you can run foreman on your local machine and it will start your web server and delayed_job workers automatically.

    Running through this process will only impact starting delayed_job on the local machine. As you are running the exact same command bundle exec rake jobs:work as you are currently using there should be no impact on your dj actions in either locally or on Heroku. Obviously some testing is required to make suer this is actually the case.

    Workless is designed to scale workers on Heroku so that you don't have to pay for them when there is no work available. It has no bearing on the procfile or defining how to actually start a dj worker process.