Skip to content

Decorators

exactly_once

django_periodic_tasks.decorators.exactly_once(func)

Decorator ensuring a scheduled task runs at most once per invocation.

When the scheduler creates a TaskExecution row and passes its ID via the _periodic_tasks_execution_id keyword argument, this decorator will:

  1. Pop _periodic_tasks_execution_id from kwargs.
  2. Lock the TaskExecution row with SELECT FOR UPDATE.
  3. Run the wrapped function only if the row’s status is PENDING.
  4. Mark the row COMPLETED on success.

If _periodic_tasks_execution_id is absent (e.g. manual invocation), the wrapped function runs normally without any execution-permit logic.