| 1 |
.. _message-protocol: |
| 2 |
|
| 3 |
=================== |
| 4 |
Message Protocol |
| 5 |
=================== |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 10 |
.. _message-protocol-task: |
| 11 |
.. _internals-task-message-protocol: |
| 12 |
|
| 13 |
Task messages |
| 14 |
============= |
| 15 |
|
| 16 |
.. _message-protocol-task-v2: |
| 17 |
|
| 18 |
Version 2 |
| 19 |
--------- |
| 20 |
|
| 21 |
Definition |
| 22 |
~~~~~~~~~~ |
| 23 |
|
| 24 |
.. code-block:: python |
| 25 |
|
| 26 |
properties = { |
| 27 |
'correlation_id': uuid task_id, |
| 28 |
'content_type': string mimetype, |
| 29 |
'content_encoding': string encoding, |
| 30 |
|
| 31 |
# optional |
| 32 |
'reply_to': string queue_or_url, |
| 33 |
} |
| 34 |
headers = { |
| 35 |
'lang': string 'py' |
| 36 |
'task': string task, |
| 37 |
'id': uuid task_id, |
| 38 |
'root_id': uuid root_id, |
| 39 |
'parent_id': uuid parent_id, |
| 40 |
'group': uuid group_id, |
| 41 |
|
| 42 |
# optional |
| 43 |
'meth': string method_name, |
| 44 |
'shadow': string alias_name, |
| 45 |
'eta': iso8601 ETA, |
| 46 |
'expires': iso8601 expires, |
| 47 |
'retries': int retries, |
| 48 |
'timelimit': (soft, hard), |
| 49 |
'argsrepr': str repr(args), |
| 50 |
'kwargsrepr': str repr(kwargs), |
| 51 |
'origin': str nodename, |
| 52 |
'replaced_task_nesting': int, |
| 53 |
'compression': string compression_method (optional; omitted when no compression is used, matches kombu compressor names such as 'zlib', 'bzip2', 'gzip'), |
| 54 |
} |
| 55 |
|
| 56 |
body = ( |
| 57 |
object[] args, |
| 58 |
Mapping kwargs, |
| 59 |
Mapping embed { |
| 60 |
'callbacks': Signature[] callbacks, |
| 61 |
'errbacks': Signature[] errbacks, |
| 62 |
'chain': Signature[] chain, |
| 63 |
'chord': Signature chord_callback, |
| 64 |
} |
| 65 |
) |
| 66 |
|
| 67 |
Example |
| 68 |
~~~~~~~ |
| 69 |
|
| 70 |
This example sends a task message using version 2 of the protocol: |
| 71 |
|
| 72 |
.. code-block:: python |
| 73 |
|
| 74 |
# chain: add(add(add(2, 2), 4), 8) == 2 + 2 + 4 + 8 |
| 75 |
|
| 76 |
import json |
| 77 |
import os |
| 78 |
import socket |
| 79 |
|
| 80 |
task_id = uuid() |
| 81 |
args = (2, 2) |
| 82 |
kwargs = {} |
| 83 |
basic_publish( |
| 84 |
message=json.dumps((args, kwargs, None)), |
| 85 |
application_headers={ |
| 86 |
'lang': 'py', |
| 87 |
'task': 'proj.tasks.add', |
| 88 |
'argsrepr': repr(args), |
| 89 |
'kwargsrepr': repr(kwargs), |
| 90 |
'origin': '@'.join([os.getpid(), socket.gethostname()]) |
| 91 |
} |
| 92 |
properties={ |
| 93 |
'correlation_id': task_id, |
| 94 |
'content_type': 'application/json', |
| 95 |
'content_encoding': 'utf-8', |
| 96 |
} |
| 97 |
) |
| 98 |
|
| 99 |
Changes from version 1 |
| 100 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 101 |
|
| 102 |
- Protocol version detected by the presence of a ``task`` message header. |
| 103 |
|
| 104 |
- Support for multiple languages via the ``lang`` header. |
| 105 |
|
| 106 |
Worker may redirect the message to a worker that supports |
| 107 |
the language. |
| 108 |
|
| 109 |
- Meta-data moved to headers. |
| 110 |
|
| 111 |
This means that workers/intermediates can inspect the message |
| 112 |
and make decisions based on the headers without decoding |
| 113 |
the payload (that may be language specific, for example serialized by the |
| 114 |
Python specific pickle serializer). |
| 115 |
|
| 116 |
- Always UTC |
| 117 |
|
| 118 |
There's no ``utc`` flag anymore, so any time information missing timezone |
| 119 |
will be expected to be in UTC time. |
| 120 |
|
| 121 |
- Body is only for language specific data. |
| 122 |
|
| 123 |
- Python stores args/kwargs and embedded signatures in body. |
| 124 |
|
| 125 |
- If a message uses raw encoding then the raw data |
| 126 |
will be passed as a single argument to the function. |
| 127 |
|
| 128 |
- Java/C, etc. can use a Thrift/protobuf document as the body |
| 129 |
|
| 130 |
- ``origin`` is the name of the node sending the task. |
| 131 |
|
| 132 |
- Dispatches to actor based on ``task``, ``meth`` headers |
| 133 |
|
| 134 |
``meth`` is unused by Python, but may be used in the future |
| 135 |
to specify class+method pairs. |
| 136 |
|
| 137 |
- Chain gains a dedicated field. |
| 138 |
|
| 139 |
Reducing the chain into a recursive ``callbacks`` argument |
| 140 |
causes problems when the recursion limit is exceeded. |
| 141 |
|
| 142 |
This is fixed in the new message protocol by specifying |
| 143 |
a list of signatures, each task will then pop a task off the list |
| 144 |
when sending the next message: |
| 145 |
|
| 146 |
.. code-block:: python |
| 147 |
|
| 148 |
execute_task(message) |
| 149 |
chain = embed['chain'] |
| 150 |
if chain: |
| 151 |
sig = maybe_signature(chain.pop()) |
| 152 |
sig.apply_async(chain=chain) |
| 153 |
|
| 154 |
- ``correlation_id`` replaces ``task_id`` field. |
| 155 |
|
| 156 |
- ``root_id`` and ``parent_id`` fields helps keep track of work-flows. |
| 157 |
|
| 158 |
- ``shadow`` lets you specify a different name for logs, monitors |
| 159 |
can be used for concepts like tasks that calls a function |
| 160 |
specified as argument: |
| 161 |
|
| 162 |
.. code-block:: python |
| 163 |
|
| 164 |
from celery.utils.imports import qualname |
| 165 |
|
| 166 |
class PickleTask(Task): |
| 167 |
|
| 168 |
def unpack_args(self, fun, args=()): |
| 169 |
return fun, args |
| 170 |
|
| 171 |
def apply_async(self, args, kwargs, **options): |
| 172 |
fun, real_args = self.unpack_args(*args) |
| 173 |
return super().apply_async( |
| 174 |
(fun, real_args, kwargs), shadow=qualname(fun), **options |
| 175 |
) |
| 176 |
|
| 177 |
@app.task(base=PickleTask) |
| 178 |
def call(fun, args, kwargs): |
| 179 |
return fun(*args, **kwargs) |
| 180 |
|
| 181 |
|
| 182 |
.. _message-protocol-task-v1: |
| 183 |
.. _task-message-protocol-v1: |
| 184 |
|
| 185 |
Version 1 |
| 186 |
--------- |
| 187 |
|
| 188 |
In version 1 of the protocol all fields are stored in the message body: |
| 189 |
meaning workers and intermediate consumers must deserialize the payload |
| 190 |
to read the fields. |
| 191 |
|
| 192 |
Message body |
| 193 |
~~~~~~~~~~~~ |
| 194 |
|
| 195 |
* ``task`` |
| 196 |
:`string`: |
| 197 |
|
| 198 |
Name of the task. **required** |
| 199 |
|
| 200 |
* ``id`` |
| 201 |
:`string`: |
| 202 |
|
| 203 |
Unique id of the task (UUID). **required** |
| 204 |
|
| 205 |
* ``args`` |
| 206 |
:`list`: |
| 207 |
|
| 208 |
List of arguments. Will be an empty list if not provided. |
| 209 |
|
| 210 |
* ``kwargs`` |
| 211 |
:`dictionary`: |
| 212 |
|
| 213 |
Dictionary of keyword arguments. Will be an empty dictionary if not |
| 214 |
provided. |
| 215 |
|
| 216 |
* ``retries`` |
| 217 |
:`int`: |
| 218 |
|
| 219 |
Current number of times this task has been retried. |
| 220 |
Defaults to `0` if not specified. |
| 221 |
|
| 222 |
* ``eta`` |
| 223 |
:`string` (ISO 8601): |
| 224 |
|
| 225 |
Estimated time of arrival. This is the date and time in ISO 8601 |
| 226 |
format. If not provided the message isn't scheduled, but will be |
| 227 |
executed asap. |
| 228 |
|
| 229 |
* ``expires`` |
| 230 |
:`string` (ISO 8601): |
| 231 |
|
| 232 |
.. versionadded:: 2.0.2 |
| 233 |
|
| 234 |
Expiration date. This is the date and time in ISO 8601 format. |
| 235 |
If not provided the message will never expire. The message |
| 236 |
will be expired when the message is received and the expiration date |
| 237 |
has been exceeded. |
| 238 |
|
| 239 |
* ``taskset`` |
| 240 |
:`string`: |
| 241 |
|
| 242 |
The group this task is part of (if any). |
| 243 |
|
| 244 |
* ``chord`` |
| 245 |
:`Signature`: |
| 246 |
|
| 247 |
.. versionadded:: 2.3 |
| 248 |
|
| 249 |
Signifies that this task is one of the header parts of a chord. The value |
| 250 |
of this key is the body of the cord that should be executed when all of |
| 251 |
the tasks in the header has returned. |
| 252 |
|
| 253 |
* ``utc`` |
| 254 |
:`bool`: |
| 255 |
|
| 256 |
.. versionadded:: 2.5 |
| 257 |
|
| 258 |
If true time uses the UTC timezone, if not the current local timezone |
| 259 |
should be used. |
| 260 |
|
| 261 |
* ``callbacks`` |
| 262 |
:`<list>Signature`: |
| 263 |
|
| 264 |
.. versionadded:: 3.0 |
| 265 |
|
| 266 |
A list of signatures to call if the task exited successfully. |
| 267 |
|
| 268 |
* ``errbacks`` |
| 269 |
:`<list>Signature`: |
| 270 |
|
| 271 |
.. versionadded:: 3.0 |
| 272 |
|
| 273 |
A list of signatures to call if an error occurs while executing the task. |
| 274 |
|
| 275 |
* ``timelimit`` |
| 276 |
:`<tuple>(float, float)`: |
| 277 |
|
| 278 |
.. versionadded:: 3.1 |
| 279 |
|
| 280 |
Task execution time limit settings. This is a tuple of hard and soft time |
| 281 |
limit value (`int`/`float` or :const:`None` for no limit). |
| 282 |
|
| 283 |
Example value specifying a soft time limit of 3 seconds, and a hard time |
| 284 |
limit of 10 seconds:: |
| 285 |
|
| 286 |
{'timelimit': (3.0, 10.0)} |
| 287 |
|
| 288 |
|
| 289 |
Example message |
| 290 |
~~~~~~~~~~~~~~~ |
| 291 |
|
| 292 |
This is an example invocation of a `celery.task.ping` task in json |
| 293 |
format: |
| 294 |
|
| 295 |
.. code-block:: javascript |
| 296 |
|
| 297 |
{"id": "4cc7438e-afd4-4f8f-a2f3-f46567e7ca77", |
| 298 |
"task": "celery.task.PingTask", |
| 299 |
"args": [], |
| 300 |
"kwargs": {}, |
| 301 |
"retries": 0, |
| 302 |
"eta": "2009-11-17T12:30:56.527191"} |
| 303 |
|
| 304 |
Task Serialization |
| 305 |
------------------ |
| 306 |
|
| 307 |
Several types of serialization formats are supported using the |
| 308 |
`content_type` message header. |
| 309 |
|
| 310 |
The MIME-types supported by default are shown in the following table. |
| 311 |
|
| 312 |
=============== ================================= |
| 313 |
Scheme MIME Type |
| 314 |
=============== ================================= |
| 315 |
json application/json |
| 316 |
yaml application/x-yaml |
| 317 |
pickle application/x-python-serialize |
| 318 |
msgpack application/x-msgpack |
| 319 |
=============== ================================= |
| 320 |
|
| 321 |
.. _message-protocol-event: |
| 322 |
|
| 323 |
Event Messages |
| 324 |
============== |
| 325 |
|
| 326 |
Event messages are always JSON serialized and can contain arbitrary message |
| 327 |
body fields. |
| 328 |
|
| 329 |
Since version 4.0. the body can consist of either a single mapping (one event), |
| 330 |
or a list of mappings (multiple events). |
| 331 |
|
| 332 |
There are also standard fields that must always be present in an event |
| 333 |
message: |
| 334 |
|
| 335 |
Standard body fields |
| 336 |
-------------------- |
| 337 |
|
| 338 |
- *string* ``type`` |
| 339 |
|
| 340 |
The type of event. This is a string containing the *category* and |
| 341 |
*action* separated by a dash delimiter (e.g., ``task-succeeded``). |
| 342 |
|
| 343 |
- *string* ``hostname`` |
| 344 |
|
| 345 |
The fully qualified hostname of where the event occurred at. |
| 346 |
|
| 347 |
- *unsigned long long* ``clock`` |
| 348 |
|
| 349 |
The logical clock value for this event (Lamport time-stamp). |
| 350 |
|
| 351 |
- *float* ``timestamp`` |
| 352 |
|
| 353 |
The UNIX time-stamp corresponding to the time of when the event occurred. |
| 354 |
|
| 355 |
- *signed short* ``utcoffset`` |
| 356 |
|
| 357 |
This field describes the timezone of the originating host, and is |
| 358 |
specified as the number of hours ahead of/behind UTC (e.g., -2 or |
| 359 |
+1). |
| 360 |
|
| 361 |
- *unsigned long long* ``pid`` |
| 362 |
|
| 363 |
The process id of the process the event originated in. |
| 364 |
|
| 365 |
Standard event types |
| 366 |
-------------------- |
| 367 |
|
| 368 |
For a list of standard event types and their fields see the |
| 369 |
:ref:`event-reference`. |
| 370 |
|
| 371 |
Example message |
| 372 |
--------------- |
| 373 |
|
| 374 |
This is the message fields for a ``task-succeeded`` event: |
| 375 |
|
| 376 |
.. code-block:: python |
| 377 |
|
| 378 |
properties = { |
| 379 |
'routing_key': 'task.succeeded', |
| 380 |
'exchange': 'celeryev', |
| 381 |
'content_type': 'application/json', |
| 382 |
'content_encoding': 'utf-8', |
| 383 |
'delivery_mode': 1, |
| 384 |
} |
| 385 |
headers = { |
| 386 |
'hostname': '[email protected]', |
| 387 |
} |
| 388 |
body = { |
| 389 |
'type': 'task-succeeded', |
| 390 |
'hostname': '[email protected]', |
| 391 |
'pid': 6335, |
| 392 |
'clock': 393912923921, |
| 393 |
'timestamp': 1401717709.101747, |
| 394 |
'utcoffset': -1, |
| 395 |
'uuid': '9011d855-fdd1-4f8f-adb3-a413b499eafb', |
| 396 |
'retval': '4', |
| 397 |
'runtime': 0.0003212, |
| 398 |
) |