Reputation: 161
Inside the perform
method, I want to know what is the ID of the current job.
For instance, the enqueue
, success
, failure
and error
methods provide the job
parameter:
def enqueue(job)
puts job.id
end
Is it possible?
Upvotes: 3
Views: 2502
Reputation: 5279
What I did was in an initializer, you can monkey patch the JobWrapper class and use
module ActiveJob
module QueueAdapters
class DelayedJobAdapter
class JobWrapper
def perform(job)
end
end
end
end
end
Upvotes: 0
Reputation: 2464
I assume you are using sidekiq as a backend. In that case you may do the following:
def perform(*args)
logger.info self.job_id
# Do something later
end
Here is the log output
[ActiveJob] [RequestHandlerJob] [e9923650-cd02-40d1-937d-859852e92c61] e9923650-cd02-40d1-937d-859852e92c61
UPDATE
Sorry, missed that.
I found solution in that SO post
# config/initializers/delayed_job.rb
Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'dj.log'))
class RequestHandlerJob < ActiveJob::Base
queue_as :default
def perform(user_id)
u = User.find(user_id)
Delayed::Worker.logger.info self.job_id
# Do something later
end
end
[retgoat@iMac-Roman ~/workspace/tapp/log]$ tail -f dj.log
I, [2016-06-24T07:58:32.329471 #23874] INFO -- : 2016-06-24T07:58:32+0600: [Worker(host:iMac-Roman.local pid:23874)] Starting job worker
I, [2016-06-24T07:58:42.404522 #23874] INFO -- : 2016-06-24T07:58:42+0600: [Worker(host:iMac-Roman.local pid:23874)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=7) RUNNING
I, [2016-06-24T07:58:42.448858 #23874] INFO -- : 5f267272-8826-491d-b7d5-82a200e1a6b6
I, [2016-06-24T07:58:42.451204 #23874] INFO -- : 2016-06-24T07:58:42+0600: [Worker(host:iMac-Roman.local pid:23874)] Job ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper (id=7) COMPLETED after 0.0466
I, [2016-06-24T07:58:42.452007 #23874] INFO -- : 2016-06-24T07:58:42+0600: [Worker(host:iMac-Roman.local pid:23874)] 1 jobs processed at 15.9642 j/s, 0 failed
Here is the job_id 5f267272-8826-491d-b7d5-82a200e1a6b6
. Hope that will help.
Upvotes: 1