1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
.. _broker-sqs:
==================
Using Amazon SQS
==================
.. _broker-sqs-installation:
Installation
============
For the Amazon SQS support you have to install additional dependencies.
You can install both Celery and these dependencies in one go using
the ``celery[sqs]`` :ref:`bundle <bundles>`:
.. code-block:: console
$ pip install "celery[sqs]"
.. _broker-sqs-configuration:
Configuration
=============
You have to specify SQS in the broker URL::
broker_url = 'sqs://ABCDEFGHIJKLMNOPQRST:ZYXK7NiynGlTogH8Nj+P9nlE73sq3@'
where the URL format is:
.. code-block:: text
sqs://aws_access_key_id:aws_secret_access_key@
Please note that you must remember to include the ``@`` sign at the end and
encode the password so it can always be parsed correctly. For example:
.. code-block:: python
from kombu.utils.url import safequote
aws_access_key = safequote("ABCDEFGHIJKLMNOPQRST")
aws_secret_key = safequote("ZYXK7NiynG/TogH8Nj+P9nlE73sq3")
broker_url = "sqs://{aws_access_key}:{aws_secret_key}@".format(
aws_access_key=aws_access_key, aws_secret_key=aws_secret_key,
)
.. warning::
Don't use this setup option with django's ``debug=True``.
It may lead to security issues within deployed django apps.
In debug mode django shows environment variables and the SQS URL
may be exposed to the internet including your AWS access and secret keys.
Please turn off debug mode on your deployed django application or
consider a setup option described below.
The login credentials can also be set using the environment variables
:envvar:`AWS_ACCESS_KEY_ID` and :envvar:`AWS_SECRET_ACCESS_KEY`,
in that case the broker URL may only be ``sqs://``.
If you are using IAM roles on instances, you can set the BROKER_URL to:
``sqs://`` and kombu will attempt to retrieve access tokens from the instance
metadata.
Options
=======
Region
------
The default region is ``us-east-1`` but you can select another region
by configuring the :setting:`broker_transport_options` setting::
broker_transport_options = {'region': 'eu-west-1'}
.. seealso::
An overview of Amazon Web Services regions can be found here:
http://aws.amazon.com/about-aws/globalinfrastructure/
.. _sqs-visibility-timeout:
Visibility Timeout
------------------
The visibility timeout defines the number of seconds to wait
for the worker to acknowledge the task before the message is redelivered
to another worker. Also see caveats below.
This option is set via the :setting:`broker_transport_options` setting::
broker_transport_options = {'visibility_timeout': 3600} # 1 hour.
The default visibility timeout is 30 minutes.
This option is used when creating the SQS queue and has no effect if
using :ref:`predefined queues <predefined-queues>`.
Polling Interval
----------------
The polling interval decides the number of seconds to sleep between
unsuccessful polls. This value can be either an int or a float.
By default the value is *one second*: this means the worker will
sleep for one second when there's no more messages to read.
You must note that **more frequent polling is also more expensive, so increasing
the polling interval can save you money**.
The polling interval can be set via the :setting:`broker_transport_options`
setting::
broker_transport_options = {'polling_interval': 0.3}
Very frequent polling intervals can cause *busy loops*, resulting in the
worker using a lot of CPU time. If you need sub-millisecond precision you
should consider using another transport, like `RabbitMQ <broker-amqp>`,
or `Redis <broker-redis>`.
Long Polling
------------
`SQS Long Polling`_ is enabled by default and the ``WaitTimeSeconds`` parameter
of `ReceiveMessage`_ operation is set to 10 seconds.
The value of ``WaitTimeSeconds`` parameter can be set via the
:setting:`broker_transport_options` setting::
broker_transport_options = {'wait_time_seconds': 15}
Valid values are 0 to 20. Note that newly created queues themselves (also if
created by Celery) will have the default value of 0 set for the "Receive Message
Wait Time" queue property.
.. _`SQS Long Polling`: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html
.. _`ReceiveMessage`: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html
Queue Prefix
------------
By default Celery won't assign any prefix to the queue names,
If you have other services using SQS you can configure it do so
using the :setting:`broker_transport_options` setting::
broker_transport_options = {'queue_name_prefix': 'celery-'}
.. _predefined-queues:
Predefined Queues
-----------------
If you want Celery to use a set of predefined queues in AWS, and to
never attempt to list SQS queues, nor attempt to create or delete them,
pass a map of queue names to URLs using the :setting:`predefined_queues`
setting::
broker_transport_options = {
'predefined_queues': {
'my-q': {
'url': 'https://ap-southeast-2.queue.amazonaws.com/123456/my-q',
'access_key_id': 'xxx',
'secret_access_key': 'xxx',
}
}
}
When using this option, the visibility timeout should be set in the SQS queue
(in AWS) rather than via the :ref:`visibility timeout <sqs-visibility-timeout>`
option.
Back-off policy
------------------------
Back-off policy is using SQS visibility timeout mechanism altering the time difference between task retries.
The mechanism changes message specific ``visibility timeout`` from queue ``Default visibility timeout`` to policy configured timeout.
The number of retries is managed by SQS (specifically by the ``ApproximateReceiveCount`` message attribute) and no further action is required by the user.
Configuring the queues and backoff policy::
broker_transport_options = {
'predefined_queues': {
'my-q': {
'url': 'https://ap-southeast-2.queue.amazonaws.com/123456/my-q',
'access_key_id': 'xxx',
'secret_access_key': 'xxx',
'backoff_policy': {1: 10, 2: 20, 3: 40, 4: 80, 5: 320, 6: 640},
'backoff_tasks': ['svc.tasks.tasks.task1']
}
}
}
``backoff_policy`` dictionary where key is number of retries, and value is delay seconds between retries (i.e
SQS visibility timeout)
``backoff_tasks`` list of task names to apply the above policy
The above policy:
+-----------------------------------------+--------------------------------------------+
| **Attempt** | **Delay** |
+-----------------------------------------+--------------------------------------------+
| ``2nd attempt`` | 20 seconds |
+-----------------------------------------+--------------------------------------------+
| ``3rd attempt`` | 40 seconds |
+-----------------------------------------+--------------------------------------------+
| ``4th attempt`` | 80 seconds |
+-----------------------------------------+--------------------------------------------+
| ``5th attempt`` | 320 seconds |
+-----------------------------------------+--------------------------------------------+
| ``6th attempt`` | 640 seconds |
+-----------------------------------------+--------------------------------------------+
STS token authentication
----------------------------
https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role.html
AWS STS authentication is supported by using the ``sts_role_arn`` and ``sts_token_timeout`` broker transport options. ``sts_role_arn`` is the assumed IAM role ARN we use to authorize our access to SQS.
``sts_token_timeout`` is the token timeout, defaults (and minimum) to 900 seconds. After the mentioned period, a new token will be created::
broker_transport_options = {
'predefined_queues': {
'my-q': {
'url': 'https://ap-southeast-2.queue.amazonaws.com/123456/my-q',
'access_key_id': 'xxx',
'secret_access_key': 'xxx',
'backoff_policy': {1: 10, 2: 20, 3: 40, 4: 80, 5: 320, 6: 640},
'backoff_tasks': ['svc.tasks.tasks.task1']
}
},
'sts_role_arn': 'arn:aws:iam::<xxx>:role/STSTest', # optional
'sts_token_timeout': 900 # optional
}
.. _sqs-caveats:
Caveats
=======
- If a task isn't acknowledged within the ``visibility_timeout``,
the task will be redelivered to another worker and executed.
This causes problems with ETA/countdown/retry tasks where the
time to execute exceeds the visibility timeout; in fact if that
happens it will be executed again, and again in a loop.
So you have to increase the visibility timeout to match
the time of the longest ETA you're planning to use.
Note that Celery will redeliver messages at worker shutdown,
so having a long visibility timeout will only delay the redelivery
of 'lost' tasks in the event of a power failure or forcefully terminated
workers.
Periodic tasks won't be affected by the visibility timeout,
as it is a concept separate from ETA/countdown.
The maximum visibility timeout supported by AWS as of this writing
is 12 hours (43200 seconds)::
broker_transport_options = {'visibility_timeout': 43200}
- SQS doesn't yet support worker remote control commands.
- SQS doesn't yet support events, and so cannot be used with
:program:`celery events`, :program:`celerymon`, or the Django Admin
monitor.
- With FIFO queues it might be necessary to set additional message properties such as ``MessageGroupId`` and ``MessageDeduplicationId`` when publishing a message.
Message properties can be passed as keyword arguments to :meth:`~celery.app.task.Task.apply_async`:
.. code-block:: python
message_properties = {
'MessageGroupId': '<YourMessageGroupId>',
'MessageDeduplicationId': '<YourMessageDeduplicationId>'
}
task.apply_async(**message_properties)
- During :ref:`shutdown <worker-stopping>`, the worker will attempt to re-queue any unacknowledged messages
with :setting:`task_acks_late` enabled. However, if the worker is terminated forcefully
(:ref:`cold shutdown <worker-cold-shutdown>`), the worker might not be able to re-queue the tasks on time,
and they will not be consumed again until the :ref:`sqs-visibility-timeout` has passed. This creates a
problem when the :ref:`sqs-visibility-timeout` is very high and a worker needs to shut down just after it has
received a task. If the task is not re-queued in such case, it will need to wait for the long visibility timeout
to pass before it can be consumed again, leading to potentially very long delays in tasks execution.
The :ref:`soft shutdown <worker-soft-shutdown>` introduces a time-limited warm shutdown phase just before
the :ref:`cold shutdown <worker-cold-shutdown>`. This time window significantly increases the chances of
re-queuing the tasks during shutdown which mitigates the problem of long visibility timeouts.
To enable the :ref:`soft shutdown <worker-soft-shutdown>`, set the :setting:`worker_soft_shutdown_timeout` to a value
greater than 0. The value must be an float describing the number of seconds. During this time, the worker will
continue to process the running tasks until the timeout expires, after which the :ref:`cold shutdown <worker-cold-shutdown>`
will be initiated automatically to terminate the worker gracefully.
If the :ref:`REMAP_SIGTERM <worker-REMAP_SIGTERM>` is configured to SIGQUIT in the environment variables, and
the :setting:`worker_soft_shutdown_timeout` is set, the worker will initiate the :ref:`soft shutdown <worker-soft-shutdown>`
when it receives the :sig:`TERM` signal (*and* the :sig:`QUIT` signal).
.. _sqs-results-configuration:
Results
-------
Multiple products in the Amazon Web Services family could be a good candidate
to store or publish results with, but there's no such result backend included
at this point.
.. warning::
Don't use the ``amqp`` result backend with SQS.
It will create one queue for every task, and the queues will
not be collected. This could cost you money that would be better
spent contributing an AWS result store backend back to Celery :)
|