我有一个需要第三方的简单任务。
当请求到来时,我将其推送到amazon sqs
队列,将其拉入工作人员并调用3rd party
。如果超时,我想实现指数退避(在 2 秒内重试,然后是 4 秒,然后是 8 秒,然后......)
最大重试次数。
Using python
, boto -> sqs
我一直在寻找内置参数,以允许我用尽可能少的代码来做到这一点(理想情况下,根本没有代码).
就像是
from boto import sqs
def handle_message(message):
try:
# send a post to api
except TimeOut, err:
# send_back_to_itself in 2/4/8 sec
if delay < DELAY_LIMIT:
queue.write(message, delay=secs)
我认为从 2021 年夏季开始,“完全没有代码”是不可能的。但是有一篇很棒的博客文章介绍了如何通过编码示例来做到这一点https://aws.amazon.com/blogs/compute/using-amazon-sqs-dead-letter-queues-to-replay-messages/ https://aws.amazon.com/blogs/compute/using-amazon-sqs-dead-letter-queues-to-replay-messages/
上面链接中的代码示例:
def handler(event, context):
"""Lambda function handler."""
for record in event['Records']:
nbReplay = 0
# number of replay
if 'sqs-dlq-replay-nb' in record['messageAttributes']:
nbReplay = int(record['messageAttributes']['sqs-dlq-replay-nb']["stringValue"])
nbReplay += 1
if nbReplay > config.MAX_ATTEMPS:
raise MaxAttempsError(replay=nbReplay, max=config.MAX_ATTEMPS)
# SQS attributes
attributes = record['messageAttributes']
attributes.update({'sqs-dlq-replay-nb': {'StringValue': str(nbReplay), 'DataType': 'Number'}})
_sqs_attributes_cleaner(attributes)
# Backoff
b = backoff.ExpoBackoffFullJitter(base=config.BACKOFF_RATE, cap=config.MESSAGE_RETENTION_PERIOD)
delaySeconds = b.backoff(n=int(nbReplay))
# SQS
SQS.send_message(
QueueUrl=config.SQS_MAIN_URL,
MessageBody=record['body'],
DelaySeconds=int(delaySeconds),
MessageAttributes=record['messageAttributes']
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)