Rails delayed job not working

前端 未结 2 748
夕颜
夕颜 2020-12-21 19:44

My delayed job is not working. I am trying to create an background task for a rake file using a delayed job that should run every 15 minute. I have copied my rake tasks in m

相关标签:
2条回答
  • 2020-12-21 20:28

    If I understand correctly what you're trying to do, then you should have the following:

    scraper.rb

    
    require 'mechanize'
    
    class Scraper
      def perform
        [:iqmedier, :euroads, :mikkelsen, :orville].each{|a| send(a)}
      end
    
      def iqmedier
        # some code
      end
    
      def mikkelsen
        # some code
      end
    
      def orville
        # some code
      end
    
    end
    

    in your controller:

    
    require 'delayed_job'
    class MyController < ApplicationController
      def runall
        Delayed::Job.enqueue(Scraper.new, :run_at => 5.minutes.from_now)
      end
    end
    
    

    For adding jobs from outside of rails, you can write something similar and then run it with rails runner:

    
    require 'scraper'
    Delayed::Job.enqueue Scraper.new
    Delayed::Job.enqueue Scraper.new, :run_at => 15.minutes.from_now
    Delayed::Job.enqueue Scraper.new, :run_at => 30.minutes.from_now
    Delayed::Job.enqueue Scraper.new, :run_at => 45.minutes.from_now
    
    0 讨论(0)
  • 2020-12-21 20:42

    If you just want to run code every X minute/hour, than you don't need delayed_job for that. That would be kind of overkill and over engineered. Just use this code here:

        until 2 < 1 do 
            if Time.now.min % 15 == 0
    
                puts "Execute your code here!"
    
                if Time.now.min % 15 == 0
                    sleep(60)
                end
            end
        end
    

    Put the code into Rakefile, make a task out of it and deploy it on a heroku worker dyno. That will work. Here is a complete tutorial for that: http://robert-reiz.com/2012/06/12/cron-jobs-on-heroku/.

    For that solution you even don't need Herokus Cron or Scheduler Add-On. And you don't need any additional GEMs like delayed_job. Pragmatic solution. Keep it slim ;-)

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