Reputation: 53
Scenario
I have a couple of Google Cloud Functions triggered by a Google Cloud Storage object.finalize event. For that I'm using two buckets and transfer job with "Synchronization options: Overwrite objects at destination" which copies every day a single file from one source bucket to destination one. The source bucket is the same for both functions and the destination buckets are different.
Problem
Most of the time it works as expected but sometimes I see multiple events at the almost the same time. Most of the time I see 2 duplicates but once was 3. I put in log event payload but it always the same.
More details
Here is an example of multiple log entries
Question
Could it be a known issue for Google Cloud Storage?
If no then most probably something is wrong in my code.
I'm using the following project structure:
/functions
|--/foo-code
|--executor.js
|--foo.sql
|--/bar-code
|--executor.js
|--bar.sql
|--/shared-code
|--utils.js
|--index.js
|--package.json
index.js
let foo;
let bar;
exports.foo = (event, callback) => {
console.log(`event ${JSON.stringify(event)}`);
foo = foo || require(`./foo-code/executor`);
foo.execute(event, callback);
};
exports.bar = (event, callback) => {
console.log(`event ${JSON.stringify(event)}`);
bar = bar || require(`./bar-code/executor`);
bar.execute(event, callback);
};
./foo-code/executor.js
const utils = require('../shared-code/utils.js)
exports.execute = (event, callback) => {
// run Big Query foo.sql statement
};
./bar-code/executor.js
const utils = require('../shared-code/utils.js)
exports.execute = (event, callback) => {
// run Big Query bar.sql statement
};
And finally deployment:
foo background function with specific bucket trigger:
gcloud beta functions deploy foo \
--source=https://<path_to_repo>/functions \
--trigger-bucket=foo-destination-bucket \
--timeout=540 \
--memory=128MB
bar background function with specific bucket trigger:
gcloud beta functions deploy bar \
--source=https://<path_to_repo>/functions \
--trigger-bucket=bar-destination-bucket \
--timeout=540 \
--memory=128MB
For me looks that the most possible problem is due to the fact of multiple deployments (only trigger-bucket flag is different). But the weird thing is that above setup works most of the time.
Upvotes: 3
Views: 7004
Reputation: 1452
The normal behavior of Cloud Function is that at least once the events are delivered and background functions are invoked, which means that rarely, spurious duplicates may occur.
To make sure that your function behaves correctly on retried execution attempts, you should make it idempotent by implementing it so that an event results in the desired results (and side effects) even if it is delivered multiple times.
Check the documentation for some guidelines for making a background function idempotent.
Upvotes: 1