Time limits and metrics

Time limits for task execution.

Celery’s soft_time_limit / time_limit and the Lambda timeout are enforced with SIGALRM timers, so a task that runs too long is stopped by the runner, which can then store a FAILURE result. Without this AWS kills the function at its timeout, nothing is stored and the task stays in STARTED forever.

Limits:
  • Signals only work in the main thread of the process (that is where AWS Lambda runs the handler). In any other thread the limits are silently not enforced.

  • SIGALRM is delivered between Python bytecodes and interrupts blocking system calls. A C extension that never returns control cannot be interrupted; the Lambda timeout remains the last safety net.

celery_cloud.deadline.effective_limits(task_soft: float | None, task_hard: float | None, seconds_left: float | None, margin: float) → tuple[float | None, float | None, str | None][source]

Combine the limits of the task with the time the Lambda has left.

Returns:

(soft, hard, hard_reason). The hard limit is the smaller of the task’s own and seconds_left - margin, so the failure can be stored before AWS stops the function.

celery_cloud.deadline.remaining_seconds(context: object) → float | None[source]

Seconds left before AWS kills the invocation, or None if unknown.

celery_cloud.deadline.signals_available() → bool[source]

True when SIGALRM timers can be used (main thread, POSIX).

celery_cloud.deadline.time_limits(soft: float | None, hard: float | None, hard_reason: str | None = None) → Iterator[None][source]

Raise SoftTimeLimitExceeded / TimeLimitExceeded in the running code.

Parameters:
  • soft – Seconds after which SoftTimeLimitExceeded is raised.

  • hard – Seconds after which TimeLimitExceeded is raised.

  • hard_reason – Text for the hard exception (default TimeLimitExceeded(N,)).

Metrics

CloudWatch metrics through the Embedded Metric Format (EMF).

When METRICS_ENABLED=true the runner writes one JSON line per task outcome to its log; CloudWatch turns it into metrics without any API call (and no extra permission). Metrics, with the task name as dimension: TaskSucceeded, TaskFailed, TaskRetried, TaskSkipped (duplicate, expired, revoked), TaskDelayed (held until the eta), TaskIgnored / TaskRejected, and TaskDuration in milliseconds for the tasks that ran.

celery_cloud.metrics.emit(task_name: str, outcome: str, duration_seconds: float | None = None) → None[source]

Write the EMF line of one task outcome (outcome is the metric name).