简体   繁体   English

有没有办法确定云 function 是否已完成运行 - 通过循环 getDownloadURL 或消息传递?

[英]Is there a way to determine once a cloud function has finished running - either through looping getDownloadURL or messaging?

I am still getting the hang of Firebase and Cloud functions, but here is what I'm trying to figure out.我仍然掌握 Firebase 和 Cloud 函数的窍门,但这就是我想要弄清楚的。

The current setup当前设置

My app has a cloud function that will take a PDF that has been uploaded into a storage bucket and convert it into PNG.我的应用程序有一个云 function,它将采用已上传到存储桶中的 PDF 并将其转换为 PNG。 It doesn't destroy the original PDF, so I am left with both files.它不会破坏原来的 PDF,所以我留下了两个文件。

The URL for the newly created PNG is then attached to a property on one of our documents in Firestore.新创建的 PNG 的 URL 然后附加到我们在 Firestore 中的一个文档的属性。

What I am trying to accomplish我想要完成的事情

I want to be able to upload a new PDF to use as a replacement image.我希望能够上传新的 PDF 用作替换图片。 I think I am running into a race condition where the cloud function hasn't finished executing by the time I am trying to call updateDoc() with the new PNG.我想我遇到了一个竞争条件,当我尝试用新的 PNG 调用updateDoc()时,云 function 还没有完成执行。

On the client side, I have the storageRef returned from the upload method:在客户端,我有从上传方法返回的storageRef

uploadFunction(...).then((snapshot) => {
     return snapshot.ref
}

I'm saving the result of this function to a variable, and I am trying to pass that into the update method that will adjust the property on my document in Firestore:我将这个 function 的结果保存到一个变量中,我试图将其传递到更新方法中,该方法将调整我在 Firestore 中文档的属性:

const storageRef = await functionThatUploadsPDF(file);

updateDocumentInFirestore(storageRef);

Within updateDocumentInFirestore , I'm trying to navigate to the new reference that should exist once the cloud function has finished, get a download URL, and update that property on my document:updateDocumentInFirestore ,我试图导航到云 function 完成后应该存在的新引用,下载 URL,并更新我文档中的该属性:

const newImageRef = ref(storageRef.parent, "generatedImage.png");

const newDownloadURL = getDownloadURL(newImageRef).then((url) => {
     updateDoc(documentRef, backgroundImage: url);
});

However, I am getting the following error - I believe due to the cloud function having not finished yet:但是,我收到以下错误 - 我相信是由于云 function 尚未完成:

Firebase Storage: Object 'storage-bucket/generatedImage.png' does not exist. Firebase 存储:Object 'storage-bucket/generatedImage.png' 不存在。 (storage/object-not-found) (存储/未找到对象)

My thoughts on potential solutions我对潜在解决方案的看法

  1. I could try to poll the storage for the existence of generatedImage.png until the getDownloadURL call returns an actual URL, but I worry about the amount of calls this would yield.我可以尝试轮询存储以了解generatedImage.png是否存在,直到getDownloadURL调用返回实际的 URL,但我担心这会产生调用量。

  2. If there is a way for the cloud function to send a message to let me know that the conversion is finished, I can send a call once for the download URL after receiving said message.如果有办法让云端function发消息告诉我转换完成,我收到消息后可以发一次电话下载URL。 However, I can't figure out how to accomplish this.但是,我无法弄清楚如何实现这一点。

Efforts so far迄今为止的努力

I have been pursuing course 1. So far, but have not met any success yet.我一直在追求课程 1。到目前为止,但还没有取得任何成功。 Scouring through Firebase documentation, I haven't been able to find any supporting resources on how to accomplish 1 or 2. Does anyone have any suggestions - either on my planned courses of action, or a new option that I haven't considered?通过 Firebase 文档搜索,我无法找到任何关于如何完成 1 或 2 的支持资源。有没有人有任何建议 - 无论是关于我计划的行动方案,还是我没有考虑过的新选项?

You can use this onFinalize trigger to send a message or update a document in Firestore to indicate that the function has finished running.您可以使用此onFinalize触发器发送消息或更新 Firestore 中的文档以指示 function 已完成运行。 This trigger is triggered whenever a file is created or updated.每当创建或更新文件时都会触发此触发器。

onFinalize Sent when a new object (or a new generation of an existing object) is successfully created in the bucket. onFinalize 在桶中成功创建新的 object(或新一代现有对象)时发送。 This includes copying or rewriting an existing object. A failed upload does not trigger this event.这包括复制或重写现有的 object。上传失败不会触发此事件。

you can also create a promise that resolves when the downloadURL is not null, and use that promise in your updateDocumentInFirestore function. This way, the updateDoc function will only be called once the downloadURL is available.您还可以创建一个 promise,当 downloadURL 不是 null 时解析,并在您的 updateDocumentInFirestore function 中使用该 promise。这样,updateDoc function 只有在 downloadURL 可用时才会被调用。

Additionally, as was mentioned in the comments, you can consider cloud workflow .The exact implementation will depend on your specific use case此外,正如评论中提到的,您可以考虑云工作流。具体实施将取决于您的具体用例

You can also check these similar cases您还可以查看这些类似案例

Firebase Storage: Object does not exist Firebase 存储:Object 不存在

Error: storage/object-not-found when trying to upload large image file 错误:尝试上传大图像文件时未找到存储/对象

Firebase Storage Put could not get object Firebase Storage Put 无法获取 object

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

相关问题 通过云运行时 Firestore 中的问题 function - Issue in the firestore while running through cloud function 有没有办法停止正在运行的 firebase 云实例 function -- 它已进入无限循环 - Is there a way to stop the running instance of firebase cloud function -- It has gone into an infinite loop 云函数:在第一个函数完成时调用第二个函数 - Cloud functions: call a second function when the first has finished 等待云Function完成 - Wait for Cloud Function to be finished Firebase 云消息 - getMessaging 不是 function - Firebase Cloud Messaging - getMessaging is not a function Firebase 通知 admin.messaging() 不是 function - Firebase 云消息传递 - Firebase notification admin.messaging() is not a function - Firebase cloud messaging Firebase:Error.getDownloadURL 不是 function - Firebase: Error .getDownloadURL is not a function 如何通过firebase云消息发送定时通知到flutter app - how to send scheduled notification to flutter app through firebase cloud messaging 有没有办法使用 firebase 云消息从 firebase-messaging-sw 执行 console.log - Is there a way to do a console.log from the firebase-messaging-sw with firebase cloud messaging Firebase 云 Function 完成状态:“响应错误” - Firebase Cloud Function finished with status: 'response error'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM