Skip to content

Commit 43e66c9

Browse files
authored
fix: Remove Orphan AWS runners (#79)
* Remove Orphan AWS runners * Remove duplicated check for time exceede
1 parent 274100d commit 43e66c9

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

modules/runners/lambdas/runners/src/scale-runners/scale-down.test.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface TestData {
3232
}
3333

3434
const environment = 'unit-test-environment';
35+
const minimumRunningTimeInMinutes = 15;
3536
const TEST_DATA: TestData = {
3637
repositoryName: 'hello-world',
3738
repositoryOwner: 'Codertocat',
@@ -58,7 +59,17 @@ const DEFAULT_RUNNERS = [
5859
},
5960
{
6061
instanceId: 'i-not-registered-104',
61-
launchTime: moment(new Date()).subtract(5, 'minutes').toDate(),
62+
launchTime: moment(new Date())
63+
.subtract(minimumRunningTimeInMinutes - 1, 'minutes')
64+
.toDate(),
65+
repo: `doe/another-repo`,
66+
org: undefined,
67+
},
68+
{
69+
instanceId: 'i-not-registered-105',
70+
launchTime: moment(new Date())
71+
.subtract(minimumRunningTimeInMinutes + 5, 'minutes')
72+
.toDate(),
6273
repo: `doe/another-repo`,
6374
org: undefined,
6475
},
@@ -73,7 +84,7 @@ const DEFAULT_REGISTERED_RUNNERS: any = {
7384
},
7485
{
7586
id: 102,
76-
name: 'i-idle-101',
87+
name: 'i-idle-102',
7788
},
7889
{
7990
id: 103,
@@ -91,8 +102,7 @@ describe('scaleDown', () => {
91102
process.env.GITHUB_APP_CLIENT_SECRET = 'TEST_CLIENT_SECRET';
92103
process.env.RUNNERS_MAXIMUM_COUNT = '3';
93104
process.env.ENVIRONMENT = environment;
94-
const minimumRunningTimeInMinutes = '15';
95-
process.env.MINIMUM_RUNNING_TIME_IN_MINUTES = minimumRunningTimeInMinutes;
105+
process.env.MINIMUM_RUNNING_TIME_IN_MINUTES = minimumRunningTimeInMinutes.toString();
96106
jest.clearAllMocks();
97107
mockOctokit.apps.getOrgInstallation.mockImplementation(() => ({
98108
data: {
@@ -166,15 +176,17 @@ describe('scaleDown', () => {
166176
});
167177
});
168178

169-
it('Terminate 2 of 4 runners for repo.', async () => {
179+
it('Terminate 3 of 5 runners for repo.', async () => {
170180
await scaleDown();
171181
expect(listRunners).toBeCalledWith({
172182
environment: environment,
173183
});
174184

175185
expect(mockOctokit.apps.getRepoInstallation).toBeCalled();
176-
177-
expect(terminateRunner).toBeCalledTimes(2);
186+
expect(terminateRunner).toBeCalledTimes(3);
187+
for (const toTerminate of [DEFAULT_RUNNERS[0], DEFAULT_RUNNERS[1], DEFAULT_RUNNERS[4]]) {
188+
expect(terminateRunner).toHaveBeenCalledWith(toTerminate);
189+
}
178190
});
179191
});
180192

@@ -187,15 +199,17 @@ describe('scaleDown', () => {
187199
});
188200
});
189201

190-
it('Terminate 2 of 4 runners for org.', async () => {
202+
it('Terminate 3 of 5 runners for org.', async () => {
191203
await scaleDown();
192204
expect(listRunners).toBeCalledWith({
193205
environment: environment,
194206
});
195207

196208
expect(mockOctokit.apps.getOrgInstallation).toBeCalled();
197-
198-
expect(terminateRunner).toBeCalledTimes(2);
209+
expect(terminateRunner).toBeCalledTimes(3);
210+
for (const toTerminate of [DEFAULT_RUNNERS[0], DEFAULT_RUNNERS[1], DEFAULT_RUNNERS[4]]) {
211+
expect(terminateRunner).toHaveBeenCalledWith(toTerminate);
212+
}
199213
});
200214
});
201215
});

modules/runners/lambdas/runners/src/scale-runners/scale-down.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ export async function scaleDown(): Promise<void> {
6262
}
6363

6464
for (const ec2runner of runners) {
65-
const githubAppClient = await createGitHubClientForRunner(ec2runner, enableOrgLevel);
65+
if (!runnerMinimumTimeExceeded(ec2runner, minimumRunningTimeInMinutes)) {
66+
continue;
67+
}
6668

69+
const githubAppClient = await createGitHubClientForRunner(ec2runner, enableOrgLevel);
6770
const repo = getRepo(ec2runner, enableOrgLevel);
6871
const registered = enableOrgLevel
6972
? await githubAppClient.actions.listSelfHostedRunnersForOrg({
@@ -74,9 +77,11 @@ export async function scaleDown(): Promise<void> {
7477
repo: repo.repoName,
7578
});
7679

80+
let orphanEc2Runner = true;
7781
for (const ghRunner of registered.data.runners) {
7882
const runnerName = ghRunner.name as string;
79-
if (runnerName === ec2runner.instanceId && runnerMinimumTimeExceeded(ec2runner, minimumRunningTimeInMinutes)) {
83+
if (runnerName === ec2runner.instanceId) {
84+
orphanEc2Runner = false;
8085
try {
8186
const result = enableOrgLevel
8287
? await githubAppClient.actions.deleteSelfHostedRunnerFromOrg({
@@ -100,5 +105,15 @@ export async function scaleDown(): Promise<void> {
100105
}
101106
}
102107
}
108+
109+
// Remove orphan AWS runners.
110+
if (orphanEc2Runner) {
111+
console.info(`Runner '${ec2runner.instanceId}' is orphan, and will be removed.`);
112+
try {
113+
await terminateRunner(ec2runner);
114+
} catch (e) {
115+
console.debug(`Orphan runner '${ec2runner.instanceId}' cannot be removed.`);
116+
}
117+
}
103118
}
104119
}

0 commit comments

Comments
 (0)