Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ const crypto = __nccwpck_require__(6982);
const MAX_WAIT_MINUTES = 360; // 6 hours
const WAIT_DEFAULT_DELAY_SEC = 15;

// AbortController for cancelling long-running wait operations on workflow cancellation
const abortController = new AbortController();
const abortSignal = abortController.signal;

function registerCancellationHandlers() {
const handler = () => {
core.info('Received cancellation signal. Aborting wait operations...');
abortController.abort();
};
process.on('SIGINT', handler);
process.on('SIGTERM', handler);
}

// Attributes that are returned by DescribeTaskDefinition, but are not valid RegisterTaskDefinition inputs
const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
'compatibilities',
Expand Down Expand Up @@ -162,6 +175,7 @@ async function waitForTasksStopped(ecs, clusterName, taskArns, waitForMinutes, w
client: ecs,
minDelay: WAIT_DEFAULT_DELAY_SEC,
maxWaitTime: waitForMinutes * 60,
abortSignal,
};

if (waitMaxDelaySeconds) {
Expand Down Expand Up @@ -252,7 +266,8 @@ async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForSe
const waiterConfig = {
client: ecs,
minDelay: WAIT_DEFAULT_DELAY_SEC,
maxWaitTime: waitForMinutes * 60
maxWaitTime: waitForMinutes * 60,
abortSignal,
};

if (waitMaxDelaySeconds) {
Expand Down Expand Up @@ -477,7 +492,8 @@ async function createCodeDeployDeployment(codedeploy, clusterName, service, task
const waiterConfig = {
client: codedeploy,
minDelay: WAIT_DEFAULT_DELAY_SEC,
maxWaitTime: totalWaitMin * 60
maxWaitTime: totalWaitMin * 60,
abortSignal,
};

if (waitMaxDelaySeconds) {
Expand All @@ -493,6 +509,8 @@ async function createCodeDeployDeployment(codedeploy, clusterName, service, task
}

async function run() {
registerCancellationHandlers();

try {
// Get inputs
const taskDefinitionFile = core.getInput('task-definition', { required: true });
Expand Down
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ const crypto = require('crypto');
const MAX_WAIT_MINUTES = 360; // 6 hours
const WAIT_DEFAULT_DELAY_SEC = 15;

// AbortController for cancelling long-running wait operations on workflow cancellation
const abortController = new AbortController();
const abortSignal = abortController.signal;

function registerCancellationHandlers() {
const handler = () => {
core.info('Received cancellation signal. Aborting wait operations...');
abortController.abort();
};
process.on('SIGINT', handler);
process.on('SIGTERM', handler);
}

// Attributes that are returned by DescribeTaskDefinition, but are not valid RegisterTaskDefinition inputs
const IGNORED_TASK_DEFINITION_ATTRIBUTES = [
'compatibilities',
Expand Down Expand Up @@ -156,6 +169,7 @@ async function waitForTasksStopped(ecs, clusterName, taskArns, waitForMinutes, w
client: ecs,
minDelay: WAIT_DEFAULT_DELAY_SEC,
maxWaitTime: waitForMinutes * 60,
abortSignal,
};

if (waitMaxDelaySeconds) {
Expand Down Expand Up @@ -246,7 +260,8 @@ async function updateEcsService(ecs, clusterName, service, taskDefArn, waitForSe
const waiterConfig = {
client: ecs,
minDelay: WAIT_DEFAULT_DELAY_SEC,
maxWaitTime: waitForMinutes * 60
maxWaitTime: waitForMinutes * 60,
abortSignal,
};

if (waitMaxDelaySeconds) {
Expand Down Expand Up @@ -471,7 +486,8 @@ async function createCodeDeployDeployment(codedeploy, clusterName, service, task
const waiterConfig = {
client: codedeploy,
minDelay: WAIT_DEFAULT_DELAY_SEC,
maxWaitTime: totalWaitMin * 60
maxWaitTime: totalWaitMin * 60,
abortSignal,
};

if (waitMaxDelaySeconds) {
Expand All @@ -487,6 +503,8 @@ async function createCodeDeployDeployment(codedeploy, clusterName, service, task
}

async function run() {
registerCancellationHandlers();

try {
// Get inputs
const taskDefinitionFile = core.getInput('task-definition', { required: true });
Expand Down
39 changes: 39 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ describe('Deploy to ECS', () => {
EXPECTED_CODE_DEPLOY_TERMINATION_WAIT_TIME +
EXPECTED_CODE_DEPLOY_DEPLOYMENT_READY_WAIT_TIME
) * 60,
abortSignal: expect.any(AbortSignal),
},
{
deploymentId: 'deployment-1',
Expand Down Expand Up @@ -768,6 +769,7 @@ describe('Deploy to ECS', () => {
EXPECTED_CODE_DEPLOY_TERMINATION_WAIT_TIME +
EXPECTED_CODE_DEPLOY_DEPLOYMENT_READY_WAIT_TIME
) * 60,
abortSignal: expect.any(AbortSignal),
},
{
deploymentId: 'deployment-1',
Expand Down Expand Up @@ -841,6 +843,7 @@ describe('Deploy to ECS', () => {
client: mockCodeDeployClient,
minDelay: 15,
maxWaitTime: 6 * 60 * 60,
abortSignal: expect.any(AbortSignal),
},
{
deploymentId: 'deployment-1',
Expand Down Expand Up @@ -1041,6 +1044,7 @@ describe('Deploy to ECS', () => {
EXPECTED_CODE_DEPLOY_TERMINATION_WAIT_TIME +
EXPECTED_CODE_DEPLOY_DEPLOYMENT_READY_WAIT_TIME
) * 60,
abortSignal: expect.any(AbortSignal),
},
{
deploymentId: 'deployment-1',
Expand Down Expand Up @@ -1089,6 +1093,7 @@ describe('Deploy to ECS', () => {
EXPECTED_CODE_DEPLOY_TERMINATION_WAIT_TIME +
EXPECTED_CODE_DEPLOY_DEPLOYMENT_READY_WAIT_TIME
) * 60,
abortSignal: expect.any(AbortSignal),
},
{
deploymentId: 'deployment-1',
Expand Down Expand Up @@ -1151,6 +1156,7 @@ describe('Deploy to ECS', () => {
client: mockEcsClient,
minDelay: 15,
maxWaitTime: EXPECTED_DEFAULT_WAIT_TIME * 60,
abortSignal: expect.any(AbortSignal),
},
{
services: ['service-456'],
Expand Down Expand Up @@ -1194,6 +1200,7 @@ describe('Deploy to ECS', () => {
client: mockEcsClient,
minDelay: 15,
maxWaitTime: 60 * 60,
abortSignal: expect.any(AbortSignal),
},
{
services: ['service-456'],
Expand Down Expand Up @@ -1237,6 +1244,7 @@ describe('Deploy to ECS', () => {
client: mockEcsClient,
minDelay: 15,
maxWaitTime: 6 * 60 * 60,
abortSignal: expect.any(AbortSignal),
},
{
services: ['service-456'],
Expand Down Expand Up @@ -1265,6 +1273,7 @@ describe('Deploy to ECS', () => {
minDelay: 15,
maxDelay: 15,
maxWaitTime: EXPECTED_DEFAULT_WAIT_TIME * 60,
abortSignal: expect.any(AbortSignal),
},
{
services: ['service-456'],
Expand Down Expand Up @@ -1567,6 +1576,7 @@ describe('Deploy to ECS', () => {
minDelay: 15,
maxDelay: 15,
maxWaitTime: EXPECTED_DEFAULT_WAIT_TIME * 60,
abortSignal: expect.any(AbortSignal),
},
{
cluster: 'somecluster',
Expand Down Expand Up @@ -2153,4 +2163,33 @@ describe('Deploy to ECS', () => {
}]
});
});

test('aborts service wait when cancellation signal is emitted', async () => {
core.getInput = jest.fn(input => {
if (input === 'task-definition') return 'task-definition.json';
if (input === 'service') return 'service-456';
if (input === 'cluster') return 'cluster-789';
if (input === 'wait-for-service-stability') return 'true';
return '';
});

let receivedAbortSignal;
waitUntilServicesStable.mockImplementation(({ abortSignal }) => {
receivedAbortSignal = abortSignal;
return new Promise((resolve, reject) => {
abortSignal.addEventListener('abort', () => {
reject(new Error('Wait aborted by signal'));
});
});
});

const runPromise = run();
await new Promise(resolve => setImmediate(resolve));
process.emit('SIGTERM');
await runPromise;

expect(receivedAbortSignal).toBeDefined();
expect(receivedAbortSignal.aborted).toBe(true);
expect(core.setFailed).toHaveBeenCalledWith('Wait aborted by signal');
});
});