'Firebase functions dispatch tasks in order
Based of this article recently published by Google I am trying to dispatch firebase tasks in the order(first in first out) of the queue. But no matter what setting I try it does not process tasks in order of the queue. Please see code attached and the result image. Is it even possible to process firebase task in an order(fifo)?
export const backupApod = functions.tasks
.taskQueue({
retryConfig: {
maxAttempts: 5,
minBackoffSeconds: 60,
},
rateLimits: {
maxConcurrentDispatches: 1,
},
})
.onDispatch(async (data) => {
try {
await FireStore.getDatabase()
.collection("queueTest")
.doc("test")
.set(
{
test: firestore.FieldValue.arrayUnion(data.id),
},
{ merge: true }
);
await LoggerService.info(JSON.stringify(data));
} catch (error) {
await LoggerService.error(JSON.stringify(error) as any);
}
});
export const enqueueBackupTasks = functions.https.onRequest(async (_request, response) => {
const queue = getFunctions(app).taskQueue("backupApod");
const enqueues = [];
for (let i = 0; i <= 100; i += 1) {
enqueues.push(
queue.enqueue(
{ id: `task-${i}` },
{
dispatchDeadlineSeconds: 60 * 5, // 5 minutes
}
)
);
}
await Promise.all(enqueues);
response.sendStatus(200);
});
// Event tried adding tasks synchronously, then pausing queue. When resume the queue the order is also not correct
export const enqueueBackupTasks = functions.https.onRequest(async (_request, response) => {
const queue = getFunctions(app).taskQueue("backupApod");
for (let i = 0; i <= 100; i += 1) {
await queue.enqueue(
{ id: `task-${i}` },
{
dispatchDeadlineSeconds: 60 * 5, // 5 minutes
}
);
}
response.sendStatus(200);
});
Solution 1:[1]
I haven't used Task Functions myself yet, but as far as I know there is no way to guarantee the execution order. This is the same for all trigger types, because order of execution is not built into the infrastructure.
The only option I can think of is to limit your maxInstances
to 1, as I expect in that case the tasks may get picked off the queue in order.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frank van Puffelen |