django 为什么celery worker一直尝试连接amqp,即使代理是sqs?

nbewdwxp  于 2023-07-01  发布在  Go
关注(0)|答案(2)|浏览(127)

我尝试通过设置和直接从celery文件配置broker。适用于以下celery 的设置。

AWS_SQS_SECRET = os.environ.get("AWS_SQS_SECRET")

broker_url = 'sqs://%s:%s@' % (AWS_SQS_ACCESS, AWS_SQS_SECRET)

task_default_queue = os.environ.get("DEFAULT_QUEUE")

AWS_SQS_REGION = os.environ.get("AWS_REGION")

broker_backend = "SQS"
broker_transport_options = {
    "region": AWS_SQS_REGION,
    # 'queue_name_prefix': '%s-' % 'dev' , # os.environ.get('ENVIRONMENT', 'development'),
    'visibility_timeout': 7200,
    'polling_interval': 1,
 
}
accept_content = ['application/json']
result_serializer = 'json'
task_serializer = 'json'

另外,正如我提到的,我尝试直接从celery文件进行配置。

import os
from celery import Celery
from celery.schedules import crontab
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyApp.settings')

AWS_SQS_ACCESS = os.environ.get("AWS_SQS_ACCESS")
AWS_SQS_SECRET = os.environ.get("AWS_SQS_SECRET")

app = Celery('MyApp') #,, broker='sqs://%s:%s@' % (AWS_SQS_ACCESS, AWS_SQS_SECRET), backend='django-db'
# app.config_from_object('django.conf:settings') #, namespace='CELERY'

CELERY_CONFIG = {
    "CELERY_TASK_SERIALIZER": "json",
    "CELERY_ACCEPT_CONTENT": ["json"],
    "CELERY_RESULT_SERIALIZER": "json",
    "CELERY_RESULT_BACKEND": None,
    "CELERY_TIMEZONE": "America/Sao_Paulo",
    "CELERY_ENABLE_UTC": True,
    "CELERY_ENABLE_REMOTE_CONTROL": False,
}

BROKER_URL = 'sqs://%s:%s@' % (AWS_SQS_ACCESS, AWS_SQS_SECRET)

CELERY_CONFIG.update(
    **{
        "BROKER_URL": BROKER_URL,
        "BROKER_TRANSPORT": "sqs",
        "BROKER_TRANSPORT_OPTIONS": {
            "region": "sa-east-1",
            "visibility_timeout": 3600,
            "polling_interval": 60,
        },
    }
)

app.conf.update(**CELERY_CONFIG)

app.autodiscover_tasks()

在elastik beanstalk上部署期间,我在服务中运行命令:

$PYTHONPATH/celery -A celery worker -Q default-dev -n default-worker \
         --logfile=/var/log/celery/celery-stdout-error.log --loglevel=DEBUG --concurrency=1

之前尝试运行:

$PYTHONPATH/celery -A MyApp worker -Q default-dev -n default-worker \
         --logfile=/var/log/celery/celery-stdout-error.log --loglevel=DEBUG --concurrency=1

但是出现错误,celery“无法加载应用程序MyApp”。
在日志文件中,我得到以下错误:

[2022-06-10 15:58:25,678: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
Trying again in 6.00 seconds... (3/100)

我的celery 版本是5.2.7

hc8w905p

hc8w905p1#

如果我正确理解了配置文档,我们应该如下设置config:

app.conf.broker_url = BROKER_URL
# or, alternatively
app.conf.update(broker_url=BROKER_URL)

即,使用小写名称而不是大写名称。

j13ufse2

j13ufse22#

如果您遇到同样的问题,请尝试重新启动celery 服务。

相关问题