I am a newbie to delayed job but in local the gem is working flawlessly. In production, the gem logs an entry in delayed_jobs table and then within milli-seconds completes it, deletes it without processing.
A critical difference I found was the handler entry created in the database.
Local
--- !ruby/object:Delayed::PerformableMethod
object: !ruby/ActiveRecord:User
attributes:
id: '1'
email: abc#example.com.au
is_pending: pending
method_name: :job_without_delay
args:
- false
Heroku
--- !ruby/object:Delayed::PerformableMethod
attributes:
id: 1
email: abc#example.com.au
is_pending: pending
What is going on? Can any one please assist?
Controller
def make_pending
#user = current_user
#user.job(false)
redirect_to user_path(current_user)
end
User Model
def job(silence)
Project.job(self.id, silence)
end
handle_asynchronously :job, :run_at => Proc.new { 5.minutes.from_now }
Another difference
Local logs
[Worker(host:__.local pid:5179)] Starting job worker
[Worker(host:__.local pid:5179)] User#job_without_delay completed after 417.0975
[Worker(host:__.local pid:5179)] 1 jobs processed at 0.0024 j/s, 0 failed ...
Heroku logs
013-01-14T12:15:37+00:00 app[worker.1]: [Worker(host:19edbbfb-b8b9-4528-bca6-46ecac4e66bc pid:2)] NilClass# completed after 0.0119
2013-01-14T12:15:37+00:00 app[worker.1]: [Worker(host:19edbbfb-b8b9-4528-bca6-46ecac4e66bc pid:2)] 1 jobs processed at 26.9897 j/s, 0 failed ...
Finally found solution
Added a new model dj.rb
class Dj < Struct.new(:uid, :silence)
def perform
......
end
end
Related
I added the delayed_job gem and I created a simple ActiveJob. I am inserting record to the DB after my job run. When I check the table I am seeing 2 records. And I am seeing the following lines in my console.
[Worker(host:k pid:4962)] Job SimpleJob (id=9305) (queue=Default) RUNNING
[Worker(host:k pid:4962)] Job SimpleJob (id=9305) (queue=Default) COMPLETED after 0.7371
[Worker(host:k pid:4962)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=9306) (queue=Default) RUNNING
[Worker(host:k pid:4962)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=9306) (queue=Default) COMPLETED after 0.5055
[Worker(host:k pid:4962)] 2 jobs processed at 1.5728 j/s, 0 failed
Why is my job running twice? How can I fix that?
Edit: The following code is my ActiveJob code. I added a SimpleJob.schedule! line in config/initializers/job_initializars.rb for first start.
class SimpleJob < ActiveJob::Base
include Delayed::RecurringJob
run_every 1.minute
queue 'Default'
def perform
begin
Vehicle.where(number_plate: lo."12345" ).update_all(:longitude => "47", :latitude => "33")
rescue => ex
Delayed::Worker.logger.info "---------------->" + ex.message
end
end
end
I have a Rails application that runs some background jobs via ActiveJob and Sidekiq. The sidekiq logs in both the terminal and the log file show the following:
2016-10-18T06:17:01.911Z 3252 TID-oukzs4q3k ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper JID-97318b38b1391672d21feb93 INFO: start
Is there some way to show the class names of the jobs here similar to how logs work for a regular Sidekiq Worker?
Update:
Here is how a Sidekiq worker logs:
2016-10-18T11:05:39.690Z 13678 TID-or4o9w2o4 ClientJob JID-b3c71c9c63fe0c6d29fd2f21 INFO: start
Update 2:
My sidekiq version is 3.4.2
I'd like to replace ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper with Client Job
So I managed to do this by removing Sidekiq::Middleware::Server::Logging from the middleware configuration and adding a modified class that displays the arguments in the logs. The arguments themself contain the job and action names as well.
For latest version, currently 4.2.3, in sidekiq.rb
require 'sidekiq'
require 'sidekiq/middleware/server/logging'
class ParamsLogging < Sidekiq::Middleware::Server::Logging
def log_context(worker, item)
klass = item['wrapped'.freeze] || worker.class.to_s
"#{klass} (#{item['args'].try(:join, ' ')}) JID-#{item['jid'.freeze]}"
end
end
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.remove Sidekiq::Middleware::Server::Logging
chain.add ParamsLogging
end
end
For version 3.4.2, or similar, override the call method instead:
class ParamsLogging < Sidekiq::Middleware::Server::Logging
def call(worker, item, queue)
klass = item['wrapped'.freeze] || worker.class.to_s
Sidekiq::Logging.with_context("#{klass} (#{item['args'].try(:join, ' ')}) JID-#{item['jid'.freeze]}") do
begin
start = Time.now
logger.info { "start" }
yield
logger.info { "done: #{elapsed(start)} sec" }
rescue Exception
logger.info { "fail: #{elapsed(start)} sec" }
raise
end
end
end
end
You must be running some ancient version. Upgrade.
Sorry, looks like that's a Rails 5+ feature only. You'll need to upgrade Rails. https://github.com/rails/rails/commit/8d2b1406bc201d8705e931b6f043441930f2e8ac
I have a job file and I am using delayed_job driver for rails.
It works fine for perform later.
My job file:
jobs/test_job_job.rb
class TestJobJob < ActiveJob::Base
queue_as :default
def perform(*args)
puts("test")
end
def test_method
puts('test method')
end
end
In my controller, I did,
TestJobJob.perform_later
It worked fine and gave me right output:
Output:
[Worker(host:hunter pid:8079)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=18) RUNNING
test
[Worker(host:hunter pid:8079)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=18) COMPLETED after 0.0397
[Worker(host:hunter pid:8079)] 1 jobs processed at 1.3259 j/s, 0 failed
But when I changed to,
TestJobJob.delay(run_at: 2.minutes.from_now)
Job was not even en-queued, and when I did,
TestJobJob.delay(run_at: 2.minutes.from_now).test_method
It gave me error: undefined method `test_method' for TestJobJob
My config:application.rb
config.active_job.queue_adapter = :delayed_job
Here is my code:
after_commit :notify, on: :create
The notify method, calls to ServiceRequestMailerWorker.perform_async(self.id, email) and here is my that mailer looks like:
class ServiceRequestMailerWorker
include Sidekiq::Worker
def perform(service_request_id, send_to)
#service_request = ServiceRequest.find(service_request_id)
#customer = #service_request.customer
WarrantyMailer.submitted(#service_request, #customer, send_to).deliver
end
end
Error:
2014-07-22T03:05:25.626004+00:00 app[worker.1]: 2014-07-22T03:05:25.625Z 2 TID-os0kqm6sg DEBUG: enqueued retry: {"retry":true,"queue":"default","class":"ServiceRequestMailerWorker","args":[787,"email#email.com"],"jid":"f1d8f416718bafa8c809fa3e","enqueued_at":1405998308.4219391,"error_message":"Couldn't find ServiceRequest with 'id'=787 [WHERE (company_id IS NOT NULL)]","error_class":"ActiveRecord::RecordNotFound","failed_at":1405998308.618538,"retry_count":0}
This ONLY happens in my staging environment on Heroku. I have been trying to debug this for hours. Anyone else run into this issue?
I'm trying to send emails using delayed job from my Rails 3.2 app on Heroku when someone fills out a form. I've been able to get the emails to send successfully through delayed job on my local development machine. I can also get the emails to send using delayed job if I run them manually through the console on Heroku. However, when someone submits a form which triggers the email, it will not send.
Here's my mailer:
class ClaimMailer < ActionMailer::Base
default from: "noreply#coatchex.com"
def patron(claim)
mail(:to => claim.email, :subject => I18n.t('mailers.claims.patron.subject'))
end
def coatchex(claim)
#claim = claim
mail(:to => 'claims#coatchex.com', :subject => I18n.t('mailers.claims.coatchex.subject'))
end
end
Here's my controller:
class ClaimsController < ApplicationController
layout 'basic'
def new
#claim = CoatChex::Forms::Claim.new
end
def create
#claim = CoatChex::Forms::Claim.new(params[:claim])
if #claim.valid?
ClaimMailer.delay.coatchex(#claim)
render :thank_you
else
render :new
end
end
end
Like I mentioned, If I run the following command through the Heroku console it queues the email up in delayed job and sends it just fine:
#claim = ...
ClaimMailer.delay.coatchex(#claim)
However, whenever I send it through the form, it does not trigger.
If I'm quick enough I can run Delayed::Job.count in the Heroku console and see a value of 1 before the job executes when submitting through the form. So I know delayed job is getting it. If I look at the worker logs using
heroku logs -p worker -t
I can see the job process getting logged when executing it manually but not when it goes through the form.
There are no failed jobs in the database.
Anybody run into anything like this before?
I had a similar problem. A good starting point was the information at https://devcenter.heroku.com/articles/delayed-job#debugging - specifically, running Delayed::Job.last.last_error on the Heroku console.
In my case, the error I was getting was Job failed to load: uninitialized constant Syck::Syck, which was fixed by adding psych to my gemfile. See Delayed_job: Job failed to load: uninitialized constant Syck::Syck and http://effectif.com/ruby-on-rails/syck-and-psych-yaml-parsers-on-heroku
You need to start the worker with the command
rake jobs:work
on your heroku rails console.