芹菜与亚马逊 SQS

发布于 2024-12-14 04:00:20 字数 302 浏览 5 评论 0原文

我想使用 Amazon SQS 作为 芹菜。 Celery 依赖于 Kombu 的 SQS 传输实现。但是没有足够的文档来使用它,所以我找不到如何在 Celery 上配置 SQS。有人在 Celery 上成功配置了 SQS 吗?

I want to use Amazon SQS as broker backed of Celery. There’s the SQS transport implementation for Kombu, which Celery depends on. However there is not enough documentation for using it, so I cannot find how to configure SQS on Celery. Is there somebody that had succeeded to configure SQS on Celery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

╰つ倒转 2024-12-21 04:00:20

我多次遇到这个问题,但仍然不完全确定如何设置 Celery 来与 SQS 一起使用。事实证明,使用最新版本的 Kombu 和 Celery 非常容易。作为另一个答案中提到的 BROKER_URL 语法的替代方案,您可以简单地设置传输、选项、用户和密码,如下所示:

BROKER_TRANSPORT = 'sqs'
BROKER_TRANSPORT_OPTIONS = {
    'region': 'us-east-1',
}
BROKER_USER = AWS_ACCESS_KEY_ID
BROKER_PASSWORD = AWS_SECRET_ACCESS_KEY

这解决了 URL 解析器据称存在的问题,该解析器不允许在 API 密钥中使用正斜杠,这在 AWS 中似乎是相当常见的情况。由于似乎还没有关于该主题的大量信息,因此我还在这里写了一篇关于该主题的简短博客文章:

http://www.caktusgroup.com/blog/2011/12/19/using-django-and-celery-amazon-sqs/

I ran into this question several times but still wasn't entirely sure how to setup Celery to work with SQS. It turns out that it is quite easy with the latest versions of Kombu and Celery. As an alternative to the BROKER_URL syntax mentioned in another answer, you can simply set the transport, options, user, and password like so:

BROKER_TRANSPORT = 'sqs'
BROKER_TRANSPORT_OPTIONS = {
    'region': 'us-east-1',
}
BROKER_USER = AWS_ACCESS_KEY_ID
BROKER_PASSWORD = AWS_SECRET_ACCESS_KEY

This gets around a purported issue with the URL parser that doesn't allow forward slashes in your API secret, which seems to be a fairly common occurrence with AWS. Since there didn't seem to be a wealth of information out there about the topic yet, I also wrote a short blog post on the topic here:

http://www.caktusgroup.com/blog/2011/12/19/using-django-and-celery-amazon-sqs/

街道布景 2024-12-21 04:00:20

我正在使用 Celery 3.0,并且在使用 BROKER_USER / BROKER_PASSWORD 设置启动工作程序时收到弃用警告。

我查看了 kombo.utils.url._parse_url 中的 SQS URL 解析,它在 URL 的用户名和密码元素上调用 urllib.unquote 。

因此,为了解决带有正斜杠的密钥问题,我能够成功地将以下内容用于 BROKER_URL:

import urllib
BROKER_URL = 'sqs://%s:%s@' % (urllib.quote(AWS_ACCESS_KEY_ID, safe=''),
                               urllib.quote(AWS_SECRET_ACCESS_KEY, safe=''))

我不确定访问密钥是否可以在其中包含正斜杠,但将其引用为也无伤大雅出色地。

I'm using Celery 3.0 and was getting deprecation warnings when launching the worker with the BROKER_USER / BROKER_PASSWORD settings.

I took a look at the SQS URL parsing in kombo.utils.url._parse_url and it is calling urllib.unquote on the username and password elements of the URL.

So, to workaround the issue of secret keys with forward slashes, I was able to successfully use the following for the BROKER_URL:

import urllib
BROKER_URL = 'sqs://%s:%s@' % (urllib.quote(AWS_ACCESS_KEY_ID, safe=''),
                               urllib.quote(AWS_SECRET_ACCESS_KEY, safe=''))

I'm not sure if access keys can ever have forward slashes in them but it doesn't hurt to quote it as well.

小帐篷 2024-12-21 04:00:20

对于任何偶然发现这个问题的人,我能够让 Celery 与 SQS 一起开箱即用(无需修补),但我确实需要更新到 Celery 和 Kombu 的最新版本才能正常工作(1.4.0)。 5 和 1.5.1 截至目前)。使用上面的配置行,它应该可以工作(尽管您可能想要更改默认区域)。

