首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌云任务不发送正文到http云功能

谷歌云任务不发送正文到http云功能
EN

Stack Overflow用户
提问于 2020-01-08 08:30:51
回答 2查看 1.7K关注 0票数 1

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

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

问题是它不推动身体,即http云功能。当使用CURL时,http云函数可以工作。

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描述任务..。没有任何关于有效载荷的东西。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
/**
 * 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]
}

我遗漏了什么?

如您所见,任务是添加的,但没有执行。

有效载荷和报头为空

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-09 12:20:39

我发现了这个问题,由于某种原因,应用程序引擎没有启用。请求现在生效了!

票数 1
EN

Stack Overflow用户

发布于 2020-01-08 13:54:33

根据Google 文档,默认情况下,responseView是基本的;默认情况下并不是所有的信息都是被检索的,因为某些数据(例如有效载荷)可能只希望在需要时才返回,这是因为它的大小很大,或者因为它包含的数据的敏感性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59642035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档