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
main
Michael Keirnan 9 years ago
parent 1a089887c0
commit bad054989d

@ -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)

Loading…
Cancel
Save