DEADLINE_EXCEEDED 正在向 Google Cloud Tasks 添加任务

DEADLINE_EXCEEDED while adding tasks to Google Cloud Tasks

从 Node.js 客户端向 Google Cloud Tasks 添加任务时,我偶尔会收到一个名为 DEADLINE_EXCEEDED 的错误。我尝试在 for 循环中添加的只有 17 个任务。不确定为什么偶尔会发生这种情况,但有时工作得很好!

这里是详细的错误日志:

Error: 4 DEADLINE_EXCEEDED: Deadline exceeded
    at Object.callErrorFromStatus (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
    at Object.onReceiveStatus (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/client.js:175:52)
    at Object.onReceiveStatus (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:341:141)
    at Object.onReceiveStatus (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:304:181)
    at Http2CallStream.outputStatus (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/call-stream.js:115:74)
    at Http2CallStream.maybeOutputStatus (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/call-stream.js:154:22)
    at Http2CallStream.endCall (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/call-stream.js:140:18)
    at Http2CallStream.cancelWithStatus (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/call-stream.js:443:14)
    at Timeout.<anonymous> (/Users/gijo/Desktop/workspace/flying-press-server/node_modules/@grpc/grpc-js/build/src/deadline-filter.js:59:28)
    at listOnTimeout (internal/timers.js:549:17) {
  code: 4,
  details: 'Deadline exceeded',
  metadata: Metadata { internalRepr: Map {}, options: {} }
}

我以为我达到了一些配额。但是一查,none个已经超标了。

这是我创建队列的方式(更新插入):

const client = require('./client');

module.exports = async (queue_name, concurrent) => {
  return client.updateQueue({
    queue: {
      name: client.queuePath(
        process.env.GCP_PROJECT,
        process.env.GCP_QUEUE_LOCATION,
        queue_name,
      ),
      rateLimits: {
        maxConcurrentDispatches: concurrent,
        maxDispatchesPerSecond: concurrent,
        maxBurstSize: 100,
      },
      retryConfig: {
        maxAttempts: 3,
        unlimitedAttempts: false,
        maxRetryDuration: {
          seconds: 3600,
        },
        minBackoff: {
          seconds: 60,
        },
        maxBackoff: {
          seconds: 300,
        },
        maxDoublings: 3,
      },
    },
  });
};

这是我添加任务的方式:

const client = require('./client');

module.exports = async (queue_name, url, config) => {
  return client.createTask({
    parent: client.queuePath(
      process.env.GCP_PROJECT,
      process.env.GCP_QUEUE_LOCATION,
      queue_name,
    ),
    task: {
      httpRequest: {
        httpMethod: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        url: `${process.env.OPTIMIZER_URL}/optimize-page`,
        body: Buffer.from(JSON.stringify({ url, config })).toString(
          'base64',
        ),
      },
    },
  });
};

我之前在 Cloud Functions 和 App Engine 中创建任务时遇到过这个问题。发生这种情况是因为我没有正确 await 创建任务承诺。这会导致请求挂起并最终超时,但这只是偶尔发生。基本上,请求在异步调用完成之前结束,这会导致 "serverless" 运行时出现问题,例如 App Engine、Cloud Functions、Cloud 运行 等

要解决此问题,请确保在调用 createTask 时执行 await 或在从请求返回之前对所有任务承诺执行 await,例如Promise.all().