-
Notifications
You must be signed in to change notification settings - Fork 103
/
index.js
77 lines (67 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const cumulusMessageAdapter = require('@cumulus/cumulus-message-adapter-js');
const { log } = require('@cumulus/common');
const {
deleteS3Object,
s3ObjectExists,
s3PutObject
} = require('@cumulus/common/aws');
/**
* Throw an error if hello world is configured to throw an error for
* testing/example purposes. Set the pass on retry value to simulate
* a task passing on a retry.
*
* @param {Object} event - input from the message adapter
* @returns {undefined} none
*/
async function throwErrorIfConfigured(event) {
const execution = event.config.execution;
const retryFilename = `${execution}_retry.txt`;
const bucket = event.config.bucket;
let isRetry = false;
if (event.config.passOnRetry) {
isRetry = await s3ObjectExists({
Bucket: bucket,
Key: retryFilename
});
}
if (event.config.passOnRetry && isRetry) {
log.debug('Detected retry');
// Delete file
await deleteS3Object(bucket, retryFilename);
} else if (event.config.fail) {
if (event.config.passOnRetry) {
await s3PutObject({
Bucket: bucket,
Key: retryFilename,
Body: ''
});
}
throw new Error('Step configured to force fail');
}
}
/**
* Return sample 'hello world' JSON
*
* @param {Object} event - input from the message adapter
* @returns {Object} sample JSON object
*/
async function helloWorld(event) {
await throwErrorIfConfigured(event);
return {
hello: 'Hello World'
};
}
/**
* Lambda handler
*
* @param {Object} event - a Cumulus Message
* @param {Object} context - an AWS Lambda context
* @param {Function} callback - an AWS Lambda handler
* @returns {undefined} - does not return a value
*/
function handler(event, context, callback) {
cumulusMessageAdapter.runCumulusTask(helloWorld, event, context, callback);
}
exports.handler = handler;
exports.helloWorld = helloWorld; // exported to support testing