Sidekiq: How to get the Job ID inside the worker?

Created on 11 Oct 2014  Â·  14Comments  Â·  Source: mperham/sidekiq

Hi @mperham I have a question is it possible to get the job id inside of a worker?

Currently I can get the job id using

job_id = MyWorker.perform_async("test")

but what I want is to get the job id inside of the worker like this:

class MyWorker

   def perform(name)
        //get current job id here
    end

end

Most helpful comment

jid

That's it. 'self.jid'

On Oct 11, 2014, at 07:50, Allen Chun [email protected] wrote:

Hi @mperham I have a question is it possible to get the job id inside of a worker?

Currently I can get the job id using

job_id = MyWorker.perform_async("test")
but what I want is to get the job id inside of the worker like this:

class MyWorker

def perform(name)
//get current job id here
end

end
—
Reply to this email directly or view it on GitHub.

All 14 comments

jid

That's it. 'self.jid'

On Oct 11, 2014, at 07:50, Allen Chun [email protected] wrote:

Hi @mperham I have a question is it possible to get the job id inside of a worker?

Currently I can get the job id using

job_id = MyWorker.perform_async("test")
but what I want is to get the job id inside of the worker like this:

class MyWorker

def perform(name)
//get current job id here
end

end
—
Reply to this email directly or view it on GitHub.

Nice. Thanks @mperham

Does this only work with MyWorker.perform_async??

It works with Sidekiq::Client.push too.

Hi @mperham,

MyMailer.delay.my_new_mail has async the mail through sidekiq 3.4.2, ruby 2.2.1 and rails 4.2.1 .

And, inside the mailer i want jid as like below:

class MyMailer < ApplicationMailer
  def my_new_mail
       mail(...)
       # jid 
  end
end

@shreeramneupane You cannot get the JID when using the delayed extensions.

Hi @mperham,

When I use delay for
job_id =MyWorker.delay_for(2.day).perform_async(self.id)

And, inside the worker jid get changed, is not same as which I get in job_id


But when I use perform at

job_id = MyWorker.perform_at(2.day.from_now, self.id)

And, inside the worker jid remain same as we get in job_id

Can you please tell me this difference(delay_for and perform_at). I am unable to find this answer?

@sumitisrani Don't use delay on a Worker.

Hi @mperham,

I am using perform at to delay the job

job_id = MyWorker.perform_at(2.day.from_now, self.id)

But when my job run jid get change, the jid is not same as which I get when I schedule the job.
I saved that jid in my DB and when my job run I find the records with jid, update the record in my DB.
But due jid get change I am unable to find out the record and my job get failed.

My sidekiq version is (3.3.4)

@mperham - Could you expand on "Don't use delay on a Worker?" I'm converting a previous implemenation that used a worker (and some wonky raises to implement a retry strategy) into a more pro-active retry that calls .delay_for(5.minutes) within the worker to schedule the next attempt, if needed.

Should I not be calling delay_for within a worker? Can you point to an example of how a method can more cleanly re-invoke itself?

def perform(*args)
  self.class.perform_in(5.minutes, *args)
end

Thanks, @mperham ! :-) I'll give that a whirl!

Thanks @mperham, needed this one also.

@dhempy In which file are you using this code:
job_id = MyWorker.perform_at(2.day.from_now, self.id)
?
If its on a model, self.id will return the id of the object
if its in a post model, you'll get the Post ID, not the ID of the job.

i too remain a bit unsure of how this is meant to be implemented. have you figured it out @pmaojo? if i call perform_async from a model, and then perform_in from within the worker (which is what seems to be the conclusion of this thread), there is still no impact on when the worker executes.

further: the API states that it's bad/unscalable to search and delete jobs from queues unless the data needs to be repaired.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sandstrom picture sandstrom  Â·  3Comments

davidcelis picture davidcelis  Â·  3Comments

BeRMaNyA picture BeRMaNyA  Â·  3Comments

homanchou picture homanchou  Â·  3Comments

michaeldiscala picture michaeldiscala  Â·  4Comments