Get The Job Context¶
Sometimes you need access to details about the job that is currently running. For example you might want to know the job’s ID to log it, or the number of times the job has been retried. Getting the job context is easy:
from chancy import QueuedJob, job
@job()
def my_job(*, context: QueuedJob):
print(f"Job ID: {context.id}")
print(f"Job attempts: {context.attempts}")
That’s it! When Chancy runs a job, it checks to see if the type signature for that
job function includes a chancy.job.QueuedJob
and assumes you want the
context for the job.
Tip
The name of the argument doesn’t matter, as long as the type is correct. For
example, you could name the argument job_context
instead of context
.
The job context is immutable, except for the meta
attribute, which you can
use to store arbitrary data about the job:
from chancy import QueuedJob, job
@job()
def my_job(*, context: QueuedJob):
# This will raise an exception because the job context is
# generally immutable.
context.id = "new_id"
# This will work because the meta attribute is mutable.
context.meta["attempts"] = context.meta.get("attempts", 0) + 1