I'm writing an Rails 4.2.0 application where I need to deliver emails. I've been advised Que
gem for managing background jobs. I've done everything as in installation and usage is listed here.
Also I've specified in application.rb
these lines:
# For https://github.com/chanks/que
config.active_record.schema_format = :sql
config.active_job.queue_adapter = :que
My job looks like this 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
After running the rails s
command my console is populated with these messages:
{
"lib":"que",
"hostname":"...",
"pid":13938,
"thread":69925811873800,
"event":"job_unavailable"
}
And when I enqueue my job like this in controller
SendWelcomeEmail.enqueue 20, priority: 100
and refresh the page, I get the following errors all the time ( despite I can send messages is sync manner without using 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
}
}
And when I open rails console
in the second terminal and enter there Que.worker_states
(it's written here and should return information about every worker in the system) I get []
.
I think that I have no workers spawned. Am I right? And how to fix it?
UPDATE
Found error in 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'
Line 8 is:
def run(user_id, options)
SOLUTION
Now its' working. I've deleted the adapter configuration from application.rb
and in place of
SendWelcomeEmail.enqueue 20, priority: 100
wrote
@user = User.find(20)
SendWelcomeEmail.enqueue @user.id, :priority => 100
Now it's works. Funny thing in the second variant the same values are passed to the function. Still error message said that run
obtained only 1 argument - 20
.
Reading the
que
gem, it looks like theenqueue
method treatspriority
keyword as a special case: https://github.com/chanks/que/blob/master/lib/que/job.rb#L31So, your
run
method is only passed the first argument. Thepriority
keyword gets swallowed byque
.Changing your
run
method toshould fix your issue.