简体   繁体   English

Firebase函数按顺序调度任务

[英]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.根据谷歌最近发表的这篇文章,我试图按队列的顺序(先进先出)调度 firebase 任务。 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)?甚至可以按顺序(先进先出)处理 firebase 任务吗?

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);
});

在此处输入图像描述

I haven't used Task Functions myself yet, but as far as I know there is no way to guarantee the execution order.我自己还没有使用过Task Functions,但据我所知没有办法保证执行顺序。 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.我能想到的唯一选择是将maxInstances限制为 1,正如我所期望的那样,在这种情况下,任务可能会按顺序从队列中取出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM