@cumulus/message 10.0.1 → 10.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/StepFunctions.js CHANGED
@@ -1,7 +1,11 @@
1
1
  'use strict';
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -22,20 +26,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
27
  };
24
28
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.parseStepMessage = exports.pullStepFunctionEvent = void 0;
26
- /**
27
- * Utility functions for working with AWS Step Function events/messages
28
- * @module StepFunctions
29
- *
30
- * @example
31
- * const StepFunctions = require('@cumulus/message/StepFunctions');
32
- */
29
+ exports.getCumulusMessageFromExecutionEvent = exports.getFailedExecutionMessage = exports.lastFailedEventStep = exports.getFailedStepName = exports.parseStepMessage = exports.pullStepFunctionEvent = void 0;
33
30
  const jsonpath_plus_1 = require("jsonpath-plus");
31
+ const get_1 = __importDefault(require("lodash/get"));
32
+ const set_1 = __importDefault(require("lodash/set"));
33
+ const StepFunctions_1 = require("@cumulus/aws-client/StepFunctions");
34
+ const execution_history_1 = require("@cumulus/common/execution-history");
34
35
  const s3Utils = __importStar(require("@cumulus/aws-client/S3"));
35
36
  const logger_1 = __importDefault(require("@cumulus/logger"));
37
+ const Executions_1 = require("./Executions");
36
38
  const log = new logger_1.default({
37
39
  sender: '@cumulus/message/StepFunctions',
38
40
  });
