Skip to content

Commit 8ff9901

Browse files
committed
fix: streamline orphan runner handling and improve logging
1 parent 15af241 commit 8ff9901

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,7 @@ async function getGitHubSelfHostedRunnerState(
6969
});
7070
metricGitHubAppRateLimit(state.headers);
7171

72-
return {
73-
id: state.data.id,
74-
name: state.data.name,
75-
busy: state.data.busy,
76-
status: state.data.status,
77-
os: state.data.os,
78-
labels: state.data.labels,
79-
runner_group_id: state.data.runner_group_id,
80-
ephemeral: state.data.ephemeral,
81-
};
72+
return state.data;
8273
}
8374

8475
async function getGitHubRunnerBusyState(client: Octokit, ec2runner: RunnerInfo, runnerId: number): Promise<boolean> {
@@ -221,33 +212,35 @@ async function markOrphan(instanceId: string): Promise<void> {
221212
}
222213
}
223214

224-
async function lastChanceCheckOrphanRunner(runner: RunnerList): Promise<void> {
215+
async function unmarkOrphan(instanceId: string): Promise<void> {
216+
try {
217+
await untag(instanceId, [{ Key: 'ghr:orphan', Value: 'true' }]);
218+
logger.info(`Runner '${instanceId}' unmarked as orphan.`);
219+
} catch (e) {
220+
logger.error(`Failed to unmark runner '${instanceId}' as orphan.`, { error: e });
221+
}
222+
}
223+
224+
async function lastChanceCheckOrphanRunner(runner: RunnerList): Promise<boolean> {
225225
const client = await getOrCreateOctokit(runner as RunnerInfo);
226226
const runnerId = parseInt(runner.runnerId || '0');
227227
const ec2Instance = runner as RunnerInfo;
228228
const state = await getGitHubSelfHostedRunnerState(client, ec2Instance, runnerId);
229+
var isOrphan = false;
229230
logger.debug(`Runner is currently '${runner.instanceId}' state: ${JSON.stringify(state)}`);
230-
if (state.status === 'online' && state.busy) {
231-
logger.info(`Runner '${runner.instanceId}' is orphan, but is online and busy.`);
232-
await untag(runner.instanceId, [{ Key: 'ghr:orphan', Value: 'true' }]);
233-
} else if (state.status === 'offline') {
234-
logger.warn(`Runner '${runner.instanceId}' is orphan.`);
235-
logger.info(`Terminating orphan runner '${runner.instanceId}'`);
236-
await terminateRunner(runner.instanceId).catch((e) => {
237-
logger.error(`Failed to terminate orphan runner '${runner.instanceId}'`, { error: e });
238-
});
231+
if (state.status === 'offline') {
232+
isOrphan = true;
239233
}
234+
return isOrphan
240235
}
241236

242237
async function terminateOrphan(environment: string): Promise<void> {
243238
try {
244239
const orphanRunners = await listEC2Runners({ environment, orphan: true });
245240

246241
for (const runner of orphanRunners) {
247-
// do we have a valid runnerId? then we are in a Jit Runner scenario else, use old method
248242
if (runner.runnerId) {
249-
logger.debug(`Runner '${runner.instanceId}' is orphan, but has a runnerId.`);
250-
await lastChanceCheckOrphanRunner(runner);
243+
await lastChanceCheckOrphanRunner(runner) ? terminateRunner(runner.instanceId) : unmarkOrphan(runner.instanceId);
251244
} else {
252245
logger.info(`Terminating orphan runner '${runner.instanceId}'`);
253246
await terminateRunner(runner.instanceId).catch((e) => {

0 commit comments

Comments
 (0)