Search code examples
google-cloud-functionsgoogle-cloud-pubsubgoogle-cloud-scheduler

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?


Solution

  • 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