Search code examples
rubyruby-on-rails-3delayed-jobproduction-environment

How to Invoke delayed_job_active_record (rake jobs:work) externally production env


I have a delayed job which is called whenever a user posts a song, this job sends an activity post to the users Facebook wall. Here is the code for the job and calling it in the controller.

I am using the 'delayed_job_active_record' gem as seen in Ryan Bates Facebook Open Graph Railscast

user.rb

def self.post_song(user_id, song_url)
    user = User.find(user_id)
    user.facebook.put_connections("me", "virtual_piano:post", song: song_url)
  end

posts_controller.rb

(upon successful save)
        User.delay.post_song(current_user.id, post_url(@post))

This job is run in development mode with the standard rake jobs:work.

I have seen some people using RAILS_ENV=production script/delayed_job start with delayed job How could i invoke this method in production and keep it running in the background?

many thanks!


Solution

  • rake jobs:work in delayed_job is like the rails s command for rails - it runs in the main process of the console (shell) that executed the command. Meaning it stops running when the console(shell) containing it stops running (i.e. you log out).

    You'll want to run RAILS_ENV=production script/delayed_job start instead - that starts the delayed_job queue as a daemon (background process) like rails s -n does for rails.