【发布时间】:2015-06-21 23:20:32
【问题描述】:
我正在编写一个 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) 我得到[]。
我认为我没有产生任何工人。我对吗?以及如何解决?
更新
在查询日志中发现错误:
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。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 background-process que