-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathsfnStep.js
286 lines (260 loc) · 10.5 KB
/
sfnStep.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* eslint-disable max-classes-per-file */
'use strict';
const StepFunctions = require('@cumulus/aws-client/StepFunctions');
const isNil = require('lodash/isNil');
const log = require('@cumulus/common/log');
const { parseStepMessage, pullStepFunctionEvent } = require('@cumulus/message/StepFunctions');
/**
* `SfnStep` provides methods for getting the output of a step within an AWS
* Step Function for a specific execution.
*/
class SfnStep {
/**
* `getStartEvent` gets the "start" event for a step, given its schedule event
*
* @param {Object} executionHistory - AWS Step Function execution history
* @param {Object} scheduleEvent - AWS Step Function schedule-type event
* @returns {Object} - AWS Step Function start-type event
*/
getStartEvent(executionHistory, scheduleEvent) {
return executionHistory.events.find((event) => {
const isStartEvent = this.startEvents.includes(event.type);
const previousEventIsScheduleEvent = event.previousEventId === scheduleEvent.id;
return isStartEvent && previousEventIsScheduleEvent;
});
}
/**
* `getCompletionEvent` gets the "completion" event for a step, given its start event
*
* @param {Object} executionHistory - AWS Step Function execution history
* @param {Object} startEvent - AWS Step Function start-type event
* @returns {Object} - AWS Step Function completion-type event
*/
getCompletionEvent(executionHistory, startEvent) {
return executionHistory.events.find((event) => {
const isCompletionEvent = this.completionEvents.includes(event.type);
const previousEventIsStartEvent = event.previousEventId === startEvent.id;
return isCompletionEvent && previousEventIsStartEvent;
});
}
/**
* Get the information for an instance of a step execution. Get the schedule, start,
* and complete event.
*
* @param {Object} executionHistory - AWS Step Function execution history
* @param {Object} scheduleEvent - AWS Step Function schedule-type event
* @returns {Object} object containing a schedule event, start event, and complete
* event if exists for each execution of the step, null if cannot find the step
*/
getStepExecutionInstance(executionHistory, scheduleEvent) {
if (scheduleEvent.type !== this.scheduleFailedEvent) {
const startEvent = this.getStartEvent(executionHistory, scheduleEvent);
if (startEvent && startEvent.type !== this.startFailedEvent) {
const completeEvent = this.getCompletionEvent(executionHistory, startEvent);
return { scheduleEvent, startEvent, completeEvent };
}
}
return null;
}
/**
* Get the events for the step execution for the given workflow execution.
* If there are multiple executions of a step, we currently assume a retry and return
* either the first passed execution or the last execution if no passing executions exist
*
* @param {string} executionArn - ARN of the workflow execution
* @param {string} stepName - name of the step
* @returns {List<Object>} objects containing a schedule event, start event, and complete
* event if exists for each execution of the step, null if cannot find the step
*/
async getStepExecutions(executionArn, stepName) {
const executionHistory = await StepFunctions.getExecutionHistory({ executionArn });
// Get the event where the step was scheduled
const scheduleEvents = executionHistory.events.filter((event) => {
const eventScheduled = this.scheduleEvents.includes(event.type);
const eventDetails = event[this.eventDetailsKeys.scheduled];
const isStepEvent = eventDetails && eventDetails.resource.includes(stepName);
return eventScheduled && isStepEvent;
});
if (scheduleEvents.length === 0) {
log.info(`Could not find step ${stepName} in execution.`);
return null;
}
return scheduleEvents.map((e) => this.getStepExecutionInstance(executionHistory, e))
.filter((e) => e);
}
/**
* Return truthyness of an execution being successful.
*
* @param {Object} execution - stepFunction execution
* @returns {boolean} truthness of the execution being successful.
*/
completedSuccessfulFilter(execution) {
return (!isNil(execution.completeEvent)
&& execution.completeEvent.type === this.successEvent);
}
/**
* Gets the input to the step by looking for the 'schedule' for the given step
* and returning the parsed input object.
*
* @param {string} workflowExecutionArn - AWS Execution ARN of the step function execution
* @param {string} stepName - Name of the workflow step of interest
* @returns {Object} - Parsed JSON string of input to step with <stepName>
* from the workflow execution of interest.
*/
async getStepInput(workflowExecutionArn, stepName) {
const stepExecutions = await this.getStepExecutions(workflowExecutionArn, stepName);
if (stepExecutions === null || stepExecutions.length === 0) {
log.info(`Could not find step ${stepName} in execution.`);
return null;
}
const scheduleEvent = stepExecutions[0].scheduleEvent;
const eventWasSuccessful = scheduleEvent.type === this.scheduleSuccessfulEvent;
if (!eventWasSuccessful) log.info('Schedule event failed');
const subStepExecutionDetails = scheduleEvent[this.eventDetailsKeys.scheduled];
const stepInput = JSON.parse(subStepExecutionDetails.input);
return parseStepMessage(stepInput, stepName);
}
/**
* Returns JSON-parsed output from a step which completed successfully
*
* @param {Object} stepExecution - AWS StepExecution
* @param {string} stepName - Name of the step
* @returns {Object} Output of the successfully completed event
*/
getSuccessOutput(stepExecution, stepName) {
if (stepExecution.completeEvent.type !== this.successEvent) {
log.info(`Step ${stepName} did not complete successfully, as expected.`);
return null;
}
const completeEventOutput = stepExecution.completeEvent[this.eventDetailsKeys.succeeded];
return JSON.parse(completeEventOutput.output.toString());
}
/**
* Returns JSON-parsed output from a step which failed
*
* @param {Object} stepExecution - AWS StepExecution
* @param {string} stepName - Name of the step
* @returns {Object} Output of the failed event
*/
getFailureOutput(stepExecution, stepName) {
if (stepExecution.completeEvent.type !== this.failureEvent) {
log.info(`Step ${stepName} did not fail, as expected.`);
return null;
}
const completeEventOutput = stepExecution.completeEvent[this.eventDetailsKeys.failed];
return completeEventOutput;
}
/**
* Get the output payload from the step, if the step succeeds
*
* @param {string} workflowExecutionArn - ARN of the workflow execution
* @param {string} stepName - name of the step
* @param {string} eventType - expected type of event, should be 'success' or 'failure'
* @returns {Object} object containing the payload, null if error
*/
async getStepOutput(workflowExecutionArn, stepName, eventType = 'success') {
const stepExecutions = await this.getStepExecutions(workflowExecutionArn, stepName);
if (stepExecutions === null) {
log.info(`Could not find step ${stepName} in execution.`);
return null;
}
// If querying for successful step output, use the first successful
// execution or the last execution if none were successful
let stepExecution;
const successfulPassedExecutions = stepExecutions
.filter((e) => this.completedSuccessfulFilter(e));
if (eventType === 'success'
&& successfulPassedExecutions
&& successfulPassedExecutions.length > 0) {
stepExecution = successfulPassedExecutions[0];
} else {
stepExecution = stepExecutions[stepExecutions.length - 1];
}
if (isNil(stepExecution.completeEvent)) {
log.info(`Step ${stepName} did not complete as expected.`);
return null;
}
let stepOutput = {};
if (eventType === 'success') {
stepOutput = this.getSuccessOutput(stepExecution, stepName);
} else if (eventType === 'failure') {
stepOutput = this.getFailureOutput(stepExecution, stepName);
}
if (!stepOutput) {
log.info(`Step ${stepName} did not complete ${eventType} as expected.`);
return null;
}
if (stepOutput.replace) {
// Message was too large and output was written to S3
log.info(`Retrieving ${stepName} output from ${JSON.stringify(stepOutput.replace)}`);
stepOutput = pullStepFunctionEvent(stepOutput);
}
return stepOutput;
}
}
/**
* `LambdaStep` is a step inside a step function that runs an AWS Lambda function.
*/
class LambdaStep extends SfnStep {
constructor() {
super();
this.scheduleFailedEvent = 'LambdaFunctionScheduleFailed';
this.scheduleSuccessfulEvent = 'LambdaFunctionScheduled';
this.scheduleEvents = [
this.scheduleFailedEvent,
this.scheduleSuccessfulEvent,
];
this.startFailedEvent = 'LambdaFunctionStartFailed';
this.startEvents = [
this.startFailedEvent,
'LambdaFunctionStarted',
];
this.successEvent = 'LambdaFunctionSucceeded';
this.taskStartEvent = 'TaskStateEntered';
this.failureEvent = 'LambdaFunctionFailed';
this.completionEvents = [
this.successEvent,
this.failureEvent,
'LambdaFunctionTimedOut',
];
this.eventDetailsKeys = {
scheduled: 'lambdaFunctionScheduledEventDetails',
succeeded: 'lambdaFunctionSucceededEventDetails',
failed: 'lambdaFunctionFailedEventDetails',
};
this.classType = 'lambda';
}
}
/**
* `ActivityStep` is a step inside a step function that runs an AWS ECS activity.
*/
class ActivityStep extends SfnStep {
constructor() {
super();
this.scheduleFailedEvent = 'ActivityScheduleFailed';
this.scheduleEvents = [
'ActivityScheduled',
this.scheduleFailedEvent,
];
this.startEvents = ['ActivityStarted'];
this.startFailedEvent = undefined; // there is no 'ActivityStartFailed'
this.successEvent = 'ActivitySucceeded';
this.failureEvent = 'ActivityFailed';
this.completionEvents = [
this.successEvent,
this.failureEvent,
'ActivityTimedOut',
];
this.eventDetailsKeys = {
scheduled: 'activityScheduledEventDetails',
succeeded: 'activitySucceededEventDetails',
failed: 'activityFailedEventDetails',
};
this.classType = 'activity';
}
}
module.exports = {
SfnStep,
ActivityStep,
LambdaStep,
};