我有一个第一代功能,只要在云存储中创建文件,它就会运行。目前我已将 timeoutSeconds 设置为允许的最大值,即 540 秒。但是当处理的数据很多,运行时间超过540s时,函数有时会超时
我试图将整个函数转换为第二代函数,以便我可以访问第二代中允许的 60 分钟超时,但在 https://firebase.google.com/docs/functions/beta/manage-functions 中的文档中 提到了。
timeoutSeconds 的最大值为 540,即 9 分钟。
但是在页面中https://firebase.google.com/docs/functions/beta它写的
如果需要长时间运行的工作负载,HTTP 函数现在可以有 1 小时的超时(以前是 9 分钟)。
这让我很困惑,因为我的函数需要在文件上传到云存储时被触发,但云存储触发器不允许超过 540 秒的超时。那么我可以调用第 2 代 https 函数,它允许第 2 代或第 1 代云存储触发函数超时 1 小时吗?即使 https 函数运行时间比存储触发函数长,我是否可以在 https 函数的响应中发回数据?
关于如何处理 timeoutSeconds 的任何其他建议都非常感谢,因为我对云功能和后端一般来说是新手。
编辑: 我试过像这样创建一个第二代函数,这个函数被下面的函数(executeAllTask)调用,这是一个第一代函数。
exports.executetask = onRequest(
{ timeoutSeconds: 3600, memory: "1GiB", cors: true },
(req, res) => {
if (res.method === "POST") {
const fileBucket = req.body.fileBucket
const taskId = req.body.taskId
const filePath = req.body.filePath
const contentType = req.body.contentType
const fileName = req.body.fileName
processTask(fileBucket, taskId, filePath, contentType, fileName)
.then(res.status(200).end())
.catch((err) => {
logger.log(err);
res.status(405).end();
})
}
else {
res.status(405);
res.end();
}
});
exports.executeAllTask = functions.runWith({ timeoutSeconds: 540,memory:"1GB" }).storage.object().onFinalize(async (object) => {
try {
const fileBucket = object.bucket;
const filePath = object.name;
const fileName = path.basename(filePath);
const pathParts = path.dirname(filePath).split('/')
const taskId = pathParts[pathParts.length - 1];
const contentType = object.contentType;
if (fileName == 'response') {
return admin.firestore().collection("tasks").doc(taskId).set(
{
taskRunning: false,
dateCompleted: admin.firestore.Timestamp.now(),
},
{ merge: true }
)
}
if (!contentType.startsWith('application/')) {
return functions.logger.log('This is not an blob.');
}
const body = {
fileBucket, filePath, fileName, taskId, contentType
}
await fetch('https://executetask-<random-hash>-<region>.a.run.app', {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' }
});
} catch (error) {
functions.logger.log(error);
}})
但是每当我尝试部署 executetask 函数时,我都会收到错误消息
HTTP 错误:400,无法创建 Cloud Run 服务 执行任务。 spec.template.spec.containers.resources.limits.cpu:为 cpu 指定的值无效。对于指定的值,maxScale 不得超过 10 个。 考虑在容量更大的区域运行您的工作负载, 如果您发现持续使用率接近此限制,请减少您请求的每个实例的 cpu,或请求增加该区域的配额,请参阅https://cloud.google.com/run/quotas。您的项目可以通过向您的帐户添加账单信息来进一步扩展。