Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent cloud scheduler from triggering a function more than once?

I'm triggering a cloud function every minute with cloud scheduler [* * * * *].

The Stackdriver logs indicate the function appears to have been triggered and run twice in the same minute. Is this possible?

PubSub promises at least once delivery but I assumed that GCP would automatically handle duplicate triggers for scheduler -> function workflows.

What is a good pattern for preventing this function from running more than once per minute?

like image 482
ProGirlXOXO Avatar asked Oct 18 '25 15:10

ProGirlXOXO


1 Answers

Your function needs to be made "idempotent" in order to ensure that a message gets processed only once. In other words, you'll have to maintain state somewhere (maybe a database) that a message was processed successfully, and check that state to make sure a message doesn't get processed twice.

All non-HTTP type Cloud Functions provide a unique event ID in the context parameter provided to the function invocation. If you see a repeat event ID, that means your function is being invoked again for the same message, for whatever reason.

This need for idempotence is not unique to pubsub or cloud scheduler. It's a concern for all non-HTTP type background functions.

A full discussion on writing idempotent functions is a bit too much a Stack Overflow answer, but there is a post in the Google Cloud blog that covers the issue pretty well.

See also: Cloud functions and Firebase Firestore with Idempotency

like image 145
Doug Stevenson Avatar answered Oct 22 '25 00:10

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!