Skip to content

Commit 7b70b29

Browse files
committed
5983 Add dependency in BulkExportTaskResources
1 parent fc16dd7 commit 7b70b29

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

java/cdk/src/main/java/sleeper/cdk/stack/SleeperCoreStacks.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
22+
import software.amazon.awscdk.CustomResource;
2223
import software.amazon.awscdk.services.cloudwatch.IMetric;
2324
import software.amazon.awscdk.services.ecs.ICluster;
2425
import software.amazon.awscdk.services.iam.IGrantable;
@@ -218,6 +219,13 @@ public void addAutoStopEcsClusterTasks(Construct scope, ICluster cluster) {
218219
autoStopEcsStack.addAutoStopEcsClusterTasks(scope, cluster);
219220
}
220221

222+
public void addAutoStopEcsClusterTasksAfterTaskCreatorIsDeleted(Construct scope, ICluster cluster, IFunction taskCreator) {
223+
CustomResource autoStop = autoStopEcsStack.addAutoStopEcsClusterTasks(scope, cluster);
224+
// This dependency means that during teardown the task creator lambda will be deleted before ECS tasks are stopped.
225+
// This is important otherwise more tasks may be created as they are being stopped.
226+
taskCreator.getNode().addDependency(autoStop);
227+
}
228+
221229
public AutoStopEcsClusterTasksStack getAutoStopEcsStack() {
222230
return autoStopEcsStack;
223231
}

java/cdk/src/main/java/sleeper/cdk/stack/bulkexport/BulkExportTaskResources.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ public BulkExportTaskResources(
8282
this.props = props;
8383
this.instanceProperties = props.getInstanceProperties();
8484
this.jobsQueue = jobsQueue;
85-
ecsClusterForBulkExportTasks(coreStacks, jarsBucket, resultsBucket);
86-
lambdaToCreateTasks(coreStacks, lambdaCode);
85+
Cluster cluster = ecsClusterForBulkExportTasks(coreStacks, jarsBucket, resultsBucket);
86+
IFunction taskCreator = lambdaToCreateTasks(coreStacks, lambdaCode);
87+
coreStacks.addAutoStopEcsClusterTasksAfterTaskCreatorIsDeleted(stack, cluster, taskCreator);
8788
}
8889

89-
private void lambdaToCreateTasks(SleeperCoreStacks coreStacks, SleeperLambdaCode lambdaCode) {
90+
private IFunction lambdaToCreateTasks(SleeperCoreStacks coreStacks, SleeperLambdaCode lambdaCode) {
9091
String instanceId = Utils.cleanInstanceId(instanceProperties);
9192
String functionName = String.join("-", "sleeper",
9293
instanceId, "bulk-export-tasks-creator");
@@ -128,9 +129,11 @@ private void lambdaToCreateTasks(SleeperCoreStacks coreStacks, SleeperLambdaCode
128129
.build();
129130
instanceProperties.set(BULK_EXPORT_TASK_CREATION_LAMBDA_FUNCTION, handler.getFunctionName());
130131
instanceProperties.set(BULK_EXPORT_TASK_CREATION_CLOUDWATCH_RULE, rule.getRuleName());
132+
133+
return handler;
131134
}
132135

133-
private void ecsClusterForBulkExportTasks(
136+
private Cluster ecsClusterForBulkExportTasks(
134137
SleeperCoreStacks coreStacks, IBucket jarsBucket, IBucket resultsBucket) {
135138
VpcLookupOptions vpcLookupOptions = VpcLookupOptions.builder()
136139
.vpcId(instanceProperties.get(VPC_ID))
@@ -167,8 +170,7 @@ private void ecsClusterForBulkExportTasks(
167170
.build();
168171
new CfnOutput(stack, BULK_EXPORT_CLUSTER_NAME, bulkExportClusterProps);
169172

170-
coreStacks.addAutoStopEcsClusterTasks(stack, cluster);
171-
173+
return cluster;
172174
}
173175

174176
private static PolicyStatement runTasksPolicyStatement() {

java/cdk/src/main/java/sleeper/cdk/stack/core/AutoStopEcsClusterTasksStack.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ private void createLambda(InstanceProperties instanceProperties, SleeperJarsInBu
9797
/**
9898
* Adds a custom resource to stop tasks in an ECS cluster.
9999
*
100-
* @param scope the stack to add the custom resource to
101-
* @param cluster the ECS cluster
100+
* @param scope the stack to add the custom resource to
101+
* @param cluster the ECS cluster
102+
* @return the custom resource that will stop tasks when it is deleted
102103
*/
103-
public void addAutoStopEcsClusterTasks(Construct scope, ICluster cluster) {
104+
public CustomResource addAutoStopEcsClusterTasks(Construct scope, ICluster cluster) {
104105

105106
String id = cluster.getNode().getId() + "-AutoStop";
106107

@@ -112,6 +113,7 @@ public void addAutoStopEcsClusterTasks(Construct scope, ICluster cluster) {
112113

113114
customResource.getNode().addDependency(cluster);
114115

116+
return customResource;
115117
}
116118

117119
}

0 commit comments

Comments
 (0)