delayed_job入队方法的新错误

时间:2011-10-21 15:34:18

标签: ruby-on-rails delayed-job

这是我的模特:

class Scraper

def perform

Tracker.all.each do |tracker|

    doc = Nokogiri::XML(open('http://share.findmespot.com/messageService/guestlinkservlet?glId=' + tracker.glid + '&completeXml=true') )

    doc.xpath('//messageList/message').map do |m|
      s = Spot.new({ :tracker_id => Tracker.find_by_esn(m.xpath('esn').text).id, :messagetype => m.xpath('messageType').text, :timestamp => m.xpath('timestamp').text, :latitude => m.xpath('latitude').text, :longitude => m.xpath('longitude').text, :timeingmtsecond => m.xpath('timeingmtsecond').text})
      s.save
    end

end

Delayed::Job.enqueue(Scraper.new, :run_at => 5.minutes.from_now)

end

def error(job, exception)
# Send a warning email to yourself, or whatever.
# The scraping will automatically be retried.
end

def success(job)
# Schedule the next scraping.

end

出现此错误:

> ** Execute gps_start rake aborted! undefined method `to_i' for {:run_at=>Fri, 21 Oct 2011 11:37:19 EDT -04:00}:Hash
> /app/.bundle/gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/backend/base.rb:21:in
> `enqueue' /app/lib/scraper.rb:16:in `perform'
> /app/lib/tasks/tracker.rake:4
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/task.rb:205:in
> `call'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/task.rb:205:in
> `execute'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/task.rb:200:in
> `each'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/task.rb:200:in
> `execute'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/task.rb:158:in
> `invoke_with_call_chain' /usr/ruby1.8.7/lib/ruby/1.8/monitor.rb:242:in
> `synchronize'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/task.rb:151:in
> `invoke_with_call_chain'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/task.rb:144:in
> `invoke'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:112:in
> `invoke_task'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in
> `top_level'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in
> `each'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in
> `top_level'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:129:in
> `standard_exception_handli ng'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:84:in
> `top_level'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:62:in
> `run'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:129:in
> `standard_exception_handli ng'
> /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/lib/rake/application.rb:59:in
> `run' /app/.bundle/gems/ruby/1.8/gems/rake-0.9.2/bin/rake:32
> /app/.bundle/gems/ruby/1.8/bin/rake:19:in `load'
> /app/.bundle/gems/ruby/1.8/bin/rake:19 Tasks: TOP => gps_start

2 个答案:

答案 0 :(得分:2)

这取决于您使用的delayed_job版本。实际上,最新版本需要一个选项哈希,支持密钥:priority和:run_at。但是,如果您使用的是旧版本的delayed_job,则其效果类似于答案1。

答案 1 :(得分:1)

Delayed::Jobs::enqueue不接受选项哈希。它按顺序需要1到3个参数:

  • 工作
  • 优先级(可选)
  • 运行时间(可选)

因为您传入了第二个参数的选项哈希值,所以它在其上调用#to_i以尝试将其转换为优先级,这会导致您获得错误。如果您只想指定一个运行时间,可以传入nil作为优先级,它将使用默认优先级:

Delayed::Job.enqueue(Scraper.new, nil, 5.minutes.from_now)
相关问题