【问题标题】:Django - Celery - SQS - S3 - Receiving MessagesDjango - Celery - SQS - S3 - 接收消息
【发布时间】:2020-03-04 16:45:06
【问题描述】:

我有一个 Django 应用程序,我正在使用 Celery、SQS 和 S3。 当我使用 Django、Celery 和 SQS 运行以下函数时,该函数可以正常工作,并且它应该每分钟打印一次“你好”。

from celery.task import periodic_task
from celery.schedules import crontab
@periodic_task(run_every=crontab(hour='*', minute='*', day_of_week="*"))
def print_hello():
    print('hello world')

但该应用还链接到 S3 存储桶。每当一个新文件被保存到S3 a notification is sent to the SQS queue。将通知消息发送到 SQS 队列时会出现此问题。当通知到达队列时,worker 失败。它停止周期性任务 print_hello(),给出以下错误消息:

[2019-11-07 22:10:57,173: CRITICAL/MainProcess] 不可恢复的错误: 错误('不正确的填充') ...parserinvoker/lib64/python3.7/base64.py",第 87 行,在 b64decode 返回 binascii.a2b_base64(s) binascii.Error: 不正确的填充

然后退出。我一直在查看文档,并且整个星期都在尝试排除故障,但没有找到解决方案。我将我的 settings.py 包括在内,以防是配置问题

设置.py

BROKER_URL = "sqs://"
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_DEFAULT_QUEUE = env('CELERY_DEFAULT_QUEUE')
CELERY_RESULT_BACKEND = None 
BROKER_TRANSPORT_OPTIONS = {
    'region': 'us-east-1',
    'polling_interval':20,
    'visibility_timeout': 3600,
    'task_default_queue': env('CELERY_DEFAULT_QUEUE'),
}

【问题讨论】:

    标签: django amazon-s3 celery amazon-sqs django-celery


    【解决方案1】:

    celery 期望队列中的 json payload 格式与 SQS 从 s3 接收的格式不同;为了正确处理这些,您可能需要一个单独的定期任务来定期检查这些并排空 s3 通知队列,而不是将 s3 通知发送到 celery 代理队列。 s3 消息正文将显示为described in the amazon documentation here。这是从 S3 发送到 SQS 的示例 2.1 记录:

       "Records":[  
          {  
             "eventVersion":"2.1",
             "eventSource":"aws:s3",
             "awsRegion":"us-west-2",
             "eventTime":The time, in ISO-8601 format, for example, 1970-01-01T00:00:00.000Z, when Amazon S3 finished processing the request,
             "eventName":"event-type",
             "userIdentity":{  
                "principalId":"Amazon-customer-ID-of-the-user-who-caused-the-event"
             },
             "requestParameters":{  
                "sourceIPAddress":"ip-address-where-request-came-from"
             },
             "responseElements":{  
                "x-amz-request-id":"Amazon S3 generated request ID",
                "x-amz-id-2":"Amazon S3 host that processed the request"
             },
             "s3":{  
                "s3SchemaVersion":"1.0",
                "configurationId":"ID found in the bucket notification configuration",
                "bucket":{  
                   "name":"bucket-name",
                   "ownerIdentity":{  
                      "principalId":"Amazon-customer-ID-of-the-bucket-owner"
                   },
                   "arn":"bucket-ARN"
                },
                "object":{  
                   "key":"object-key",
                   "size":object-size,
                   "eTag":"object eTag",
                   "versionId":"object version if bucket is versioning-enabled, otherwise null",
                   "sequencer": "a string representation of a hexadecimal value used to determine event sequence, 
                       only used with PUTs and DELETEs"
                }
             },
             "glacierEventData": {
                "restoreEventData": {
                   "lifecycleRestorationExpiryTime": "The time, in ISO-8601 format, for example, 1970-01-01T00:00:00.000Z, of Restore Expiry",
                   "lifecycleRestoreStorageClass": "Source storage class for restore"
                }
             }
          }
       ]
    }
    

    芹菜消息格式looks like this

    【讨论】:

    • 感谢您的解释。我不知道格式不同。我创建了一个单独的队列,到目前为止一切似乎都在工作。我有一个专门用于 Celery 的队列和一个用于接收 SQS 消息的队列。
    猜你喜欢
    • 1970-01-01
    • 2022-07-19
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多