Messaging

How the runner sends Celery messages to SQS: delayed tasks, retries and the next tasks of a workflow.

Send Celery messages to SQS from the runner.

The runner needs to publish messages in three cases: to hold a task until its eta (it sends the same message back with a delay), to retry a task (same message with an updated retries/eta header) and to start the next task of a canvas (a message built from a signature). Messages follow Celery’s protocol 2, the format that the Celery client (kombu) writes: base64(json(envelope)) with the body, headers and properties, where the envelope body is base64(json([args, kwargs, embed])).

class celery_cloud.messaging.QueueRef(partition: str, region: str, account: str, name: str)[source]

Bases: NamedTuple

An SQS queue, as found in the eventSourceARN of a record.

account: str

Alias for field number 2

classmethod from_arn(arn: str) → QueueRef[source]
name: str

Alias for field number 3

named(name: str) → QueueRef[source]

Another queue of the same account and Region.

partition: str

Alias for field number 0

region: str

Alias for field number 1

url() → str[source]
celery_cloud.messaging.build_message(signature: dict[str, Any], args_prefix: Sequence[Any] = (), *, queue: str, parent_id: str | None = None, root_id: str | None = None, chain: list[dict[str, Any]] | None = None, now: datetime | None = None) → tuple[str, str][source]

Build the message that runs a Celery signature (as a client would send it).

Parameters:
  • signature – A signature dict (task, args, kwargs, options, immutable), the format Celery puts in link, chain and chord.

  • args_prefix – Arguments placed before the signature’s own (the result of the previous task). Ignored when the signature is immutable.

  • queue – Name of the queue the message is for (its routing key).

  • parent_id – Id of the task that triggers this one.

  • root_id – Id of the first task of the workflow.

  • chain – Remaining steps of a chain to carry in the message.

  • now – Clock, for tests.

Returns:

(task_id, sqs_message_body).

celery_cloud.messaging.delay_message(record: SQSRecord, seconds: float) → int[source]

Send the message of record back to its queue, delayed. Returns the delay.

celery_cloud.messaging.encode_body(envelope: dict[str, Any]) → str[source]

The SQS message body kombu writes for a Celery message envelope.

celery_cloud.messaging.retry_message(record: SQSRecord, retries: int, eta: datetime | None) → str[source]

The body of record with the retries and eta headers updated.

celery_cloud.messaging.send(queue: QueueRef, body: str, delay_seconds: int = 0) → None[source]

Send a message body to a queue.

Raises:

BackendException – When SQS rejects or cannot receive the message.