From bad054989d515f07208e7363748983319c83797e Mon Sep 17 00:00:00 2001 From: Michael Keirnan Date: Tue, 22 Sep 2015 08:18:31 -0400 Subject: [PATCH] export worker id and job id to env of work horse For tracing job execution in a distributed system it is useful to tag log entries with the worker id and job id. The current job is accessible via get_current_job(), but that requires an extra redis connection. And the current worker id (the worker id of the parent process) does not appear to be available. Rather than introducing an `rqworker` alternative or subclassing Worker, it feels simple and efficient to make these contextual ids available as environment variables. This should have no performance cost and no API compatibility issues. Some useful things to do with these values in the worker horse process: + include them in log messages + include them as 'x-' headers in HTTP requests made by workers --- rq/worker.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rq/worker.py b/rq/worker.py index c1c5d8e..f1cebf9 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -491,6 +491,8 @@ class Worker(object): within the given timeout bounds, or will end the work horse with SIGALRM. """ + os.environ['RQ_WORKER_ID'] = self.name + os.environ['RQ_JOB_ID'] = job.id child_pid = os.fork() if child_pid == 0: self.main_work_horse(job)