gpt4 book ai didi

node.js - Google Cloud Tasks 在 5 分钟后重新排队任务,忽略更大的 dispatchDeadline 设置

转载 作者:行者123 更新时间:2023-12-05 06:54:41 35 4
gpt4 key购买 nike

基础设施:将 Google Cloud Task 项目排队的 Firebase 函数和执行一些长时间运行的作业的本地 NodeJS Express 应用程序。出于调试目的,我使用 ngrok 将任务隧道传输到我的本地应用程序

GC Tasks 文档声明默认超时为 10 分钟,可以将其调整为 15 秒到 30 分钟之间的任何值。但是对于略长于 5 分钟的作业,我收到了另一个请求,尽管之前的请求尚未完成。

在我的 nodejs 应用程序中,我使用 @google-cloud/tasks 包来创建任务。我尝试了默认设置(没有 dispatchDeadline)和自定义设置(见下面的代码)

export const createQueueClient = (ref: string) => {
const client = new google.CloudTasksClient();

return {
client,
parent: client.queuePath(project, region, ref),
};
};


export const postMessageToQueue = async <T>(
queue: QueueNamesType,
url: string,
message: T,
context: functions.https.CallableContext
) => {
const { client, parent } = createQueueClient(queue);

const [response] = await client.createTask({
task: {
dispatchDeadline: {
seconds: 15 * 60, // <------------------- attempt to set a timeout value
},
httpRequest: {
httpMethod: 'POST',
url: url,
body: Buffer.from(
JSON.stringify({
data: message,
})
).toString('base64'),
headers: {
// forward auth header to next service
Authorization: context.rawRequest.header('Authorization') || '',
'Content-Type': 'application/json',
},
},
},
parent: parent,
});

return response;
};

ngrok:

enter image description here

nodejs app logs,可以清楚的看到请求之间有5分钟的差异:

[[11:46:31.084]] [LOG]    Received request
[[11:51:31.317]] [LOG] Received request
[[11:51:34.006]] [LOG] Finished request iCg7raEbrw6LlbBhjpBS

我在面板中启用了登录,我看到了 300 秒的尝试持续时间和“不可用”状态

log viewer

由于重新尝试收集更多“证据”,屏幕截图/日志上的时间可能会有所不同

所有运行时间少于 5 分钟的作业都成功完成并且不会重新排队。我错过了什么吗?

最佳答案

dispatchDeadline 属性对我有用。通过以下配置将超时增加到 30 分钟

const {CloudTasksClient} = require('@google-cloud/tasks');
class MyService {
createMyTask(...): Promise<any> {
return new Promise((resolve, reject) => {
const client = new CloudTasksClient();
const payload = {...}
const parent = client.queuePath(GCP_PROJECT_ID, GCP_SERVICE_LOCATION, GCP_TASK_QUEUE_NAME);
const url = TASK_URL;
const task = {
httpRequest: {
httpMethod: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': authToken
},
url,
body: Buffer.from(JSON.stringify(payload)).toString('base64'),
},
dispatchDeadline: { seconds: 1800 },
};
const request = {parent, task};
client.createTask(request).then((result: Array<any>) => {
resolve(`task created: ${result[0].name}`);
}).catch((err: Error) => {
reject(`Create task Error: ${err}`);
});
});
}

除了 dispatchDeadline,您还需要为 Google Cloud Task 设置重试参数。

enter image description here

关于node.js - Google Cloud Tasks 在 5 分钟后重新排队任务,忽略更大的 dispatchDeadline 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65476395/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com