1
votes

I have a gcloud task where the code I largely copied from the cloud tasks documentation. https://cloud.google.com/tasks/docs/creating-http-target-tasks

The goal of the cloud function is to be able to push a dateTo and dateFrom date to it so it can loop the period and create cloudTasks from it. I did not create the loop yet because I first want this issue solved.

The problem is it does not push the body the the http cloud function. The http cloud function works when using CURL.

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

I checked the method like mentioned here and it is POST so it should be fine. https://stackoverflow.com/a/56448479/2785289

Checking the interface there is no payload. Using gcloud beta describe tasks ... there is no body or nothing about payload.

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

This is the code for the cloud function creating the tasks. The tasks are added to the queue but when clicking run they don't seem to trigger the function. (probably because the function requires a body to work)

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

What am I missing?

enter image description here

as you can see the task is added but not executed enter image description here payload and headers are empty

2
Have you tried for testing purposes developers.google.com/oauthplayground? You can use the scope and request URL as indicated here: developers.google.com/tasks/v1/reference/tasks/insertziganotschka
@ziganotschka inserting works, it's the execution that does not seems to workChristoph
I am running strackdriver logging and there are no logs there either.Christoph

2 Answers

1
votes

As per the Google docs, By default responseView is BASIC; not all information is retrieved by default because some data, such as payloads, might be desirable to return only when needed because of its large size or because of the sensitivity of data that it contains.

1
votes

I found the issue, the app engine was not enabled for some reason. Requests are working now!