Skip to content

Commit 5a275e5

Browse files
committed
Implement last chance check for orphan runners and refactor termination logic
1 parent 971ec2d commit 5a275e5

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

lambdas/functions/control-plane/src/scale-runners/scale-down.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,24 @@ async function markOrphan(instanceId: string): Promise<void> {
218218
}
219219
}
220220

221+
async function lastChanceCheckOrphanRunner(runner: RunnerList): Promise<void> {
222+
const client = await getOrCreateOctokit(runner as RunnerInfo);
223+
const runnerId = parseInt(runner.runnerId);
224+
const ec2Instance = runner as RunnerInfo;
225+
const state = await getGitHubSelfHostedRunnerState(client, ec2Instance, runnerId);
226+
logger.debug(`Runner is currently '${runner.instanceId}' state: ${JSON.stringify(state)}`);
227+
if (state.status === 'online' && state.busy) {
228+
logger.info(`Runner '${runner.instanceId}' is orphan, but is online and busy.`);
229+
await untag(runner.instanceId, [{ Key: 'ghr:orphan', Value: 'true' }]);
230+
} else if (state.status === 'offline') {
231+
logger.warn(`Runner '${runner.instanceId}' is orphan.`);
232+
logger.info(`Terminating orphan runner '${runner.instanceId}'`);
233+
await terminateRunner(runner.instanceId).catch((e) => {
234+
logger.error(`Failed to terminate orphan runner '${runner.instanceId}'`, { error: e });
235+
});
236+
}
237+
}
238+
221239
async function terminateOrphan(environment: string): Promise<void> {
222240
try {
223241
const orphanRunners = await listEC2Runners({ environment, orphan: true });
@@ -226,22 +244,7 @@ async function terminateOrphan(environment: string): Promise<void> {
226244
// do we have a valid runnerId? then we are in a Jit Runner scenario else, use old method
227245
if (runner.runnerId) {
228246
logger.debug(`Runner '${runner.instanceId}' is orphan, but has a runnerId.`);
229-
// build a runner instance
230-
const client = await getOrCreateOctokit(runner as RunnerInfo);
231-
const runnerId = parseInt(runner.runnerId);
232-
const ec2Instance = runner as RunnerInfo;
233-
const state = await getGitHubSelfHostedRunnerState(client, ec2Instance, runnerId);
234-
logger.debug(`Runner is currently '${runner.instanceId}' state: ${JSON.stringify(state)}`);
235-
if (state.status === 'online' && state.busy) {
236-
logger.info(`Runner '${runner.instanceId}' is orphan, but is online and busy.`);
237-
await untag(runner.instanceId, [{ Key: 'ghr:orphan', Value: 'true' }]);
238-
} else if (state.status === 'offline') {
239-
logger.warn(`Runner '${runner.instanceId}' is orphan.`);
240-
logger.info(`Terminating orphan runner '${runner.instanceId}'`);
241-
await terminateRunner(runner.instanceId).catch((e) => {
242-
logger.error(`Failed to terminate orphan runner '${runner.instanceId}'`, { error: e });
243-
});
244-
}
247+
await lastChanceCheckOrphanRunner(runner);
245248
} else {
246249
logger.info(`Terminating orphan runner '${runner.instanceId}'`);
247250
await terminateRunner(runner.instanceId).catch((e) => {

0 commit comments

Comments
 (0)