node.jsfirebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-tasks

How to cancel a task enqueued on Firebase Functions?


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


Solution

  • In latest releases a feature was added to delete the tasks. You can pass an id to the TaksOptions

    const queue = getFunctions().taskQueue("yourQueue");
    // Enqueue and pass id to TaksOptions
    await queue.enqueue({ 
    ...data
    }, 
    {
      id: idYouWillBeUsingToDelete,
      ...taskOptions
    });
    

    Delete the task

    const queue = getFunctions().taskQueue("yourQueue");
    // Use de delete method
    await queue.delete(idYouWillBeUsingToDelete)