I am using firebase functions and am trying to enqueue a function from within a RTDB onWrite trigger, so far I either get errors or nothing happens, I also cannot find anything useful online, and even the firebase docs aren't much of a help...
This is my Realtime DB trigger function which is supposed to enqueue the firebase function.
Attempt #1
exports.onUserBasketUpdated = functions
.region("my-region")
.database.ref("/path/{uid}")
.onWrite(async (change, context) => {
const queue = getFunctions(admin.app, "my-region").taskQueue(
"expireUserBasket"
);
await queue.enqueue(
{
id: 1,
},
{
dispatchDeadlineSeconds: 60 * 1, // 1 minute
}
);
return { data: "ok" };
});
Attempt #1 Results: Error: "ReferenceError: queue is not defined"
Attempt #2:
exports.onUserBasketUpdated = functions
.region("my-region")
.database.ref("/path/{uid}")
.onWrite(async (change, context) => {
await functions
.taskQueue(`locations/my-region/functions/expireUserBasket`)
.enqueue({ message: "hello" });
return { data: "ok" };
});
Attempt #2 Result: Error: "TypeError: functions.taskQueue is not a function"
The function which I am trying to enqueue:
exports.expireUserBasket = functions
.region("my-region")
.runWith({ timeoutSeconds: 120 })
.tasks.taskQueue({
retryConfig: {
maxAttempts: 5,
minBackoffSeconds: 60,
},
rateLimits: {
maxConcurrentDispatches: 6,
},
})
.onDispatch(async (data) => {
console.info("data from the scheduled function", data);
});
Any help is greatly appreciated :)
CodePudding user response:
The documentation is pretty good on this feature. Example code is provided.
In terms of actually calling a particular function, do that directly in your function's code. That's how you would call it.
I don't believe the Enqueue functions with Cloud Tasks is utilized in your use case.
CodePudding user response:
As you are getting a queue not defined error, you've probably missed this step in the documentation (emphasis mine):
Deploying the task queue function
Deploy task queue function using the Firebase CLI:
$ firebase deploy --only functions:backupApod
When deploying a task queue function for the first time, the CLI creates a task queue in Cloud Tasks with options (rate limiting and retry) specified in your source code.
So before trying to deploy and test onUserBasketUpdated
, deploy only the expireUserBasket
function:
$ firebase deploy --only functions:expireUserBasket