简体   繁体   English

如何在 firebase 中排队函数

[英]How to enqueue functions in firebase

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...我正在使用 firebase 函数,并试图从 RTDB onWrite 触发器中排队 function,到目前为止,我要么出错,要么没有任何反应,我也无法在网上找到任何有用的东西,甚至 firebase 文档也没有太大帮助。 ..

This is my Realtime DB trigger function which is supposed to enqueue the firebase function.这是我的实时数据库触发器 function,它应该使 firebase function 入队。

Attempt #1尝试 #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"尝试 #1 结果:错误:“ReferenceError:未定义队列”

Attempt #2:尝试#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"尝试 #2 结果:错误:“TypeError:functions.taskQueue 不是函数”

The function which I am trying to enqueue:我正在尝试排队的 function:

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:)任何帮助是极大的赞赏:)

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.就实际调用特定的 function 而言,直接在您的函数代码中执行此操作。 That's how you would call it.你会这样称呼它。

I don't believe the Enqueue functions with Cloud Tasks is utilized in your use case.我不相信在您的用例中使用了 Cloud Tasks 的 Enqueue 函数

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部署任务队列 function

Deploy task queue function using the Firebase CLI:使用 Firebase CLI 部署任务队列 function:

 $ 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.首次部署任务队列 function 时,CLI 在 Cloud Tasks 中创建一个任务队列,并在您的源代码中指定选项(速率限制和重试)。

So before trying to deploy and test onUserBasketUpdated , deploy only the expireUserBasket function:因此,在尝试部署和测试onUserBasketUpdated ,仅部署expireUserBasket function:

$ firebase deploy --only functions:expireUserBasket

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

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