我正在编写一个Rails 4.2.0应用程序,我需要发送电子邮件。我被告知Que
gem用于管理后台工作。我已经做了所有事情,因为安装和使用列出了here。
此外,我在application.rb
这些行中指定了:
# For https://github.com/chanks/que
config.active_record.schema_format = :sql
config.active_job.queue_adapter = :que
我的工作看起来像send_welcome_message.rb
:
class SendWelcomeEmail < Que::Job
# Default settings for this job. These are optional - without them, jobs
# will default to priority 100 and run immediately.
@priority = 10
@run_at = proc { 1.minute.from_now }
def run(user_id, options)
@user = User.find(user_id)
UserMailer.welcome_email(@user).deliver_now
# Destroy the job.
destroy
end
end
运行rails s
命令后,我的控制台将填充以下消息:
{
"lib":"que",
"hostname":"...",
"pid":13938,
"thread":69925811873800,
"event":"job_unavailable"
}
当我在控制器中将这样的工作排入队列时
SendWelcomeEmail.enqueue 20, priority: 100
并刷新页面,我总是得到以下错误(尽管我可以发送消息是同步方式而不使用que):
{
"lib":"que",
"hostname":"...",
"pid":13938,
"thread":69925811873800,
"event":"job_errored",
"error":{
"class":"ArgumentError",
"message":"wrong number of arguments (1 for 2)"
},
"job":{
"queue":"",
"priority":100,
"run_at":"2015-06-22T01:59:45.187+03:00",
"job_id":11,
"job_class":"SendWelcomeEmail",
"args":[
20
],
"error_count":2
}
}
当我在第二个终端中打开rails console
并输入Que.worker_states
(it's written here and should return information about every worker in the system)时,我得到[]
。
我认为我没有产生任何工人。我对吗?以及如何解决它?
更新
在que log中发现错误:
wrong number of arguments (1 for 2)
/home/username/train/project/app/jobs/send_welcome_email.rb:8:in `run'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/job.rb:15:in `_run'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/job.rb:99:in `block in work'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/adapters/active_record.rb:5:in `block in checkout'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/adapters/active_record.rb:34:in `checkout_activerecord_adapter'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/adapters/active_record.rb:5:in `checkout'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/job.rb:82:in `work'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/worker.rb:78:in `block in work_loop'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/worker.rb:73:in `loop'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/worker.rb:73:in `work_loop'
/home/username/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/que-0.10.0/lib/que/worker.rb:17:in `block in initialize'
第8行是:
def run(user_id, options)
解
现在它的'工作。我已从application.rb
删除了适配器配置,而不是
SendWelcomeEmail.enqueue 20, priority: 100
写
@user = User.find(20)
SendWelcomeEmail.enqueue @user.id, :priority => 100
现在它的确有效。在第二个变体中有趣的事情是将相同的值传递给函数。仍有错误消息表示run
只获得了1个参数 - 20
。
答案 0 :(得分:0)
阅读que
gem,看起来enqueue
方法将priority
关键字视为一种特殊情况:https://github.com/chanks/que/blob/master/lib/que/job.rb#L31
因此,您的run
方法仅传递第一个参数。 priority
关键字被que
吞没。
将run
方法更改为
def run(user_id)
应该解决您的问题。