[英]Cloud Function scheduler with memory allocation management
我正在嘗試使用第三方 API 在 Firebase 雲函數上創建計划的 function。 由於通過第三方 API 收集並傳遞給此計划的 function 的數據量很大,因此返回Function invocation was interrupted. Error: memory limit exceeded.
Function invocation was interrupted. Error: memory limit exceeded.
我已經在幫助下編寫了這個index.js
(如下),但仍在尋找它應該如何處理調度程序 function 內的大尺寸 output 數據的方式。
index.js
const firebaseAdmin = require("firebase-admin");
const firebaseFunctions = require("firebase-functions");
firebaseAdmin.initializeApp();
const fireStore = firebaseAdmin.firestore();
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const serviceToken = "SERVICE-TOKEN";
const serviceBaseUrl = "https://api.service.com/";
const app = express();
app.use(cors());
const getAllExamples = async () => {
var url = `${serviceBaseUrl}/examples?token=${serviceToken}`;
var config = {
method: "get",
url: url,
headers: {}
};
return axios(config).then((res) => {
console.log("Data saved!");
return res.data;
}).catch((err) => {
console.log("Data not saved: ", err);
return err;
});
}
const setExample = async (documentId, dataObject) => {
return fireStore.collection("examples").doc(documentId).set(dataObject).then(() => {
console.log("Document written!");
}).catch((err) => {
console.log("Document not written: ", err);
});
}
module.exports.updateExamplesRoutinely = firebaseFunctions.pubsub.schedule("0 0 * * *").timeZone("America/Los_Angeles").onRun(async (context) => {
const examples = await getAllExamples(); // This returns an object of large size, containing 10,000+ arrays
const promises = [];
for(var i = 0; i < examples.length; i++) {
var example = examples[i];
var exampleId = example["id"];
if(exampleId && example) promises.push(setExample(exampleId, example));
}
return Promise.all(promises);
});
Firebase 的官方文檔簡單地講述了如何手動設置超時和 memory 分配,如下所示,我正在尋找如何將其與上述調度程序 function 結合的方式。
exports.convertLargeFile = functions
.runWith({
// Ensure the function has enough memory and time
// to process large files
timeoutSeconds: 300,
memory: "1GB",
})
.storage.object()
.onFinalize((object) => {
// Do some complicated things that take a lot of memory and time
});
Firebase 的官方文檔簡單地講述了如何手動設置超時和 memory 分配,如下所示,我正在尋找我們應該如何將其與上述調度程序 function 結合的方式。
您應該執行以下操作:
module.exports.updateExamplesRoutinely = firebaseFunctions
.runWith({
timeoutSeconds: 540,
memory: "8GB",
})
.pubsub
.schedule("0 0 * * *")
.timeZone("America/Los_Angeles")
.onRun(async (context) => {...)
但是,如果您在 CF 中處理大量“示例”,您可能仍然會遇到相同的錯誤。 正如您在對另一個答案的評論中提到的,建議將其切成塊。
怎么做? 這在很大程度上取決於您的具體情況(例如:您是否在每次運行時處理 10,000 多個示例?或者它只會發生一次,以便“消化”積壓?)。
您可以只處理計划的 function 中的數千個文檔,並計划它每 xx 秒運行一次。 或者,您可以使用 Cloud Function 的PubSub 觸發版本在 CF 的多個實例之間分配工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.