如何在Google Cloud Functions中实现“运行一次并重试”(带有Firestore)

huangapple go评论69阅读模式
英文:

How to implement "run once with retry" in Google Cloud Functions (with Firestore)

问题

I understand your request. Here is the translated text:

我有一个异步的JavaScript函数(返回一个Promise),可以在Google Cloud Functions上运行。它接受一个数据对象作为参数。一旦我创建了数据对象,我需要让函数以此执行。当函数失败(Promise被拒绝时),必须在之后的1小时内再次运行,使用相同的数据对象,直到成功。

示例:函数名为sendEmail,数据对象包含收件人、主题和正文。我想发送邮件。但有时邮件服务器会出现问题,sendMail会失败。然后必须稍后再次尝试。

在Firebase项目中使用JavaScript Cloud Functions和Firestore实现这一点的正确方式是什么?

我在寻找一个方案,不一定是代码。

我认为将数据对象写入Firestore作为一个文档是一个良好的起点。然后,我可以使用onCreate触发器来启动第一次尝试,并使用每小时的定时触发器来进行后续尝试。但是我如何确保我的函数只运行一次?

英文:

I have an asynchronous JavaScript function (returns a Promise) which can run on Google Cloud Functions. It takes a data object as a parameter. Once I create the data object I need the function to execute with it. When the function fails (Promise reject is called) the function must run again (1 hour) later with the same data object. Until it succeeds.

Example: The function is sendEmail and the data object contains the recipient, subject, and text. I want to send the email. But sometimes the mail server is down and sendMail fails. Then it must be tried later.

What is the correct way to implement this in a Firebase project using JavaScript Cloud Functions and Firestore?

I am looking for a recipe, not necessarily code.

I assume writing the data object as a document to Firestore is a good start. Then I could have an onCreate trigger to initiate the first attempt and an hourly schedule trigger for the next attempts. But how do I ensure, my function runs only once?

答案1

得分: 1

以下是已经翻译好的部分:

使用Cloud Tasks在将来运行云函数,如文章中所述,将会起到作用。

在您的onCreate云函数代码的catch块中,您可以安排另一个云函数在故障后正好一小时后运行。这个云函数包含与onCreate云函数相同的代码(唯一的区别是触发方式,顺便说一下,您可以将代码编写成异步JS函数并从这两个CF中调用它)。

然而,很少见的情况是catch块中的代码也会失败...为了处理这种情况,您可以(a)在成功发送电子邮件后在Firestore文档中设置一个标志,并且(b)有一个标准定期调度的Cloud Function,定期检查是否有Firestore文档没有这个标志,并且创建时间超过一小时(或更长时间)。

英文:

Scheduling a Cloud Function to run in the future with Cloud Tasks as explained in this article would do the trick.

In the catch block of your onCreate Cloud Function code you schedule another Cloud Function to run exactly one hour after the failure. This Cloud Function contains the same code than the onCreate Cloud Function (the only difference is how it is triggered and, BTW, you can write the code in an async JS function and call it from the two CFs).

There is however the rare possibility that the code in the catch block fails as well... To manage this case you can (a) set a flag in the Firestore document upon email sending success and, (b) have a standard scheduled Cloud Function that regularly checks if there are Firestore documents without this flag and which were created more than one (or more) hours ago.

huangapple
  • 本文由 发表于 2023年6月15日 03:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定