This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathbulkExportStateMachine.ts
113 lines (100 loc) · 4.56 KB
/
bulkExportStateMachine.ts
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
import { Duration } from 'aws-cdk-lib';
import { Function } from 'aws-cdk-lib/aws-lambda';
import { LogGroup } from 'aws-cdk-lib/aws-logs';
import {
TaskInput,
Wait,
WaitTime,
Choice,
Condition,
Parallel,
StateMachine,
LogLevel,
} from 'aws-cdk-lib/aws-stepfunctions';
import { LambdaInvoke } from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { Construct } from 'constructs';
export default class BulkExportStateMachine {
bulkExportStateMachine: StateMachine;
constructor(
scope: Construct,
updateStatusLambdaFunction: Function,
startExportJobLambdaFunction: Function,
getExportJobLambdaFunction: Function,
stopExportJobLambdaFunction: Function,
stage: string,
) {
const catchAllUpdateStatusToFailed = new LambdaInvoke(scope, 'catchAllUpdateStatusToFailed', {
lambdaFunction: updateStatusLambdaFunction,
payload: TaskInput.fromObject({
'globalParams.$': '$',
status: 'failed',
}),
});
const updateStatusToFailed = new LambdaInvoke(scope, 'updateStatusToFailed', {
lambdaFunction: updateStatusLambdaFunction,
payload: TaskInput.fromObject({
'globalParams.$': '$',
status: 'failed',
}),
});
const updateStatusToCanceled = new LambdaInvoke(scope, 'updateStatusToCanceled', {
lambdaFunction: updateStatusLambdaFunction,
payload: TaskInput.fromObject({
'globalParams.$': '$',
status: 'canceled',
}),
});
const updateStatusToCompleted = new LambdaInvoke(scope, 'updateStatusToCompleted', {
lambdaFunction: updateStatusLambdaFunction,
payload: TaskInput.fromObject({
'globalParams.$': '$',
status: 'completed',
}),
});
const stopExportJob = new LambdaInvoke(scope, 'stopExportJob', {
lambdaFunction: stopExportJobLambdaFunction,
outputPath: '$.Payload',
}).next(updateStatusToCanceled);
const waitForExportJob = new Wait(scope, 'waitForExportJob', {
time: WaitTime.duration(Duration.seconds(10)),
});
const choiceOnJobStatus = new Choice(scope, 'choiceOnJobStatus')
.when(Condition.booleanEquals('$.executionParameters.isCanceled', true), stopExportJob)
.when(
Condition.stringEquals('$.executionParameters.glueJobRunStatus', 'SUCCEEDED'),
updateStatusToCompleted,
)
.when(Condition.stringEquals('$.executionParameters.glueJobRunStatus', 'STARTING'), waitForExportJob)
.when(Condition.stringEquals('$.executionParameters.glueJobRunStatus', 'RUNNING'), waitForExportJob)
.when(Condition.stringEquals('$.executionParameters.glueJobRunStatus', 'FAILED'), updateStatusToFailed)
.when(Condition.stringEquals('$.executionParameters.glueJobRunStatus', 'TIMEOUT'), updateStatusToFailed)
// STOPPING and STOPPED can only occur here if the job was forcefully stopped with a Glue API call from outside the FHIR server, so we treat it as failure
.when(Condition.stringEquals('$.executionParameters.glueJobRunStatus', 'STOPPING'), updateStatusToFailed)
.when(Condition.stringEquals('$.executionParameters.glueJobRunStatus', 'STOPPED'), updateStatusToFailed);
const getJobStatus = new LambdaInvoke(scope, 'getJobStatus', {
lambdaFunction: getExportJobLambdaFunction,
outputPath: '$.Payload',
}).next(choiceOnJobStatus);
waitForExportJob.next(getJobStatus);
const startExportJob = new LambdaInvoke(scope, 'startExportJob', {
lambdaFunction: startExportJobLambdaFunction,
outputPath: '$.Payload',
}).next(waitForExportJob);
const parallelHelper = new Parallel(scope, 'parallelHelper');
parallelHelper.addCatch(catchAllUpdateStatusToFailed, {
resultPath: '$.error',
});
parallelHelper.branch(startExportJob);
this.bulkExportStateMachine = new StateMachine(scope, 'bulkExportStateMachine', {
stateMachineName: `BulkExportStateMachine-${stage}`,
definition: parallelHelper,
tracingEnabled: true,
logs: {
level: LogLevel.ALL,
destination: new LogGroup(scope, 'bulkExportStateMachineLogs', {
logGroupName: `BulkExportSM-Logs-${stage}`,
}),
},
});
}
}