2017-09-04 68 views
0

我有这样的方法:如何使用Delayed Job在Ruby on Rails的特定小时内执行方法?

module ApplicationHelper 
    class AtualizarBanco 

    require 'open-uri' 
    require 'nokogiri' 
    require 'net/http' 

    def updatetable 
     retorno = Net::HTTP.get_response(URI.parse('http://www.olx.com.br/loja/id/174483')) 

     if retorno.code == "200" 
     site = open ('http://www.olx.com.br/loja/id/174483') 
     site_lido = site.read 
     site_html = Nokogiri::HTML(site_lido) 

     vetor_preco = site_html.at_css('#main-ad-list').css('.item').css('.OLXad-list-price').map{|q| q.text.sub!(" R$ ", "").sub!(".", "").to_f} 
     vetor_texto = site_html.at_css('#main-ad-list').css('.item').css('.OLXad-list-title').map{|lista| lista.text.strip} 
     vetor_catg = site_html.css('#main-ad-list .OLXad-list-line-2').xpath('p[@class="text detail-category"]').map{|q| q.children.first.text.strip} 
     vetor_img = site_html.css('#main-ad-list .OLXad-list-image-box').xpath('span|img[@class="image"]/@src|img[@class="image lazy"]/@data-original').map(&:text) 

     i = 0 
     if ((vetor_preco.length == vetor_texto.length) && (vetor_preco.length == vetor_catg.length) && (vetor_preco.length == vetor_img.length)) 
      while i < vetor_preco.length 
      @prod = Produto.new(nm_produto: vetor_texto[i], preco: vetor_preco[i], categoria: vetor_catg[i], img_produto: vetor_img[i]) 
      i += 1 
      if (@prod.valid?) 
       @prod.save 
      end 
      end 
     end 
     end 
    end 

    end 
end 

而且我想这个方法每天上午5:30 GMT执行。

我做了一些搜索,我发现了关于延迟招聘的宝石和克龙调度,但我如何使用它们做什么,我需要它,我不清楚。

延迟招聘效果很好on Rails的5.0.0.1? 关于3.0+和4.2版本的文档,我不清楚它是“4.2以上”还是4.2。

我也不知道我是否仅仅使用gem'delayed-job'或者需要使用gem'delayed_job_active_record',或者如果我需要更改config.active_job.queue_adapter,才会调用延迟作业gem。

的文档,它也不会说,如果:run_at参数接受特定时间的命令,在实例它只有像“5分钟再予从现在开始”的东西或“2小时了。”

任何帮助?

回答

1

您可以使用宝石whenever,从application_helper更换你的方法耙任务updatetable.rake例如

而且你的时间表会随着

every 1.day, at: '5:30 am' do 
    rake 'atualizar_banco:updatetable' 
end 
+0

我不明白“取代你的方法耙任务”。 我需要把它放到一个名为updatetable.rake文件? –

+0

是的,你可以找到更多信息[这里](http://guides.rubyonrails.org/command_line.html#custom-rake-tasks) –

+0

一两件事,这个“5:30”的标准时间是格林尼治标准时间已,或者我需要配置? –