我已经从 Pub/Sub 主题创建了一个 Cloud Function,如果函数失败,我是否应该重试从 pub/sub 发送消息?
I've created a Cloud Function from Pub/Sub topic, shall I expect retry a delivery of message from pub/sub if function fails?
我有一个带有简单测试代码的函数,例如:
exports.helloPubSub = (event, context) => {
const message = event.data
? Buffer.from(event.data, 'base64').toString()
: 'Hello, World';
console.log(context);
throw new Error("Fail");
};
当我向 Pub/sub 发布消息时,该函数失败,我希望它会在 600 秒后再次调用相同的消息,因为 pub/sub 订阅的确认截止日期设置为 600 秒.
但是它并没有像预期的那样工作,尽管云函数失败了,但它似乎立即确认了消息。
根据文档:Cloud Functions 在成功执行函数后在内部确认消息。
您必须确保已启用该功能的重试。来自 "Retrying Background Functions" documentation(强调我的):
Cloud Functions guarantees at-least-once execution of a background
function for each event emitted by an event source. However, by
default, if a function invocation terminates with an error, the
function will not be invoked again, and the event will be dropped.
When you enable retries on a background function, Cloud Functions will
retry a failed function invocation until it completes successfully, or
the retry window (by default, 7 days) expires.
您可以通过在通过 gcloud 命令行工具创建函数时提供 --retry
选项或在通过 Cloud Console 创建时选中“失败时重试”框来启用重试。
当 Cloud Functions 失败并启用重试时,它会拒绝该消息,从而使该消息成为重新传递的候选者。要延迟 nacked 消息的重新传递,您可以在订阅上设置 RetryPolicy。要更新订阅以使用重试策略,请在 Cloud Console Pub/Sub 部分中找到 Cloud Functions 创建的订阅的名称。然后,例如,您可以发出此 gcloud
命令以将最小重试延迟设置为 1 秒,最大延迟设置为 60 秒:
gcloud pubsub subscriptions update <subscription name> --min-retry-delay=1s --max-retry-delay=60s
我有一个带有简单测试代码的函数,例如:
exports.helloPubSub = (event, context) => {
const message = event.data
? Buffer.from(event.data, 'base64').toString()
: 'Hello, World';
console.log(context);
throw new Error("Fail");
};
当我向 Pub/sub 发布消息时,该函数失败,我希望它会在 600 秒后再次调用相同的消息,因为 pub/sub 订阅的确认截止日期设置为 600 秒. 但是它并没有像预期的那样工作,尽管云函数失败了,但它似乎立即确认了消息。
根据文档:Cloud Functions 在成功执行函数后在内部确认消息。
您必须确保已启用该功能的重试。来自 "Retrying Background Functions" documentation(强调我的):
Cloud Functions guarantees at-least-once execution of a background function for each event emitted by an event source. However, by default, if a function invocation terminates with an error, the function will not be invoked again, and the event will be dropped. When you enable retries on a background function, Cloud Functions will retry a failed function invocation until it completes successfully, or the retry window (by default, 7 days) expires.
您可以通过在通过 gcloud 命令行工具创建函数时提供 --retry
选项或在通过 Cloud Console 创建时选中“失败时重试”框来启用重试。
当 Cloud Functions 失败并启用重试时,它会拒绝该消息,从而使该消息成为重新传递的候选者。要延迟 nacked 消息的重新传递,您可以在订阅上设置 RetryPolicy。要更新订阅以使用重试策略,请在 Cloud Console Pub/Sub 部分中找到 Cloud Functions 创建的订阅的名称。然后,例如,您可以发出此 gcloud
命令以将最小重试延迟设置为 1 秒,最大延迟设置为 60 秒:
gcloud pubsub subscriptions update <subscription name> --min-retry-delay=1s --max-retry-delay=60s