Skip to main content

Module task_context

Module task_context 

Source
Expand description

Context that follows work off the request thread — see task_context::TaskContext. tokio::spawn inherits no task_local!, so deferred work starts with none of its caller’s ambient context unless it is carried deliberately. Context that follows work off the request thread.

tokio::spawn does not inherit tokio::task_local!, so a job runs with none of the ambient context its caller had. An audit row written from a job records system — so “who deleted this customer?” is a dead end whenever a job did it.

§An explicit list, not every task-local

It copies the task-locals named below, and only those. Declaring a new task_local! does not opt it in.

task-localcopied?why
audit::AUDIT_SOURCE (+ its tenant binding)yeswho triggered the work
i18n::timezone::ACTIVE_TZyesa formatting preference
sql::executor::atomic::BLOCKnothe caller’s live transaction and its on-commit queue
signals::SUPPRESS_SIGNALSnosave_quietly must not silence a job days later
admin::session::CURRENT_SESSION, CURRENT_CSRF_TOKENnobelong to one request
tenant_log::TENANTnoa log label for one request

A value that was never set is not copied as a default: the work sees “no scope”, exactly as its caller did.

A source set by the tenant admin (or audit::with_tenant_source) names a user of that tenant, so it is recorded only where writes are known to go there (a tenancy::with_tenant or for_each_tenant pass over it); elsewhere, the registry included, it reads as system.

§Where it is carried

  • jobs::InMemoryJobQueue captures at dispatch.
  • scheduler::Scheduler captures at every(): a tick has no caller, so registration is the hand-off.
  • jobs::pg::PgJobQueue stores it in rustango_jobs.context at dispatch. A table without that column (a hand-written migration not yet updated) runs jobs with no context, as before.

§What the source does not tell you

A job’s audit row names the enqueuer and nothing else, so it reads identically to one written on the request itself. Recording that it ran deferred needs its own column rather than a prefix on the token, which would break the exact-match filters that read it: #1385.

Structs§

TaskContext
A snapshot of the ambient context worth carrying into deferred work.