Initialize the Delayed Jobs gem by starting the workers on application start

后端 未结 3 1444
时光取名叫无心
时光取名叫无心 2021-01-15 19:07

I am using Ruby on Rails 3.0.9 and I am trying to setup the delay_job gem. All works if, after rebooting the Apache2 server, I run in the Terminal\\Console following command

3条回答
  •  忘掉有多难
    2021-01-15 19:23

    I don't think you can do that because when you start the '/script/delayed_job', the rails environment will be loaded, causing the 'config/initializers/delayed_job.rb' file to be executed again. You can see this results in an infinite loop. Also each time you invoke rake, for example: 'rake db:migrate', it would initialize delayed_jobs.

    You can hack around it with this:

    if Rails.env.production?
      if(!File.exists?(Rails.root.join('tmp','pids', 'delayed_job.pid')))
        system "echo \"Starting delayed_jobs...\""
        system "./script/delayed_job start &"
      else
        system "echo \"delayed_jobs is running\""
      end
    end
    

    With the '&' the delayed_job script is run in background, on a separate process than rails. Subsequent calls to rake will skip launching delayed_jobs if its already running. You'll still have some problems if, for some reason, that file is not deleted when delayed_jobs terminates.

    The command /script/delayed_job status will detect if that's the case, but you can't run it inside this 'config/initializers/delayed_job.rb' file because it would cause an infinite loop :(

提交回复
热议问题