我正在编写一个需要发送电子邮件的 Rails 4.2.0 应用程序。有人建议我Que
使用 gem 来管理后台作业。我已经完成了安装和使用中列出的所有操作。
我也在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
(它写在这里,应该返回系统中每个工人的信息)时,我得到了[]
。
我认为我没有产生任何工人。我对吗?以及如何解决?
更新
在 que 日志中发现错误:
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
。