2017-09-04 42 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」的標準時間是格林尼治標準時間已,或者我需要配置? –

相關問題