-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial commit * Add region tags * Fix bugs * Remove unnecessary async's * s/attrs/data/ * Add comments * Add remaining Node8 samples * Fix region tags * Updated sample functions_helloworld_pubsub_node8 * Updated sample functions_helloworld_pubsub_node8 * Fix docstrings * Fix comments + add license * Use data parameter name * Clarify README * Clarify comments
- Loading branch information
Ace Nassri
authored
Jul 20, 2018
1 parent
022055e
commit 04340fc
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright 2018, Google LLC. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// [START functions_tips_infinite_retries_node8] | ||
/** | ||
* Background Cloud Function that only executes within a certain time | ||
* period after the triggering event to avoid infinite retry loops. | ||
* | ||
* @param {object} event The Cloud Functions event payload. | ||
* @param {function} context Information about the event. | ||
*/ | ||
exports.avoidInfiniteRetries = (data, context) => { | ||
const eventAge = Date.now() - Date.parse(context.timestamp); | ||
const eventMaxAge = 10000; | ||
|
||
// Ignore events that are too old | ||
if (eventAge > eventMaxAge) { | ||
console.log(`Dropping event ${context.eventId} with age ${eventAge} ms.`); | ||
return; | ||
} | ||
|
||
// Do what the function is supposed to do | ||
console.log(`Processing event ${context.eventId} with age ${eventAge} ms.`); | ||
}; | ||
// [END functions_tips_infinite_retries_node8] | ||
|
||
// [START functions_tips_retry_node8] | ||
/** | ||
* Background Cloud Function that demonstrates | ||
* how to toggle retries using a promise | ||
* | ||
* @param {object} data The Cloud Functions event payload. | ||
* @param {function} context The Cloud Functions metadata. | ||
*/ | ||
exports.retryPromise = (data, context) => { | ||
const tryAgain = !!data.retry; | ||
|
||
if (tryAgain) { | ||
throw new Error(`Retrying...`); | ||
} else { | ||
return new Error('Not retrying...'); | ||
} | ||
}; | ||
// [END functions_tips_retry_node8] | ||
|
||
// [START functions_helloworld_background_node8] | ||
/** | ||
* Background Cloud Function. | ||
* | ||
* @param {object} data Data passed to the Cloud Function. | ||
*/ | ||
exports.helloBackground = (data) => { | ||
return `Hello ${data.name || 'World'}!`; | ||
}; | ||
// [END functions_helloworld_background_node8] | ||
|
||
// [START functions_helloworld_pubsub_node8] | ||
/** | ||
* Background Cloud Function to be triggered by Pub/Sub. | ||
* This function is exported by index.js, and executed when | ||
* the trigger topic receives a message. | ||
* | ||
* @param {object} data The Cloud Functions event payload. | ||
* @param {object} context The Cloud Functions metadata. | ||
*/ | ||
exports.helloPubSub = (data) => { | ||
const name = data.data ? Buffer.from(data.data, 'base64').toString() : 'World'; | ||
|
||
console.log(`Hello, ${name}!`); | ||
}; | ||
// [END functions_helloworld_pubsub_node8] | ||
|
||
// [START functions_helloworld_storage_node8] | ||
/** | ||
* Background Cloud Function to be triggered by Cloud Storage. | ||
* | ||
* @param {object} data The Cloud Functions event payload. | ||
*/ | ||
exports.helloGCS = (data) => { | ||
if (data.resourceState === 'not_exists') { | ||
console.log(`File ${data.name} deleted.`); | ||
} else if (data.metageneration === '1') { | ||
// metageneration attribute is updated on metadata changes. | ||
// on create value is 1 | ||
console.log(`File ${data.name} uploaded.`); | ||
} else { | ||
console.log(`File ${data.name} metadata updated.`); | ||
} | ||
}; | ||
// [END functions_helloworld_storage_node8] | ||
|
||
// [START functions_helloworld_storage_generic_node8] | ||
/** | ||
* Generic background Cloud Function to be triggered by Cloud Storage. | ||
* | ||
* @param {object} data The Cloud Functions event payload. | ||
* @param {object} context The Cloud Functions event metadata. | ||
*/ | ||
exports.helloGCSGeneric = (data, context) => { | ||
console.log(` Event ${context.eventId}`); | ||
console.log(` Event Type: ${context.eventType}`); | ||
console.log(` Bucket: ${data.bucket}`); | ||
console.log(` File: ${data.name}`); | ||
console.log(` Metageneration: ${data.metageneration}`); | ||
console.log(` Created: ${data.timeCreated}`); | ||
console.log(` Updated: ${data.updated}`); | ||
}; | ||
// [END functions_helloworld_storage_generic_node8] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "nodejs-docs-samples-gcf-node8", | ||
"version": "0.0.1", | ||
"description": "Google Cloud Functions samples for Node.js 8", | ||
"scripts": { | ||
}, | ||
"engines": { "node": "^8"}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"author": "Google LLC", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues" | ||
} | ||
} |