【问题标题】:google cloud task not sending body to http cloud function谷歌云任务未将正文发送到 http 云功能
【发布时间】:2020-04-25 18:19:46
【问题描述】:

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

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

问题是它没有推送http云函数的body。 http cloud 功能在使用 CURL 时有效。

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

我检查了这里提到的方法,它是 POST,所以应该没问题。 https://stackoverflow.com/a/56448479/2785289

检查接口没有负载。使用 gcloud beta 描述任务......关于有效负载没有正文或没有任何内容。

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

这是创建任务的云函数的代码。 任务已添加到队列中,但单击运行时似乎不会触发该功能。 (可能是因为该功能需要一个身体才能工作)

/**
 * 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]
}

我错过了什么?

如您所见,任务已添加但未执行 有效载荷和标头为空

【问题讨论】:

标签: javascript node.js google-cloud-platform google-cloud-functions google-cloud-tasks


【解决方案1】:

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

【讨论】:

  • 我在后面的代码中将其更改为“FULL”,并且数据在响应中。不过,这不是我的问题的解决方案。似乎任务没有执行。
  • @Christoph,是否在 Cloud Task 中创建任务?
  • 是的,它在列表中。但是如果您单击任务名称,则没有有效负载。查看我的云功能日志,它似乎没有被触发。
  • 当点击执行按钮时它会运行,但我在云功能日志中看不到任何内容。
  • 1.您的 Cloud Function HTTP 触发了吗? 2. Cloud Task中的任务被删除了吗?
【解决方案2】:

我发现了问题,应用引擎由于某种原因没有启用。请求正在处理中!

【讨论】:

  • 很好,你找到了根本原因。
猜你喜欢
  • 1970-01-01
  • 2021-01-25
  • 2017-11-14
  • 2021-09-10
  • 2021-02-05
  • 2019-03-28
  • 2021-10-31
  • 1970-01-01
  • 2018-09-05
相关资源
最近更新 更多