Delayed_job未执行perform方法,但清空了作业队列 [英] Delayed_job not executing the perform method but emptying the job queue
问题描述
我有一个新鲜的Rails 3应用程序,这是我的Gemfile:
I have a fresh rails 3 app, here's my Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.0' gem 'delayed_job'
gem 'sqlite3-ruby', :require => 'sqlite3'
以下是代表我要排队的作业的类:
Here's the class that represents the job that I want to queue:
class Me < Struct.new(:something)
def perform
puts "Hello from me"
logger.info "Hello from me"
logger.debug "Hello from me"
raise Exception.new
end
end
从没有工人的控制台中运行:
From the console with no workers running:
irb(main):002:0> Delayed::Job.enqueue Me.new(1)
=> #<Delayed::Backend::ActiveRecord::Job id: 7, priority: 0, attempts: 0, handler: "--- !ruby/struct:Me \nsomething: 1\n", last_error: nil, run_at: "2010-12-29 07:24:11", locked_at: nil, failed_at: nil, locked_by: nil, created_at: "2010-12-29 07:24:11", updated_at: "2010-12-29 07:24:11">
就像我提到的那样:没有工人在跑:
Like I mentioned: there are no workers running:
irb(main):003:0> Delayed::Job.all
=> [#<Delayed::Backend::ActiveRecord::Job id: 7, priority: 0, attempts: 0, handler: "--- !ruby/struct:Me \nsomething: 1\n", last_error: nil, run_at: "2010-12-29 07:24:11", locked_at: nil, failed_at: nil, locked_by: nil, created_at: "2010-12-29 07:24:11", updated_at: "2010-12-29 07:24:11">]
我从 script / delayed_job运行
队列被清空:
irb(main):006:0> Delayed::Job.all
=> []
但是,投入$不会导致任何结果c $ c>,
logger
调用不会记录任何内容,并且不会引发异常。
However, nothing happens as a result of the puts
, nothing is logged from the logger
calls, and no exception is raised. I'd appreciate any help / insight or anything to try.
推荐答案
默认情况下,delay_job破坏失败的作业:
By default, delayed_job destroys failed jobs:
所以第一步是配置一个初始化程序并停用该行为
So the first step is to configure an initializer and deactivate that behaviour
Delayed::Worker.destroy_failed_jobs = false
此外,如果反序列化失败,那将是一项即时工作
Also, if you get a deserialization failure, then that is an instant job failure.
这会触发作业删除(如果您尚未将其删除)。
This triggers job deletion (if you haven't deleted it).
因此请尝试添加跟随worker的第120行。rb
So try adding the following around line 120 of the worker.rb
rescue DeserializationError => error
say "DeserializationError: #{error.message}"
job.last_error = "{#{error.message}\n#{error.backtrace.join('\n')}"
failed(job)
这是相当无益的,但至少您会知道
It's fairly unhelpful, but at least you'll know it's a deserialization error then.
我最终只是使用作业w。 perform()方法构造。更可靠。还记得将作业定义作为单独的文件放置,以便类加载器在运行时可以找到它(而不是仅将类定义内联到模型中的某个地方)。
I ended up just using the job w. perform() method construct. Much more reliable. Also remember to put your job definition as a separate file so the class loader can find it when it is running (rather than just inlining the class definition into your model somewhere).
这篇关于Delayed_job未执行perform方法,但清空了作业队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!