| 1 |
.. _broker-gcpubsub: |
| 2 |
|
| 3 |
===================== |
| 4 |
Using Google Pub/Sub |
| 5 |
===================== |
| 6 |
|
| 7 |
.. versionadded:: 5.5 |
| 8 |
|
| 9 |
.. _broker-gcpubsub-installation: |
| 10 |
|
| 11 |
Installation |
| 12 |
============ |
| 13 |
|
| 14 |
For the Google Pub/Sub support you have to install additional dependencies. |
| 15 |
You can install both Celery and these dependencies in one go using |
| 16 |
the ``celery[gcpubsub]`` :ref:`bundle <bundles>`: |
| 17 |
|
| 18 |
.. code-block:: console |
| 19 |
|
| 20 |
$ pip install "celery[gcpubsub]" |
| 21 |
|
| 22 |
.. _broker-gcpubsub-configuration: |
| 23 |
|
| 24 |
Configuration |
| 25 |
============= |
| 26 |
|
| 27 |
You have to specify gcpubsub and google project in the broker URL:: |
| 28 |
|
| 29 |
broker_url = 'gcpubsub://projects/project-id' |
| 30 |
|
| 31 |
where the URL format is: |
| 32 |
|
| 33 |
.. code-block:: text |
| 34 |
|
| 35 |
gcpubsub://projects/project-id |
| 36 |
|
| 37 |
Please note that you must prefix the project-id with `projects/` in the URL. |
| 38 |
|
| 39 |
The login credentials will be your regular GCP credentials set in the environment. |
| 40 |
|
| 41 |
Options |
| 42 |
======= |
| 43 |
|
| 44 |
Resource expiry |
| 45 |
--------------- |
| 46 |
|
| 47 |
The default settings are built to be as simple cost effective and intuitive as possible and to "just work". |
| 48 |
The pubsub messages and subscriptions are set to expire after 24 hours, and can be set |
| 49 |
by configuring the :setting:`expiration_seconds` setting:: |
| 50 |
|
| 51 |
expiration_seconds = 86400 |
| 52 |
|
| 53 |
.. seealso:: |
| 54 |
|
| 55 |
An overview of Google Cloud Pub/Sub settings can be found here: |
| 56 |
|
| 57 |
https://cloud.google.com/pubsub/docs |
| 58 |
|
| 59 |
.. _gcpubsub-ack_deadline_seconds: |
| 60 |
|
| 61 |
Ack Deadline Seconds |
| 62 |
-------------------- |
| 63 |
|
| 64 |
The `ack_deadline_seconds` defines the number of seconds pub/sub infra shall wait |
| 65 |
for the worker to acknowledge the task before the message is redelivered |
| 66 |
to another worker. |
| 67 |
|
| 68 |
This option is set via the :setting:`broker_transport_options` setting:: |
| 69 |
|
| 70 |
broker_transport_options = {'ack_deadline_seconds': 60} # 1 minute. |
| 71 |
|
| 72 |
The default visibility timeout is 240 seconds, and the worker takes care for |
| 73 |
automatically extending all pending messages it has. |
| 74 |
|
| 75 |
.. seealso:: |
| 76 |
|
| 77 |
An overview of Pub/Sub deadline can be found here: |
| 78 |
|
| 79 |
https://cloud.google.com/pubsub/docs/lease-management |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
Polling Interval |
| 84 |
---------------- |
| 85 |
|
| 86 |
The polling interval decides the number of seconds to sleep between |
| 87 |
unsuccessful polls. This value can be either an int or a float. |
| 88 |
By default the value is *0.1 seconds*. However it doesn't mean |
| 89 |
that the worker will bomb the Pub/Sub API every 0.1 seconds when there's no |
| 90 |
more messages to read, since it will be blocked by a blocking call to |
| 91 |
the Pub/Sub API, which will only return when there's a new message to read |
| 92 |
or after 10 seconds. |
| 93 |
|
| 94 |
The polling interval can be set via the :setting:`broker_transport_options` |
| 95 |
setting:: |
| 96 |
|
| 97 |
broker_transport_options = {'polling_interval': 0.3} |
| 98 |
|
| 99 |
Very frequent polling intervals can cause *busy loops*, resulting in the |
| 100 |
worker using a lot of CPU time. If you need sub-millisecond precision you |
| 101 |
should consider using another transport, like `RabbitMQ <broker-amqp>`, |
| 102 |
or `Redis <broker-redis>`. |
| 103 |
|
| 104 |
Queue Prefix |
| 105 |
------------ |
| 106 |
|
| 107 |
By default Celery will assign `kombu-` prefix to the queue names, |
| 108 |
If you have other services using Pub/Sub you can configure it do so |
| 109 |
using the :setting:`broker_transport_options` setting:: |
| 110 |
|
| 111 |
broker_transport_options = {'queue_name_prefix': 'kombu-'} |
| 112 |
|
| 113 |
.. _gcpubsub-results-configuration: |
| 114 |
|
| 115 |
Results |
| 116 |
------- |
| 117 |
|
| 118 |
Google Cloud Storage (GCS) could be a good candidate to store the results. |
| 119 |
See :ref:`gcs` for more information. |
| 120 |
|
| 121 |
|
| 122 |
Caveats |
| 123 |
======= |
| 124 |
|
| 125 |
- When using celery flower, an --inspect-timeout=10 option is required to |
| 126 |
detect workers state correctly. |
| 127 |
|
| 128 |
- GCP Subscriptions idle subscriptions (no queued messages) |
| 129 |
are configured to removal after 24hrs. |
| 130 |
This aims at reducing costs. |
| 131 |
|
| 132 |
- Queued and unacked messages are set to auto cleanup after 24 hrs. |
| 133 |
Same reason as above. |
| 134 |
|
| 135 |
- Channel queue size is approximation, and may not be accurate. |
| 136 |
The reason is that the Pub/Sub API does not provide a way to get the |
| 137 |
exact number of messages in a subscription. |
| 138 |
|
| 139 |
- Orphan (no subscriptions) Pub/Sub topics aren't being auto removed!! |
| 140 |
Since GCP introduces a hard limit of 10k topics per project, |
| 141 |
it is recommended to remove orphan topics manually in a periodic manner. |
| 142 |
|
| 143 |
- Max message size is limited to 10MB, as a workaround you can use GCS Backend to |
| 144 |
store the message in GCS and pass the GCS URL to the task. |