我有一个Django应用程序,正在使用Celery,SQS和S3。当我使用Django,Celery和SQS运行以下函数时,该函数有效,并且每分钟应输出“ hello”。
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队列时,就会发生此问题。当通知到达队列时,工作程序将失败。它会停止定期任务print_hello(),并显示以下错误消息:
[[2019-11-07 22:10:57,173:CRITICAL / MainProcess]无法恢复的错误:错误(“填充错误”)... parserinvoker / lib64 / python3.7 / base64.py“,第64行,第64行返回binascii.a2b_base64(s)binascii.Error:不正确的填充
然后退出。我一直在查看文档,并试图整周进行故障排除,但没有找到解决方案。如果包含配置问题,我将包含我的settings.py
Settings.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'),
}
芹菜在队列上期望的json负载格式与SQS从s3接收的格式不同;为了正确处理这些消息,您可能希望有一个单独的定期任务,该任务会定期检查这些消息并耗尽s3通知队列,而不是将s3通知发送到celery broker队列。 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。