Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

ruby - Rails delayed job not working

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 my rake file and set them in my controller as methods. I need an delayed job because heroku only makes a cron every 1 hour.

Here is my Application 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 }

When I run rake jobs:work it starts but is not working the runall job of

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...