Home > OS >  How to get the scheduled time of Google Cloud Task by task name?
How to get the scheduled time of Google Cloud Task by task name?

Time:12-18

Is there any way I can get the time when the Cloud Task is scheduled to run using its task name?

I'm creating a task in a function using:

const task = {
  httpRequest: {
    httpMethod: 'POST',
    url,
    body: Buffer.from(JSON.stringify(payload)).toString('base64'),
    headers: {
      'Content-Type': 'application/json',
    },
  },
  scheduleTime: {
    seconds: 86400 
  }
}

const [response] = await tasksClient.createTask({ parent: queuePath, task })
return response.name; 

It gives me a task name that looks like:

projects/.../queues/foo/tasks/987555410648792221

How can I get the time when this task will run at (epoch time) using the above task name?

CodePudding user response:

The response is of type ITask that has a scheduleTime property. Try:

console.log(`Task will trigger at ${response.scheduleTime?.seconds}`)

However, this is not necessary as you are supposed to pass the task trigger time as follows:

const task = {
  scheduleTime: {
    seconds: Date.now()   86400 // Time at which task should trigger
  }
}

Just passing 86400 will run the tasks immediately.


If you know the task name, then you can use getTask() to fetch task information and read schedule time:

const [task] = await tasks.getTask({
  name: response.name, // <-- task name
});

console.log(`Task schedule time: ${task.scheduleTime?.seconds}`);
  • Related