如何使用Delayed Job 在Ruby on Rails 的特定时间执行方法?

How to use Delayed Job to execute a method in a specific hour on 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 上午执行此方法。

我进行了一些搜索,发现了延迟作业 gem 和 Cron 调度程序,但我不清楚如何使用它们来完成我需要的事情。

延迟作业在 Rails 5.0.0.1 上运行良好? 在它谈论版本 3.0+ 和 4.2 的文档中,我不清楚它是“4.2 及更高版本”还是只是 4.2。

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

在文档中,它也没有说明 :run_at 参数是否接受特定时间命令,在示例中它只有“从现在开始 5 分钟”或“从现在开始 2 小时”之类的内容.

有什么帮助吗?

您可以使用 gem whenever,将您的方法从 application_helper 替换为 rake 任务 updatetable.rake 例如

你的日程安排如下

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