我正在编写一个Rails 4.2.0应用程序,我需要提供电子邮件。我曾被建议Que
gem来管理后台工作。我已经完成了所有的安装和使用列表here。使用后台作业管理器gem执行作业的错误称为Que
而且我在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
,并刷新页面,我收到以下错误所有的时间(尽管我可以发送消息是同步的方式,而不使用阙):
{
"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)我得到[]
。
我认为我没有工人产卵。我对吗?以及如何解决它?
UPDATE
在阙日志实测值误差:8
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'
线是:
def run(user_id, options)
SOLUTION
现在我ts'工作。我从application.rb
删除适配器配置和在
SendWelcomeEmail.enqueue 20, priority: 100
地方写
@user = User.find(20)
SendWelcomeEmail.enqueue @user.id, :priority => 100
现在,它的作品。在第二种变体中,有趣的是相同的值被传递给函数。仍然错误消息说,run
只获得1个参数 - 20
。
我会在家里试试,然后回到这里。感谢您的调查。 – zmii