gpt4 book ai didi

ruby - Rails 延迟工作不工作

转载 作者:太空宇宙 更新时间:2023-11-03 17:26:59 25 4
gpt4 key购买 nike

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

这是我的应用程序 Controller :

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 jobs:work 时,它启动了,但没有运行 runall 作业

最佳答案

如果我正确理解了您要执行的操作,那么您应该具备以下条件:

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

在你的 Controller 中:


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

关于ruby - Rails 延迟工作不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5674880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com