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

后端 未结 3 1386
时光取名叫无心
时光取名叫无心 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 :(

    0 讨论(0)
  • 2021-01-15 19:30

    Try using this code:

    system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} stop"
    system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"
    

    Testing it in the console produced this output which indicates it should work:

    Loading development environment (Rails 3.0.9)
    ruby-1.9.2-p290 :001 > Rails.root.join('script','delayed_job')
     => #<Pathname:/home/devin/testsoapp/script/delayed_job> 
    ruby-1.9.2-p290 :002 > "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"
     => "RAILS_ENV=production /home/devin/testsoapp/script/delayed_job -n 2 start" 
    ruby-1.9.2-p290 :003 > 
    
    0 讨论(0)
  • 2021-01-15 19:30

    I had a problem where the initializing delayed_job process was running though its initializers before creating the pid file (i.e. delayed_job.pid in case of a single worker and delayed_job.0.pid delayed_job.1.pid etc. in case of many workers)

    So I resorted to creating my own lock file as such:

    Delayed::Worker.destroy_failed_jobs = false
    Delayed::Worker.sleep_delay = 2
    Delayed::Worker.max_attempts = 5
    Delayed::Worker.max_run_time = 4.hour
    Delayed::Worker.delay_jobs = !Rails.env.test?
    
    workers = 2
    
    
    if Rails.env.production? || Rails.env.development?
      # Check if the delayed job process is already running
      # Since the process loads the rails env, this file will be called over and over
      # Unless this condition is set.
      pids = Dir.glob(Rails.root.join('tmp','pids','*'))
    
      system "echo \"delayed_jobs INIT check\""
      if pids.select{|pid| pid.start_with?(Rails.root.join('tmp','pids','delayed_job.init').to_s)}.empty?
    
        f = File.open(Rails.root.join('tmp','pids','delayed_job.init'), "w+") 
        f.write(".")
        f.close
        system "echo \"Restatring delayed_jobs...\""
        system "RAILS_ENV=#{Rails.env} #{Rails.root.join('bin','delayed_job')} stop"
        system "RAILS_ENV=#{Rails.env} #{Rails.root.join('bin','delayed_job')} -n #{workers} start"
        system "echo \"delayed_jobs Workers Initiated\""
        File.delete(Rails.root.join('tmp','pids','delayed_job.init')) if File.exist?(Rails.root.join('tmp','pids','delayed_job.init'))
    
      else
        system "echo \"delayed_jobs is running\""
      end
    end
    

    file: config\initializers\delayed_job.rb

    Note: will not work for windows!

    0 讨论(0)
提交回复
热议问题