AWS Lambda 挂起,直到它在条带发票列表上超时

AWS Lambda hang until it timout on stipe invoices list

我正在使用 AWS Lambda 来托管一个 nodeJs 服务,该服务在 Stripe 上获取我的未结发票并执行付款并更新我的数据库。 问题是大部分时间,但不是所有时间(有时一切都按预期进行),它挂在发票列表的调用上,什么都不做。

这是日志停止的代码部分:

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY, {
    maxNetworkRetries: 1,
    timeout: 2000
});
[other imports]

const microservice = async (event, context, callback) => {
    [some code including database connection]

    console.log('retrieving all open invoices...')
    let invoices;
    try {
        invoices = await stripe.invoices.list({
            status: 'open',
            limit: 100,
        });
        console.log(invoices.data.length + ' data retrieved.');
    } catch (error) {
        console.log('Unable fetch stripe invoices : ', error);
        console.log('Exiting due to stripe connection error.');
        reports.push(new Report('Unable fetch stripe invoices', 'ERROR'));
        return {
        statusCode: 500,
        };
    }
    
    [code that process invoices]

    return {};
};
module.exports.microservice = microservice;

这里是日志输出:

START RequestId: d628aa1e-dee6-4cc6-9ce0-f7c11cf73249 Version: $LATEST
2021-10-26T00:04:05.741Z d628aa1e-dee6-4cc6-9ce0-f7c11cf73249 INFO Connecting to database...
2021-10-26T00:04:05.929Z d628aa1e-dee6-4cc6-9ce0-f7c11cf73249 INFO Executing (default): SELECT 1+1 AS result
2021-10-26T00:04:05.931Z d628aa1e-dee6-4cc6-9ce0-f7c11cf73249 INFO Connection has been established successfully.
2021-10-26T00:04:05.931Z d628aa1e-dee6-4cc6-9ce0-f7c11cf73249 INFO retrieving all open invoices...
END RequestId: d628aa1e-dee6-4cc6-9ce0-f7c11cf73249
REPORT RequestId: d628aa1e-dee6-4cc6-9ce0-f7c11cf73249 Duration: 15015.49 ms Billed Duration: 15000 ms Memory Size: 400 MB Max Memory Used: 40 MB
2021-10-26T00:04:20.754Z d628aa1e-dee6-4cc6-9ce0-f7c11cf73249 Task timed out after 15.02 seconds

当一切顺利时就是这样:

START RequestId: e5fb6b08-adf9-433f-b1da-fd9ec29dde31 Version: $LATEST
2021-10-25T14:35:03.369Z e5fb6b08-adf9-433f-b1da-fd9ec29dde31 INFO Connecting to database...
2021-10-25T14:35:03.590Z e5fb6b08-adf9-433f-b1da-fd9ec29dde31 INFO Executing (default): SELECT 1+1 AS result
2021-10-25T14:35:03.600Z e5fb6b08-adf9-433f-b1da-fd9ec29dde31 INFO Connection has been established successfully.
2021-10-25T14:35:03.600Z e5fb6b08-adf9-433f-b1da-fd9ec29dde31 INFO retrieving all open invoices...
2021-10-25T14:35:04.011Z e5fb6b08-adf9-433f-b1da-fd9ec29dde31 INFO 0 data retrieved.
2021-10-25T14:35:04.011Z e5fb6b08-adf9-433f-b1da-fd9ec29dde31 INFO Everything went smoothly !
END RequestId: e5fb6b08-adf9-433f-b1da-fd9ec29dde31
REPORT RequestId: e5fb6b08-adf9-433f-b1da-fd9ec29dde31 Duration: 646.58 ms Billed Duration: 647 ms Memory Size: 400 MB

我不明白为什么它没有错误或日志挂起...

网络问题的发生可能有多种原因。在你的情况下,你可以尝试做的是减少限制(例如 limit : 30)并设置你的客户端库通过设置 maxNetworkRetries : 3 或适合你的应用程序的数字再次重试连接。设置后,Stripe 将在发生超时错误时重试连接。

这是 Step functions use cases 的绝配。它将允许您协调获取发票和处理它们的步骤,并在出现错误时轻松设计重试机制。

这不是真正的解决方案,但导致问题的原因是 Stripe 需要很长时间才能 return 少于 100 个结果。

我们找到了不获取此列表的解决方法。