Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the ID of my sidekiq worker inside the worker?

In sidekiq's log file, I can see IDs of workers, like this:

2013-08-28T10:19:03Z 8911 TID-osy5fnl1o MyWorker JID-262996c2737e7a5ec5c71674 INFO: start
2013-08-28T10:19:03Z 8911 TID-ptes4 MyWorker JID-6830e08b5da72b360d4d1ae2 INFO: start
2013-08-28T10:19:03Z 8911 TID-povog MyWorker JID-2d31755b001ecd02fe1abc09 INFO: done: 22.52 sec
2013-08-28T10:19:04Z 8911 TID-povog MyWorker JID-df52f500a3ba27e18b2ba313 INFO: start

Inside the body of my worker's @perform@ method, I'd like to get that ID. If not possible, what would be the best strategy to get a unique ID for that worker, taking into consideration that there are multiple concurrent workers of the same class?

I need this for help in processing log files.

like image 819
Ovesh Avatar asked Aug 29 '13 03:08

Ovesh


2 Answers

You can access the job id using the jid accessor method and the thread id using the Thread.current method. Like this:

logger.info "JID #{jid} - TID #{Thread.current.object_id.to_s(36)}"
like image 81
mirza Avatar answered Sep 17 '22 07:09

mirza


If you're using ActiveJob you get get it with self.job_id (or @job_id).

like image 30
AlexChaffee Avatar answered Sep 20 '22 07:09

AlexChaffee