4

I'm talking about this: https://firebase.google.com/docs/functions/task-functions

I want to enqueue tasks with the scheduleTime parameter to run in the future, but I must be able to cancel those tasks.

I expected it would be possible to do something like this pseudo code:

const task = await queue.enqueue({ foo: true })

// Then...
await queue.cancel(task.id)

I'm using Node.js. In case it's not possible to cancel a scheduled task with firebase-admin, can I somehow work around it by using @google-cloud/tasks directly?

PS: I've also created a feature request: https://github.com/firebase/firebase-admin-node/issues/1753

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
gustavopch
  • 796
  • 9
  • 16
  • 1
    Tasks can be deleted using the `@google-cloud/tasks` SDK but you'll need the task name/ID. `enqueue()` seems to return `void` and not the ID. Found the relevant code on [Github](https://github.com/firebase/firebase-admin-node/blob/dd50aaa75f682d7d8e384daeece9ea2163e7319b/src/functions/functions-api-client-internal.ts#L101) where the function just returns nothing. – Dharmaraj Jun 08 '22 at 18:39
  • @Dharmaraj this seems like an answer for the OP. Can you please convert your comment to an answer? – ErnestoC Jun 09 '22 at 16:28
  • 1
    The comment would become a very useful answer if it provides a workaround to somehow get the task ID and cancel the task. – gustavopch Jun 09 '22 at 16:44
  • Can you add more use case details for a workaround? Which tasks are the ones you would want to delete, and how would you want to delete them? The Node.js Cloud Tasks library is also able to [list the tasks](https://github.com/googleapis/nodejs-tasks/blob/main/samples/listQueues.js), which could be useful here depending on how you’d like to have it implemented. As far as using your exact code, it would be as Dharmaraj explained. – ErnestoC Jun 10 '22 at 18:38
  • You can instead use Cloud Tasks directly as in my answer below (until Firebase adds this functionality). – Dharmaraj Jun 12 '22 at 18:09
  • 1
    @ErnestoC listing the tasks might work as long as there's some identifier that OP can use to delete the specific tasks but might be an issue if there are plenty of tasks. Got a bit delayed to respond : – Dharmaraj Jun 12 '22 at 18:12

1 Answers1

3

The Firebase SDK doesn't return the task name/ID right now as in the code. If you need this functionality, I'd recommend filing a feature request and meanwhile use Cloud Tasks directly.

You can simply create a HTTP Callable Function and then use the Cloud Tasks SDK to create a HTTP Target tasks that call this Cloud Function instead of using the onDispatch.

// Instead of onDispatch()
export const handleQueueEvent = functions.https.onRequest((req, res) => {
  // ...
});

Adding a Cloud Task:

async function createHttpTask() {
  const parent = client.queuePath(project, location, queue);

  const task = {
    httpRequest: {
      httpMethod: 'POST', // change method if required
      url, // Use URL of handleQueueEvent function here
    },
  };

  const request = {
    parent: parent,
    task: task
  };

  const [response] = await client.createTask(request);
  return;
}

Checkout the documentation for more information.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84