41
+ const executionStatusToWorkflowStatus = (executionStatus) => {
42
+ const statusMap = {
43
+ ABORTED: 'failed',
44
+ FAILED: 'failed',
45
+ RUNNING: 'running',
46
+ SUCCEEDED: 'completed',
47
+ TIMED_OUT: 'failed',
48
+ };
49
+ return statusMap[executionStatus];
50
+ };
39
51
  /**
40
52
  * Given a Step Function event, replace specified key in event with contents
41
53
  * of S3 remote message
@@ -100,4 +112,115 @@ const parseStepMessage = async (stepMessage, stepName) => {
100
112
  return parsedMessage;
101
113
  };
102
114
  exports.parseStepMessage = parseStepMessage;
115
+ /**
116
+ * Searches the Execution step History for the TaskStateEntered pertaining to
117
+ * the failed task Id. HistoryEvent ids are numbered sequentially, starting at
118
+ * one.
119
+ *
120
+ * @param {HistoryEvent[]} events - Step Function events array
121
+ * @param {HistoryEvent} failedStepEvent - Step Function's failed event.
122
+ * @returns {string} name of the current stepfunction task or 'UnknownFailedStepName'.
123
+ */
124
+ const getFailedStepName = (events, failedStepEvent) => {
125
+ var _a, _b;
126
+ try {
127
+ const previousEvents = events.slice(0, failedStepEvent.id - 1);
128
+ const startEvents = previousEvents.filter((e) => e.type === 'TaskStateEntered');
129
+ const stateName = (_b = (_a = startEvents.pop()) === null || _a === void 0 ? void 0 : _a.stateEnteredEventDetails) === null || _b === void 0 ? void 0 : _b.name;
130
+ if (!stateName)
131
+ throw new Error('TaskStateEntered Event Object did not have `stateEnteredEventDetails.name`');
132
+ return stateName;
133
+ }
134
+ catch (error) {
135
+ log.info('Failed to determine a failed stepName from execution events.');
136
+ log.error(error);
137
+ }
138
+ return 'UnknownFailedStepName';
139
+ };
140
+ exports.getFailedStepName = getFailedStepName;
141
+ /**
142
+ * Finds all failed execution events and returns the last one in the list.
143
+ *
144
+ * @param {Array<HistoryEventList>} events - array of AWS Stepfunction execution HistoryEvents
145
+ * @returns {HistoryEvent|[]} - the last lambda or activity that failed in the
146
+ * event array, or an empty array.
147
+ */
148
+ const lastFailedEventStep = (events) => {
149
+ const failures = events.filter((event) => ['LambdaFunctionFailed', 'ActivityFailed'].includes(event.type));
150
+ return failures.pop();
151
+ };
152
+ exports.lastFailedEventStep = lastFailedEventStep;
153
+ /**
154
+ * Get message to use for publishing failed execution notifications.
155
+ *
156
+ * Try to get the input to the last failed step in the execution so we can
157
+ * update the status of any granules/PDRs that don't exist in the initial execution
158
+ * input.
159
+ *
160
+ * Falls back to overall execution input.
161
+ *
162
+ * @param {Object} inputCumulusMessage - Workflow execution input message
163
+ * @param {Function} getExecutionHistoryFunction - Testing override for mock/etc of
164
+ * StepFunctions.getExecutionHistory
165
+ * @returns {Object} - CumulusMessage Execution step message or execution input message
166
+ */
167
+ const getFailedExecutionMessage = async (inputCumulusMessage, getExecutionHistoryFunction = StepFunctions_1.getExecutionHistory) => {
168
+ const amendedCumulusMessage = { ...inputCumulusMessage };
169
+ try {
170
+ const executionArn = (0, Executions_1.getMessageExecutionArn)(amendedCumulusMessage);
171
+ if (!executionArn) {
172
+ throw new Error('No execution arn found');
173
+ }
174
+ const { events } = await getExecutionHistoryFunction({ executionArn });
175
+ const lastFailedEvent = (0, exports.lastFailedEventStep)(events);
176
+ if (!lastFailedEvent) {
177
+ log.warn(`No failed step events found in execution ${executionArn}`);
178
+ return amendedCumulusMessage;
179
+ }
180
+ const failedExecutionStepName = (0, exports.getFailedStepName)(events, lastFailedEvent);
181
+ const failedStepExitedEvent = (0, execution_history_1.getStepExitedEvent)(events, lastFailedEvent);
182
+ if (!failedStepExitedEvent) {
183
+ log.info(`Could not retrieve output from last failed step in execution ${executionArn}, falling back to execution input`);
184
+ log.info(`Could not find TaskStateExited event after step ID ${lastFailedEvent.id} for execution ${executionArn}`);
185
+ return {
186
+ ...amendedCumulusMessage,
187
+ exception: {
188
+ ...lastFailedEvent.activityFailedEventDetails,
189
+ ...lastFailedEvent.lambdaFunctionFailedEventDetails,
190
+ failedExecutionStepName,
191
+ },
192
+ };
193
+ }
194
+ const taskExitedEventOutput = JSON.parse((0, execution_history_1.getTaskExitedEventOutput)(failedStepExitedEvent) || '{}');
195
+ taskExitedEventOutput.exception = {
196
+ ...taskExitedEventOutput.exception,
197
+ failedExecutionStepName,
198
+ };
199
+ return await (0, exports.parseStepMessage)(taskExitedEventOutput, failedExecutionStepName);
200
+ }
201
+ catch (error) {
202
+ log.error('getFailedExecution failed to retrieve failure:', error);
203
+ return amendedCumulusMessage;
204
+ }
205
+ };
206
+ exports.getFailedExecutionMessage = getFailedExecutionMessage;
207
+ const getCumulusMessageFromExecutionEvent = async (executionEvent) => {
208
+ let cumulusMessage;
209
+ if (executionEvent.detail.status === 'RUNNING') {
210
+ cumulusMessage = JSON.parse(executionEvent.detail.input);
211
+ }
212
+ else if (executionEvent.detail.status === 'SUCCEEDED') {
213
+ cumulusMessage = JSON.parse(executionEvent.detail.output);
214
+ }
215
+ else {
216
+ const inputMessage = JSON.parse((0, get_1.default)(executionEvent, 'detail.input', '{}'));
217
+ cumulusMessage = await (0, exports.getFailedExecutionMessage)(inputMessage);
218
+ }
219
+ const fullCumulusMessage = (await (0, exports.pullStepFunctionEvent)(cumulusMessage));
220
+ const workflowStatus = executionStatusToWorkflowStatus(executionEvent.detail.status);
221
+ (0, set_1.default)(fullCumulusMessage, 'meta.status', workflowStatus);
222
+ (0, set_1.default)(fullCumulusMessage, 'cumulus_meta.workflow_stop_time', executionEvent.detail.stopDate);
223
+ return fullCumulusMessage;
224
+ };
225
+ exports.getCumulusMessageFromExecutionEvent = getCumulusMessageFromExecutionEvent;
103
226
  //# sourceMappingURL=StepFunctions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"StepFunctions.js","sourceRoot":"","sources":["src/StepFunctions.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AAEb;;;;;;GAMG;AAEH,iDAAyC;AACzC,gEAAkD;AAClD,6DAAqC;AAGrC,MAAM,GAAG,GAAG,IAAI,gBAAM,CAAC;IACrB,MAAM,EAAE,gCAAgC;CACzC,CAAC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CACpD,cAAc,CAAC,MAAM,CAAC,MAAyB,CAChD,CAAC;IACF,IAAA,aAAG,EAAC,kBAAkB,EAAE,aAAa,EAAE,cAAc,CAAC,CAAC;IAEvD,IAAA,aAAG,EACD,kBAAkB,EAClB,iCAAiC,EACjC,cAAc,CAAC,MAAM,CAAC,QAAQ,CAC/B,CAAC;IAEF,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC;AA3BW,QAAA,mCAAmC,uCA2B9C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulus/message",
