google 云任务没有发送 body 到 http 云功能

google cloud task not sending body to http cloud function

我有一个 gcloud 任务,其中的代码主要是从云任务文档中复制的。 https://cloud.google.com/tasks/docs/creating-http-target-tasks

云函数的目标是能够将 dateTo 和 dateFrom 日期推送给它,以便它可以循环周期并从中创建 cloudTasks。我还没有创建循环,因为我首先要解决这个问题。

问题是它不推送 body http 云功能。 http 云功能在使用 CURL 时有效。

curl -X POST "posturl" -H "Content-Type:application/json" --data '{"date": "2019-12-01", "lastRun": false}'

我检查了这里提到的方法,它是POST,所以应该没问题。

检查接口没有负载。使用 gcloud beta describe tasks ...没有 body 或没有关于有效负载的内容。

httpRequest:
  headers:
    User-Agent: Google-Cloud-Tasks
  httpMethod: POST
  url: correcthttpurl
name: name
scheduleTime: '2020-01-07T15:45:24.774042Z'
view: view
scheduleTime: '2020-01-07T15:45:24.774042Z'
view: BASIC

这是创建任务的云函数的代码。 任务已添加到 queue,但单击 运行 时它们似乎不会触发该功能。 (可能是因为该功能需要 body 才能工作)

/**
 * Background Cloud Function to be triggered by Pub/Sub.
 * This function is exported by index.js, and executed when
 * the trigger topic receives a message.
 *
 * @param {object} pubSubEvent The event payload.
 * @param {object} context The event metadata.
 */

// gcloud functions deploy queueAffiliateApiTasks --trigger-topic queue-affiliate-api-tasks --region europe-west1 --runtime=nodejs8

const moment = require("moment");


exports.queueAffiliateApiTasks = async (pubSubEvent, context) => {
  const data =
    pubSubEvent.data || Buffer.from(pubSubEvent.data, "base64").toString();

  const attributes = pubSubEvent.attributes;

  // take 30 days ago untill yesterday
  let dateFrom = moment().subtract(1, "days");
  let dateTo = moment().subtract(1, "days");

  // if dates provided in pubsub use those
  if (attributes && "dateFrom" in attributes && "dateTo" in attributes) {
    console.log("with attributes");
    dateFrom = attributes.dateFrom;
    dateTo = attributes.dateTo;
  } else {
    console.log("no attributes");
  }

  console.log(dateFrom);
  console.log(dateTo);

  // use dates for looping
  dateFrom = moment(dateFrom);
  dateTo = moment(dateTo);

  console.log(dateFrom);
  console.log(dateTo);

  const date = dateTo.format("YYYY-MM-DD").toString();
  const lastRun = false;
  const url =
    "the correct url to the http cloud function";
  const payload = JSON.stringify({ date: date, lastRun: false }, null, 2);

  await createHttpTask(url, payload);
};

async function createHttpTask(url, payload) {
  const project = "xxx";
  const queue = "affiliate-api-queue";
  const location = "europe-west1";
  const inSeconds = 0 // Delay in task execution
  // [START cloud_tasks_create_http_task]
  // Imports the Google Cloud Tasks library.
  const {CloudTasksClient} = require('@google-cloud/tasks');

  // Instantiates a client.
  const client = new CloudTasksClient();

  // Construct the fully qualified queue name.
  const parent = client.queuePath(project, location, queue);

  const task = {
    httpRequest: {
      httpMethod: 'POST',
      url,
    },
  };

  task.httpRequest.body = Buffer.from(payload).toString('base64');

  if (inSeconds) {
    // The time when the task is scheduled to be attempted.
    task.scheduleTime = {
      seconds: inSeconds + Date.now() / 1000,
    };
  }

  // Send create task request.
  console.log('Sending task:');
  console.log(task);
  const request = {parent, task};
  const [response] = await client.createTask(request);
  console.log(`Created task ${response.name}`);
  console.log(`Response: ${JSON.stringify(response.httpRequest, null, 2)}`);
  // [END cloud_tasks_create_http_task]
}

我错过了什么?

如您所见,任务已添加但未执行 payload 和 headers 为空

根据 Google docs,默认 responseView 是 BASIC;默认情况下,并非所有信息都会被检索,因为某些数据(例如有效负载)可能 return 仅在需要时才需要 return 因为它的大小很大或因为它包含的数据的敏感性。

我发现了问题,应用引擎由于某种原因未启用。请求现在有效!