简体   繁体   English

AWS lambda:超时时执行函数

[英]AWS lambda: Execute function on timeout

I am developing a lambda function that migrates logs from an SFTP server to an S3 bucket.我正在开发一个将日志从 SFTP 服务器迁移到 S3 存储桶的 lambda 函数。

Due to the size of the logs, the function sometimes is timing out - even though I have set the maximum timeout of 15 minutes.由于日志的大小,该功能有时会超时 - 即使我已将最大超时设置为 15 分钟。

try:
   logger.info(f'Migrating {log_name}... ')
   transfer_to_s3(log_name, sftp)
   logger.info(f'{log_name} was migrated succesfully ')

If transfer_to_s3() fails due to timeout logger.info(f'{log_name} was migrated succesfully') line won't be executed.如果transfer_to_s3()由于超时而失败logger.info(f'{log_name} was migrated succesfully')行将不会被执行。

I want to ensure that in this scenario, I will somehow know that a log was not migrated due to timeout.我想确保在这种情况下,我会以某种方式知道由于超时而未迁移日志。

Is there a way to force lambda to perform an action, before exiting, in the case of a timeout?在超时的情况下,有没有办法在退出之前强制 lambda 执行操作?

Probably a better way would be to use SQS for that:可能更好的方法是使用 SQS:

Logo info ---> SQS queue ---> Lambda function

If lambda successful moves the files, it removes the log info from SQS queue.如果 lambda 成功移动文件,它会从 SQS 队列中删除日志信息。 If it fails, the log info persists in the SQS queue (or goes to DLQ for special handling), so the next lambda invocation can handle it.如果失败,日志信息会保留在 SQS 队列中(或转到 DLQ 进行特殊处理),因此下一个 lambda 调用可以处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM