Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
refactor(runners): Move bootTimeExceeded to runners module (#2880)
Browse files Browse the repository at this point in the history
refactor: Move bootTimeExceeded to runners module
  • Loading branch information
M1kep authored Jan 12, 2023
1 parent af04380 commit 3247f8d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
8 changes: 8 additions & 0 deletions modules/runners/lambdas/runners/src/aws/runners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EC2, SSM } from 'aws-sdk';
import moment from 'moment';

import { LogFields, logger as rootLogger } from '../logger';
import ScaleError from './../scale-runners/ScaleError';
Expand Down Expand Up @@ -275,3 +276,10 @@ export async function createRunner(runnerParameters: RunnerInputParameters): Pro
}
}
}

// If launchTime is undefined, this will return false
export function bootTimeExceeded(ec2Runner: { launchTime?: Date }): boolean {
const runnerBootTimeInMinutes = process.env.RUNNER_BOOT_TIME_IN_MINUTES;
const launchTimePlusBootTime = moment(ec2Runner.launchTime).utc().add(runnerBootTimeInMinutes, 'minutes');
return launchTimePlusBootTime < moment(new Date()).utc();
}
5 changes: 4 additions & 1 deletion modules/runners/lambdas/runners/src/pool/pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ jest.mock('@octokit/rest', () => ({
Octokit: jest.fn().mockImplementation(() => mockOctokit),
}));

jest.mock('./../aws/runners');
jest.mock('./../aws/runners', () => ({
...jest.requireActual('./../aws/runners'),
listEC2Runners: jest.fn(),
}));
jest.mock('./../gh-auth/gh-auth');
jest.mock('./../scale-runners/scale-up');

Expand Down
9 changes: 1 addition & 8 deletions modules/runners/lambdas/runners/src/pool/pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import moment from 'moment';
import yn from 'yn';

import { RunnerList, listEC2Runners } from '../aws/runners';
import { bootTimeExceeded, listEC2Runners } from '../aws/runners';
import { createGithubAppAuth, createGithubInstallationAuth, createOctoClient } from '../gh-auth/gh-auth';
import { logger as rootLogger } from '../logger';
import { createRunners } from '../scale-runners/scale-up';
Expand Down Expand Up @@ -128,9 +127,3 @@ async function getInstallationId(ghesApiUrl: string, org: string): Promise<numbe
})
).data.id;
}

function bootTimeExceeded(ec2Runner: RunnerList): boolean {
const runnerBootTimeInMinutes = process.env.RUNNER_BOOT_TIME_IN_MINUTES;
const launchTimePlusBootTime = moment(ec2Runner.launchTime).utc().add(runnerBootTimeInMinutes, 'minutes');
return launchTimePlusBootTime < moment(new Date()).utc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ jest.mock('@octokit/rest', () => ({
Octokit: jest.fn().mockImplementation(() => mockOctokit),
}));

jest.mock('./../aws/runners');
jest.mock('./../aws/runners', () => ({
...jest.requireActual('./../aws/runners'),
terminateRunner: jest.fn(),
listEC2Runners: jest.fn(),
}));
jest.mock('./../gh-auth/gh-auth');
jest.mock('./cache');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment from 'moment';

import { createGithubAppAuth, createGithubInstallationAuth, createOctoClient } from '../gh-auth/gh-auth';
import { LogFields, logger as rootLogger } from '../logger';
import { RunnerInfo, RunnerList, listEC2Runners, terminateRunner } from './../aws/runners';
import { RunnerInfo, RunnerList, bootTimeExceeded, listEC2Runners, terminateRunner } from './../aws/runners';
import { GhRunners, githubCache } from './cache';
import { ScalingDownConfig, getIdleRunnerCount } from './scale-down-config';

Expand Down Expand Up @@ -101,12 +101,6 @@ function runnerMinimumTimeExceeded(runner: RunnerInfo): boolean {
return launchTimePlusMinimum < now;
}

function bootTimeExceeded(ec2Runner: RunnerInfo): boolean {
const runnerBootTimeInMinutes = process.env.RUNNER_BOOT_TIME_IN_MINUTES;
const launchTimePlusBootTime = moment(ec2Runner.launchTime).utc().add(runnerBootTimeInMinutes, 'minutes');
return launchTimePlusBootTime < moment(new Date()).utc();
}

async function removeRunner(ec2runner: RunnerInfo, ghRunnerIds: number[]): Promise<void> {
const githubAppClient = await getOrCreateOctokit(ec2runner);
try {
Expand Down

0 comments on commit 3247f8d

Please sign in to comment.