Rails推迟了工作不工作

时间:2011-04-15 09:34:54

标签: ruby delayed-job

我的延迟工作无效。我正在尝试使用应该每15分钟运行一次的延迟作业为rake文件创建后台任务。我已经在rake文件中复制了我的rake任务,并将它们作为方法设置在我的控制器中。我需要延迟工作,因为heroku每1小时只生一个cron。

这是我的应用程序控制器:

class ApplicationController < ActionController::Base

require 'delayed_job'
require 'Mechanize'
require 'pp'


protect_from_forgery

def iqmedier
          agent = WWW::Mechanize.new
          agent.get("http://www.iqmedier.dk")
          form = agent.page.forms.first
          form.submit

          agent.page.link_with(:href => "/Publisher/Stats").click

          form = agent.page.forms.first
          form.submit

          @stats = agent.page.search('//tr')[-2]

          @existing = Reklamer.find(:first, :conditions => {:dato => @stats[0]})
          if @existing.nil?
              Reklamer.create!(:virksomhed => 'Iqmedier', :dato => @stats[0], :unik_klik => @stats[1], :klik => @stats[2], :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
          elsif @existing.dato != Date.today
          Reklamer.create!(:virksomhed => 'Iqmedier', :dato => Date.today, :unik_klik => 0, :klik => 0, :unik_vis => 0, :vis => 0, :leads => 0, :ordre => 0, :cpc => 0, :earn => 0)
          else
            @existing.update_attributes!(:unik_klik => @stats[1], :klik => @stats[2].to_i, :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
          end
  end
 def euroads
        agent = Mechanize.new { |agent|
        agent.user_agent_alias = 'Mac Safari'}
        agent.get("http://www.euroads.dk")
        form = agent.page.forms.first
        form.submit
        @month = Date.today.strftime("%m").to_s
        agent.get("http://www.euroads.dk/system/index.php?showpage=showstat&show=overview&month=#{@month}&year0=2011&day=1&month=#{@month}&year=2011&day1=31&month1=#{@month}&year1=2011&fk_campaign=&fk_survey=&fk_track=&fk_pool=&columns1=1&columns2=2&columns4=4&columns14=14&columns5=5&columns6=6&columns7=7&columns8=8&columns9=9")

        @stats =  agent.page.search('table.ea').search('tr')

        @existing = Reklamer.find(:first, :conditions => {:dato => @stats[0]})
        if @existing.nil?
            Reklamer.create!(:virksomhed => 'Euroads', :dato => @stats[0], :unik_klik => @stats[1], :klik => @stats[2].to_i, :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
        elsif @existing.dato != Date.today
          Reklamer.create!(:virksomhed => 'Euroads', :dato => Date.today, :unik_klik => 0, :klik => 0, :unik_vis => 0, :vis => 0, :leads => 0, :ordre => 0, :cpc => 0, :earn => 0)
        else
          @existing.update_attributes(:unik_klik => @stats[1], :klik => @stats[2].to_i, :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
        end
  end

def mikkelsen
        agent = Mechanize.new
        agent.get("http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php")

        form = agent.page.forms.first
        @stats = agent.page.search('//tr')

        @existing = Reklamer.find(:first, :conditions => {:dato => @stats[0] })
        if @existing.nil?
            Reklamer.create!(:virksomhed => 'Mikkelsen', :dato => @stats[0], :unik_klik => @stats[3], :klik => @stats[3].to_i, :unik_vis => @stats[1], :vis => @stats[1], :leads => @stats[4], :ordre => @stats[9], :cpc => @stats[3], :earn => @stats[5])
        else
            @existing.update_attributes(:virksomhed => 'Mikkelsen', :dato => @stats[0], :unik_klik => @stats[3], :klik => @stats[3].to_i, :unik_vis => @stats[1], :vis => @stats[1], :leads => @stats[4], :ordre => @stats[9], :cpc => @stats[3], :earn => @stats[5])
        end
   end
def orville
        agent = WWW::Mechanize.new
        agent.get("https://dk.orvillemedia.com")

        form = agent.page.forms.first
        form.submit
        @dato = Date.today.strftime("%Y-%m-%d").to_s
        @month = Date.today.strftime("%m").to_s
        @day = Date.today.strftime("%d").to_s
        @stats = agent.page.search('//tr')

        @existing = Reklamer.find(:first, :conditions => {:dato => @dato})
        if @existing.nil?
            Reklamer.create!(:virksomhed => 'Orville', :dato => @dato, :unik_klik => @stats[2], :klik => @stats[2].to_i, :unik_vis => @stats[1], :vis => @stats[1], :leads => @stats[3], :ordre => '0', :cpc => (@stats[5] == '-' ? 0 : @stats[3]), :earn => @stats[6])
        else
          @existing.update_attributes(:virksomhed => 'Orville', :dato => Date.today, :unik_klik => @stats[2], :klik => @stats[2].to_i, :unik_vis => @stats[1], :vis => @stats[1], :leads => @stats[3], :ordre => '0', :cpc => (@stats[5] == '-' ? 0 : @stats[3]), :earn => @stats[6])
        end
   end

def runall
  [:iqmedier, :euroads, :mikkelsen, :orville].each{|a| send(a)}    
end
handle_asynchronously :runall, :run_at => Proc.new { 5.minutes.from_now }

当我运行rake作业时:它开始工作但是没有运行

的runall作业

2 个答案:

答案 0 :(得分:6)

如果我理解你正在尝试做什么,那么你应该有以下内容:

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

在您的控制器中:


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

要从rails外部添加作业,您可以编写类似的东西,然后使用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

答案 1 :(得分:0)

如果您只想每X分钟/小时运行一次代码,那么您不需要使用delayed_job。这将是一种矫枉过正和过度设计。只需在此处使用此代码:

    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

将代码放入Rakefile,从中创建任务并将其部署在heroku worker dyno上。那可行。以下是完整的教程:http://robert-reiz.com/2012/06/12/cron-jobs-on-heroku/

对于该解决方案,您甚至不需要Herokus Cron或Scheduler Add-On。而且您不需要任何额外的GEM,如delayed_job。务实的解决方案。保持苗条; - )