首页 > 解决方案 > 更改在代码中运行 ExpressJS 服务的 Google Cloud Function 的执行超时?

问题描述

如何更改在代码中运行 ExpressJS 服务的 Google Cloud Function 的执行超时?

我找到了 Google Functions 的文档来更改一个简单函数的默认超时 60 秒。

https://cloud.google.com/functions/docs/concepts/exec

exports.afterTimeout = (req, res) => {
  setTimeout(() => {
  // May not execute if function's timeout is <2 minutes
    console.log('Function running...');
    res.end();
  }, 120000); // 2 minute delay
};

表达

const express = require('express');
const app = express();

...
module.exports.app = app;

谢谢

标签: expresstimeoutgoogle-cloud-functions

解决方案


独立于您在 Cloud Function 中运行的内容,当您使用gcloud命令部署它时,您只需将--timeout标志设置为您想要的值(以秒为单位),最多 9 分钟。

如果您使用控制台创建云函数,“创建”按钮正上方有一个下拉菜单,将显示高级选项,您可以在其中选择所需的超时时间(1 到 540 秒之间)。

如果您想在执行时执行此操作,则可以从函数内部执行API调用来更改超时。但是,它不会影响任何已经运行的函数执行。


推荐阅读