3
- "version": "10.0.1",
3
+ "version": "10.0.2",
4
4
  "description": "Utilities for building and parsing Cumulus messages",
5
5
  "keywords": [
6
6
  "GIBS",
@@ -38,11 +38,11 @@
38
38
  "author": "Cumulus Authors",
39
39
  "license": "Apache-2.0",
40
40
  "dependencies": {
41
- "@cumulus/aws-client": "10.0.1",
42
- "@cumulus/common": "10.0.1",
43
- "@cumulus/errors": "10.0.1",
44
- "@cumulus/logger": "10.0.1",
45
- "@cumulus/types": "10.0.1",
41
+ "@cumulus/aws-client": "10.0.2",
42
+ "@cumulus/common": "10.0.2",
43
+ "@cumulus/errors": "10.0.2",
44
+ "@cumulus/logger": "10.0.2",
45
+ "@cumulus/types": "10.0.2",
46
46
  "jsonpath-plus": "^3.0.0",
47
47
  "lodash": "^4.17.20",
48
48
  "uuid": "^8.2.0"
@@ -50,5 +50,5 @@
50
50
  "devDependencies": {
51
51
  "@types/uuid": "^8.0.0"
52
52
  },
53
- "gitHead": "49c3c88336838184f22f35fbce298c71cd269138"
53
+ "gitHead": "e678a624b5179759200b988de72292ce389f41e2"
54
54
  }
package/src/Build.ts CHANGED
@@ -10,9 +10,9 @@
10
10
  */
11
11
 
12
12
  import merge from 'lodash/merge';
13
+
13
14
  import { Message } from '@cumulus/types';
14
15
  import { v4 as uuidv4 } from 'uuid';
15
-
16
16
  import {
17
17
  WorkflowMessageTemplate,
18
18
  WorkflowMessageTemplateCumulusMeta,
@@ -0,0 +1,54 @@
1
+ import { SQSRecord, EventBridgeEvent } from 'aws-lambda';
2
+
3
+ import { parseSQSMessageBody } from '@cumulus/aws-client/SQS';
4
+ import { CumulusMessage } from '@cumulus/types/message';
5
+ import Logger from '@cumulus/logger';
6
+
7
+ import { getCumulusMessageFromExecutionEvent } from './StepFunctions';
8
+
9
+ const log = new Logger({ sender: '@cumulus/DeadLetterMessage' });
10
+
11
+ type StepFunctionEventBridgeEvent = EventBridgeEvent<'Step Functions Execution Status Change', { [key: string]: string }>;
12
+ type UnwrapDeadLetterCumulusMessageReturnType = (
13
+ StepFunctionEventBridgeEvent
14
+ | AWS.SQS.Message
15
+ | SQSRecord
16
+ | CumulusMessage
17
+ );
18
+
19
+ /**
20
+ * Unwrap dead letter Cumulus message, which may be wrapped in a
21
+ * States cloudwatch event, which is wrapped in an SQS message.
22
+ *
23
+ * @param {Object} messageBody - received SQS message
24
+ * @returns {Object} the cumulus message or nearest available object
25
+ */
26
+ export const unwrapDeadLetterCumulusMessage = async (
27
+ messageBody: UnwrapDeadLetterCumulusMessageReturnType
28
+ ): Promise<UnwrapDeadLetterCumulusMessageReturnType> => {
29
+ try {
30
+ if ('cumulus_meta' in messageBody) {
31
+ return messageBody;
32
+ }
33
+ if ('Body' in messageBody || 'body' in messageBody) {
34
+ // AWS.SQS.Message/SQS.Record case
35
+ const unwrappedMessageBody = parseSQSMessageBody(
36
+ messageBody
37
+ ) as CumulusMessage;
38
+ return await unwrapDeadLetterCumulusMessage(unwrappedMessageBody);
39
+ }
40
+ if (!('detail' in messageBody)) {
41
+ // Non-typed catchall
42
+ return messageBody;
43
+ }
44
+ // StepFunctionEventBridgeEvent case
45
+ const unwrappedMessageBody = await getCumulusMessageFromExecutionEvent(messageBody);
46
+ return await unwrapDeadLetterCumulusMessage(unwrappedMessageBody);
47
+ } catch (error) {
48
+ log.error(
49
+ 'Falling back to storing wrapped message after encountering unwrap error',
50
+ error
51
+ );
52
+ return messageBody;
53
+ }
54
+ };
@@ -8,14 +8,41 @@
8
8
  * const StepFunctions = require('@cumulus/message/StepFunctions');
9
9
  */
10
10
 
11
+ import { EventBridgeEvent } from 'aws-lambda';
11
12
  import { JSONPath } from 'jsonpath-plus';
13
+ import get from 'lodash/get';
14
+ import set from 'lodash/set';
15
+
16
+ import { getExecutionHistory } from '@cumulus/aws-client/StepFunctions';
17
+ import { getStepExitedEvent, getTaskExitedEventOutput } from '@cumulus/common/execution-history';
18
+ import { Message } from '@cumulus/types';
12
19
  import * as s3Utils from '@cumulus/aws-client/S3';
13
20
  import Logger from '@cumulus/logger';
14
- import { Message } from '@cumulus/types';
21
+
22
+ import { getMessageExecutionArn } from './Executions';
15
23
 
16
24
  const log = new Logger({
17
25
  sender: '@cumulus/message/StepFunctions',
18
26
  });
27
+ type ExecutionStatus = ('ABORTED' | 'RUNNING' | 'TIMED_OUT' | 'SUCCEEDED' | 'FAILED');
28
+
29
+ type ExecutionStatusToWorkflowStatusMap = {
30
+ [K in ExecutionStatus]: Message.WorkflowStatus;
31
+ };
32
+
33
+ const executionStatusToWorkflowStatus = (
34
+ executionStatus: ExecutionStatus
35
+ ): Message.WorkflowStatus => {
36
+ const statusMap: ExecutionStatusToWorkflowStatusMap = {
37
+ ABORTED: 'failed',
38
+ FAILED: 'failed',
39
+ RUNNING: 'running',
40
+ SUCCEEDED: 'completed',
41
+ TIMED_OUT: 'failed',
42
+ };
43
+
44
+ return statusMap[executionStatus];
45
+ };
19
46
 
20
47
  /**
21
48
  * Given a Step Function event, replace specified key in event with contents
@@ -91,3 +118,133 @@ export const parseStepMessage = async (
91
118
  }
92
119
  return <Message.CumulusMessage>parsedMessage;
93
120
  };
121
+
122
+ /**
123
+ * Searches the Execution step History for the TaskStateEntered pertaining to
124
+ * the failed task Id. HistoryEvent ids are numbered sequentially, starting at
125
+ * one.
126
+ *
127
+ * @param {HistoryEvent[]} events - Step Function events array
128
+ * @param {HistoryEvent} failedStepEvent - Step Function's failed event.
129
+ * @returns {string} name of the current stepfunction task or 'UnknownFailedStepName'.
130
+ */
131
+ export const getFailedStepName = (
132
+ events: AWS.StepFunctions.HistoryEvent[],
133
+ failedStepEvent: { id: number }
134
+ ) => {
135
+ try {
136
+ const previousEvents = events.slice(0, failedStepEvent.id - 1);
137
+ const startEvents = previousEvents.filter(
138
+ (e) => e.type === 'TaskStateEntered'
139
+ );
140
+ const stateName = startEvents.pop()?.stateEnteredEventDetails?.name;
141
+ if (!stateName) throw new Error('TaskStateEntered Event Object did not have `stateEnteredEventDetails.name`');
142
+ return stateName;
143
+ } catch (error) {
144
+ log.info('Failed to determine a failed stepName from execution events.');
145
+ log.error(error);
146
+ }
147
+ return 'UnknownFailedStepName';
148
+ };
149
+
150
+ /**
151
+ * Finds all failed execution events and returns the last one in the list.
152
+ *
153
+ * @param {Array<HistoryEventList>} events - array of AWS Stepfunction execution HistoryEvents
154
+ * @returns {HistoryEvent|[]} - the last lambda or activity that failed in the
155
+ * event array, or an empty array.
156
+ */
157
+ export const lastFailedEventStep = (
158
+ events: AWS.StepFunctions.HistoryEvent[]
159
+ ): AWS.StepFunctions.HistoryEvent | undefined => {
160
+ const failures = events.filter((event) =>
161
+ ['LambdaFunctionFailed', 'ActivityFailed'].includes(event.type));
162
+ return failures.pop();
163
+ };
164
+
165
+ /**
166
+ * Get message to use for publishing failed execution notifications.
167
+ *
168
+ * Try to get the input to the last failed step in the execution so we can
169
+ * update the status of any granules/PDRs that don't exist in the initial execution
170
+ * input.
171
+ *
172
+ * Falls back to overall execution input.
173
+ *
174
+ * @param {Object} inputCumulusMessage - Workflow execution input message
175
+ * @param {Function} getExecutionHistoryFunction - Testing override for mock/etc of
176
+ * StepFunctions.getExecutionHistory
177
+ * @returns {Object} - CumulusMessage Execution step message or execution input message
178
+ */
179
+ export const getFailedExecutionMessage = async (
180
+ inputCumulusMessage: Message.CumulusMessage,
181
+ getExecutionHistoryFunction: typeof getExecutionHistory = getExecutionHistory
182
+ ) => {
183
+ const amendedCumulusMessage = { ...inputCumulusMessage };
184
+
185
+ try {
186
+ const executionArn = getMessageExecutionArn(amendedCumulusMessage);
187
+ if (!executionArn) { throw new Error('No execution arn found'); }
188
+ const { events } = await getExecutionHistoryFunction({ executionArn });
189
+
190
+ const lastFailedEvent = lastFailedEventStep(events);
191
+ if (!lastFailedEvent) {
192
+ log.warn(`No failed step events found in execution ${executionArn}`);
193
+ return amendedCumulusMessage;
194
+ }
195
+ const failedExecutionStepName = getFailedStepName(events, lastFailedEvent);
196
+ const failedStepExitedEvent = getStepExitedEvent(events, lastFailedEvent);
197
+
198
+ if (!failedStepExitedEvent) {
199
+ log.info(`Could not retrieve output from last failed step in execution ${executionArn}, falling back to execution input`);
200
+ log.info(`Could not find TaskStateExited event after step ID ${lastFailedEvent.id} for execution ${executionArn}`);
201
+ return {
202
+ ...amendedCumulusMessage,
203
+ exception: {
204
+ ...lastFailedEvent.activityFailedEventDetails,
205
+ ...lastFailedEvent.lambdaFunctionFailedEventDetails,
206
+ failedExecutionStepName,
207
+
208
+ },
209
+ };
210
+ }
211
+ const taskExitedEventOutput = JSON.parse(getTaskExitedEventOutput(failedStepExitedEvent) || '{}');
212
+ taskExitedEventOutput.exception = {
213
+ ...taskExitedEventOutput.exception,
214
+ failedExecutionStepName,
215
+ };
216
+ return await parseStepMessage(taskExitedEventOutput, failedExecutionStepName);
217
+ } catch (error) {
218
+ log.error('getFailedExecution failed to retrieve failure:', error);
219
+ return amendedCumulusMessage;
220
+ }
221
+ };
222
+
223
+ export const getCumulusMessageFromExecutionEvent = async (executionEvent: EventBridgeEvent<'Step Functions Execution Status Change', { [key: string]: string }>) => {
224
+ let cumulusMessage;
225
+ if (executionEvent.detail.status === 'RUNNING') {
226
+ cumulusMessage = JSON.parse(executionEvent.detail.input);
227
+ } else if (executionEvent.detail.status === 'SUCCEEDED') {
228
+ cumulusMessage = JSON.parse(executionEvent.detail.output);
229
+ } else {
230
+ const inputMessage = JSON.parse(get(executionEvent, 'detail.input', '{}'));
231
+ cumulusMessage = await getFailedExecutionMessage(inputMessage);
232
+ }
233
+
234
+ const fullCumulusMessage = (await pullStepFunctionEvent(
235
+ cumulusMessage
236
+ )) as Message.CumulusMessage;
237
+
238
+ const workflowStatus = executionStatusToWorkflowStatus(
239
+ executionEvent.detail.status as ExecutionStatus
240
+ );
241
+ set(fullCumulusMessage, 'meta.status', workflowStatus);
242
+
243
+ set(
244
+ fullCumulusMessage,
245
+ 'cumulus_meta.workflow_stop_time',
246
+ executionEvent.detail.stopDate
247
+ );
248
+
249
+ return fullCumulusMessage;
250
+ };