Skip to content

Commit

Permalink
queueprocessingservice changes
Browse files Browse the repository at this point in the history
  • Loading branch information
piradeepk committed Feb 17, 2021
1 parent ec9a552 commit 25a578c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ export interface QueueProcessingServiceBaseProps {
/**
* Maximum capacity to scale to.
*
* @default (desiredTaskCount * 2)
* @default 2
*/
readonly maxScalingCapacity?: number

/**
* Minimum capacity to scale to.
*
* @default 1
*/
readonly minScalingCapacity?: number

/**
* The intervals for scaling based on the SQS queue's ApproximateNumberOfMessagesVisible metric.
*
Expand Down Expand Up @@ -223,6 +230,11 @@ export abstract class QueueProcessingServiceBase extends CoreConstruct {
*/
public readonly maxCapacity: number;

/**
* The minimum number of instances for autoscaling to scale down to.
*/
public readonly minCapacity: number;

/**
* The scaling interval for autoscaling based off an SQS Queue size.
*/
Expand Down Expand Up @@ -275,12 +287,9 @@ export abstract class QueueProcessingServiceBase extends CoreConstruct {

// Determine the desired task count (minimum) and maximum scaling capacity
this.desiredCount = props.desiredTaskCount ?? 1;
this.minCapacity = props.minScalingCapacity || this.desiredCount;
this.maxCapacity = props.maxScalingCapacity || (2 * this.desiredCount);

if (!this.desiredCount && !this.maxCapacity) {
throw new Error('maxScalingCapacity must be set and greater than 0 if desiredCount is 0');
}

new CfnOutput(this, 'SQSQueue', { value: this.sqsQueue.queueName });
new CfnOutput(this, 'SQSQueueArn', { value: this.sqsQueue.queueArn });
}
Expand All @@ -291,7 +300,7 @@ export abstract class QueueProcessingServiceBase extends CoreConstruct {
* @param service the ECS/Fargate service for which to apply the autoscaling rules to
*/
protected configureAutoscalingForService(service: BaseService) {
const scalingTarget = service.autoScaleTaskCount({ maxCapacity: this.maxCapacity, minCapacity: this.desiredCount });
const scalingTarget = service.autoScaleTaskCount({ maxCapacity: this.maxCapacity, minCapacity: this.minCapacity });
scalingTarget.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 50,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,26 +264,6 @@ export = {
test.done();
},

'throws if desiredTaskCount and maxScalingCapacity are 0'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

// THEN
test.throws(() =>
new ecsPatterns.QueueProcessingEc2Service(stack, 'Service', {
cluster,
desiredTaskCount: 0,
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry('test'),
})
, /maxScalingCapacity must be set and greater than 0 if desiredCount is 0/);

test.done();
},

'can set custom containerName'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@
"MaximumPercent": 200,
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": false,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
Expand Down

0 comments on commit 25a578c

Please sign in to comment.