问题:为了使用上面的 URL 格式,您需要确保您的 AWS 密钥不包含斜杠,因为这会使 URL 解析器感到困惑。只要不断生成新的秘密,直到得到一个没有斜杠的秘密。

For anybody stumbling upon this question, I was able to get Celery working out-of-the-box with SQS (no patching required), but I did need to update to the latest versions of Celery and Kombu for this to work (1.4.5 and 1.5.1 as of now). Use the config lines above and it should work (although you'll probably want to change the default region).

Gotcha: in order to use the URL format above, you need to make sure your AWS secret doesn't contain slashes, as this confuses the URL parser. Just keep generating new secrets until you get one without a slash.

初见 2024-12-21 04:00:20

没有人回答这个问题。无论如何,我尝试使用 Amazon SQS 配置 Celery,看来我取得了小小的成功。

Kombu 应该为此打补丁,所以我写了一些补丁,并且有我的拉取请求作为出色地。您可以通过在修补的 Kombu 上的 Celery 中设置 sqs:// 方案的 BROKER_URL 来配置 Amazon SQS。例如:

BROKER_URL = 'sqs://AWS_ACCESS:AWS_SECRET@:80//'
BROKER_TRANSPORT_OPTIONS = {
    'region': 'ap-northeast-1',
    'sdb_persistence': False
}

Nobody answered about this. Anyway I tried to configure Celery with Amazon SQS, and it seems I achieved a small success.

Kombu should be patched for this, so I wrote some patches and there is my pull request as well. You can configure Amazon SQS by setting BROKER_URL of sqs:// scheme in Celery on the patched Kombu. For example:

BROKER_URL = 'sqs://AWS_ACCESS:AWS_SECRET@:80//'
BROKER_TRANSPORT_OPTIONS = {
    'region': 'ap-northeast-1',
    'sdb_persistence': False
}
萌逼全场 2024-12-21 04:00:20

我能够使用 kombu 在 celery 4.3 (python 3.7) 上配置 SQS。

from kombu.utils.url import quote

CELERY_BROKER_URL = 'sqs://{AWS_ACCESS_KEY_ID}:{AWS_SECRET_ACCESS_KEY}@'.format(
    AWS_ACCESS_KEY_ID=quote(AWS_ACCESS_KEY_ID, safe=''),
    AWS_SECRET_ACCESS_KEY=quote(AWS_SECRET_ACCESS_KEY, safe='')
)

I was able to configure SQS on celery 4.3 (python 3.7) by using kombu.

from kombu.utils.url import quote

CELERY_BROKER_URL = 'sqs://{AWS_ACCESS_KEY_ID}:{AWS_SECRET_ACCESS_KEY}@'.format(
    AWS_ACCESS_KEY_ID=quote(AWS_ACCESS_KEY_ID, safe=''),
    AWS_SECRET_ACCESS_KEY=quote(AWS_SECRET_ACCESS_KEY, safe='')
)
萌能量女王 2024-12-21 04:00:20

我在 IAM 控制台中重新生成了凭证,直到获得不带斜线的密钥 (/)。解析问题仅与该字符有关,因此,如果您的秘密没有该字符,那么您会没事的。

这不是最优雅的解决方案,但绝对可以保持代码免受黑客攻击。

I regenerated the credentials in the IAM consonle until I got a key without a slash (/). The parsing issues are only with that character, so if your secret doesn't have one you'll be fine.

Not the most terribly elegant solution, but definitely keeps the code clean of hacks.

ζ澈沫 2024-12-21 04:00:20

Python 3 的更新,从 AWS KEY 中删除反斜杠。

from urllib.parse import quote_plus
BROKER_URL = 'sqs://{}:{}@'.format(
    quote_plus(AWS_ACCESS_KEY_ID), 
    quote_plus(AWS_SECRET_ACCESS_KEY)
)

Update for Python 3, removing backslashes from the AWS KEY.

from urllib.parse import quote_plus
BROKER_URL = 'sqs://{}:{}@'.format(
    quote_plus(AWS_ACCESS_KEY_ID), 
    quote_plus(AWS_SECRET_ACCESS_KEY)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文