0

DEADLINE_EXCEEDED从 Node.js 客户端向 Google Cloud Tasks 添加任务时,我偶尔会遇到一个错误。我只尝试在 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: {} }
}

我以为我达到了一些配额。但在检查时,它们都没有超过。

在此处输入图像描述

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

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',
        ),
      },
    },
  });
};
4

1 回答 1

6

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

要解决此问题,请确保在从请求返回之前对所有任务承诺进行await调用createTask或以其他方式执行,例如使用.awaitPromise.all()

于 2020-05-15T16:15:30.437 回答