Job Context¶
::: taskito.context
Thread-local context for the currently executing job. Provides access to job metadata and controls from inside a running task.
Usage¶
current_job is a module-level singleton. It uses thread-local storage internally, so each worker thread sees its own job context.
Warning
current_job can only be used inside a running task. Accessing it outside a task raises RuntimeError.
Properties¶
current_job.id¶
The unique ID of the currently executing job.
current_job.task_name¶
The registered name of the currently executing task.
current_job.retry_count¶
How many times this job has been retried. 0 on the first attempt.
@queue.task(max_retries=3)
def flaky_task():
if current_job.retry_count > 0:
print(f"Retry attempt #{current_job.retry_count}")
call_external_api()
current_job.queue_name¶
The name of the queue this job is running on.
Methods¶
current_job.update_progress()¶
Update the job's progress percentage (0–100). The value is written directly to the database and can be read via job.progress or queue.get_job().
@queue.task()
def process_files(file_list):
for i, path in enumerate(file_list):
handle(path)
current_job.update_progress(int((i + 1) / len(file_list) * 100))
Read progress from the caller:
job = process_files.delay(files)
# Poll progress
import time
while job.status == "running":
print(f"Progress: {job.progress}%")
time.sleep(1)
How It Works¶
- Before each task execution, the Rust worker calls
_set_context()with the job's metadata current_jobreads from thread-local storage (threading.local())- After the task completes (success or failure),
_clear_context()resets the thread-local - Each worker thread has independent context — no cross-thread interference