sudcha
sudcha

Reputation: 783

How to run multiple Firestore Functions Sequentially?

We have 20 functions that must run everyday. Each of these functions do something different based on inputs from the previous function.

We tried calling all the functions in one function, but it hits the timeout error as these 20 functions take more than 9 minutes to execute.

How can we trigger these multiple functions sequentially, or avoid timeout error for one function that executes each of these functions?

Upvotes: 1

Views: 584

Answers (3)

ultraGentle
ultraGentle

Reputation: 6319

If you simply want the functions to execute in order, and you don't need to pass the result of one directly to the next, you could wrap them in a scheduled function (docs) that spaces them out with enough time for each to run.

Sketch below with 3 minute spacing:

exports.myScheduler = functions.pubsub
.schedule('every 3 minutes from 22:00 to 23:00') 
.onRun(context => {
  let time = // check the time
  if (time === '22:00') func1of20();
  else if (time === '22:03') func2of20();
  // etc. through func20of20()
}

If you do need to pass the results of each function to the next, func1 could store its result in a DB entry, then func2 starts by reading that result, and ends by overwriting with its own so func3 can read when fired 3 minutes later, etc. — though perhaps in this case, the other solutions are more tailored to your needs.

Upvotes: 0

guillaume blaquiere
guillaume blaquiere

Reputation: 75775

There is a variant to the Doug solution. At the end of the function, instead of publishing a message into pubsub, simply write a specific log (for example " end").

Then, go to stackdriver logging, search for this specific log trace (turn on advanced filters) and configure a sink into a PubSub topic of this log entry. Thereby, every time that the log is detected, a PubSub message is published with the log content.

Finally, plug your next function on this PubSub topic.

If you need to pass values from function to another one, you can simply add these values in the log trace at the end of the function and parse it at the beginning of the next one.

Chaining functions is not an easy things to do. Things are coming, maybe Google Cloud Next will announce new products for helping you in this task.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317487

There is no configuration or easy way to get this done. You will have to set up a fair amount of code and infrastructure to get this done.

The most straightforward solution involves chaining together calls using pubsub type functions. You can send a message to a pubsub topic that will trigger the next function to run. The payload of the message to send can be the parameters that the function should use to determine how it should operate. If the payload is too big, or some more complex sources of data are required to make that decision, you can use a database to store intermediate data that the next function can query and use.

Since we don't have any more specific details about how your functions actually work, nothing more specific can be said. If you run into problems with a specific detail of this scheme, please post again describing that specifically you're trying to do and what's not working the way you expect.

Upvotes: 1

Related Questions