我一直在尝试探索是否有办法重试创建任务函数 https://github.com/googleapis/nodejs-tasks/blob/master/samples/createTask.js。原因是因为我时不时地遇到超出截止日期的错误,如下所示:
错误:4 DEADLINE_EXCEEDED:Object.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:1204) 处的 Object.exports.createStatusError (/srv/node_modules/grpc/src/common.js:91:15) 超出截止日期:28) 在 InterceptingListener._callNext (/srv/node_modules/grpc/src/client_interceptors.js:568:42) 在 InterceptingListener.onReceiveStatus (/srv/node_modules/grpc/src/client_interceptors.js:618:8) 在回调 ( /srv/node_modules/grpc/src/client_interceptors.js:845:24)
阅读 createTask 函数背后的代码,我发现默认超时配置 https://github.com/googleapis/nodejs-tasks/blob/master/src/v2beta3/cloud_tasks_client_config.json#L88只有10秒。
目前,我尝试通过这样做将超时延长到 30 秒(我认为这是最大值):
try {
console.log("Sending task %j", task);
const callOptions = {
timeout: 30000
};
// Send create task request.
const [response] = await client.createTask(request, callOptions);
const name = response.name;
console.log(`Created task ${name}`);
} catch (error) {
console.error("CREATE_TASK_ERROR::", error);
}
看起来它确实有效。不过,我还想介绍一下 API 在 30 秒内无法响应的情况。
我试过这段代码:
try {
console.log("Sending task %j", task);
const callOptions = {
timeout: 2000, // I've set it to 2 seconds to be able to reproduce the deadline exceeded error easily
retry: {
initial_retry_delay_millis: 100,
retry_delay_multiplier: 1.3,
max_retry_delay_millis: 60000,
initial_rpc_timeout_millis: 20000,
rpc_timeout_multiplier: 1.0,
max_rpc_timeout_millis: 20000,
total_timeout_millis: 300000
}
};
// Send create task request.
const [response] = await client.createTask(request, callOptions);
const name = response.name;
console.log(`Created task ${name}`);
} catch (error) {
console.error("CREATE_TASK_ERROR::", error);
}
但我没有看到 createTask 被重试。但根据评论here https://github.com/googleapis/nodejs-tasks/blob/master/src/v2beta3/cloud_tasks_client.js#L1432,我们应该能够覆盖默认设置,包括重试。
我究竟做错了什么?请帮忙。