Skip to content

Commit

Permalink
Process review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
npalm committed Dec 22, 2021
1 parent ec7af4c commit e08bd36
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions modules/runners/lambdas/runners/src/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async function testInvalidRecords(sqsRecords: SQSRecord[]) {

expect(logWarnSpy).toHaveBeenCalledWith(
'Event ignored, only one record at the time can be handled, ensure the lambda batch size is set to 1.',
undefined,
);
}

Expand Down
40 changes: 19 additions & 21 deletions modules/runners/lambdas/runners/src/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
import { scaleUp } from './scale-runners/scale-up';
import { scaleDown } from './scale-runners/scale-down';
import { SQSEvent, ScheduledEvent, Context, Callback } from 'aws-lambda';
import { logger } from './scale-runners/logger';
import { LogFields, logger } from './scale-runners/logger';
import ScaleError from './scale-runners/ScaleError';
import 'source-map-support/register';

export async function scaleUpHandler(event: SQSEvent, context: Context): Promise<void> {
logger.setSettings({ requestId: context.awsRequestId });
logger.debug(JSON.stringify(event));
if (event.Records.length !== 1) {
logger.warn('Event ignored, only one record at the time can be handled, ensure the lambda batch size is set to 1.');
logger.warn(
'Event ignored, only one record at the time can be handled, ensure the lambda batch size is set to 1.',
LogFields.print(),
);
return new Promise((resolve) => resolve());
}

return new Promise((resolve, reject) => {
scaleUp(event.Records[0].eventSource, JSON.parse(event.Records[0].body))
.then(() => resolve())
.catch((e: Error) => {
if (e instanceof ScaleError) {
reject(e);
} else {
logger.warn(`Ignoring error: ${e.message}`);
}
});
});
try {
await scaleUp(event.Records[0].eventSource, JSON.parse(event.Records[0].body));
} catch (e) {
if (e instanceof ScaleError) {
throw e;
} else {
logger.warn(`Ignoring error: ${(e as Error).message}`, LogFields.print());
}
}
}

export async function scaleDownHandler(event: ScheduledEvent, context: Context): Promise<void> {
logger.setSettings({ requestId: context.awsRequestId });

return new Promise((resolve) => {
scaleDown()
.then(() => resolve())
.catch((e) => {
logger.error(e);
resolve();
});
});
try {
await scaleDown();
} catch (e) {
logger.error(e);
}
}
10 changes: 8 additions & 2 deletions modules/runners/lambdas/runners/src/scale-runners/scale-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export interface ActionRequestMessage {
}

export async function scaleUp(eventSource: string, payload: ActionRequestMessage): Promise<void> {
logger.info(`Received ${payload.eventType} from ${payload.repositoryOwner}/${payload.repositoryName}`);
logger.info(
`Received ${payload.eventType} from ${payload.repositoryOwner}/${payload.repositoryName}`,
LogFields.print(),
);

if (eventSource !== 'aws:sqs') throw Error('Cannot handle non-SQS events!');
const enableOrgLevel = yn(process.env.ENABLE_ORGANIZATION_RUNNERS, { default: true });
Expand All @@ -28,7 +31,10 @@ export async function scaleUp(eventSource: string, payload: ActionRequestMessage
const ephemeralEnabled = yn(process.env.ENABLE_EPHEMERAL_RUNNERS, { default: false });

if (ephemeralEnabled && payload.eventType !== 'workflow_job') {
logger.warn(`${payload.eventType} event is not supported in combination with ephemeral runners.`);
logger.warn(
`${payload.eventType} event is not supported in combination with ephemeral runners.`,
LogFields.print(),
);
throw Error(
`The event type ${payload.eventType} is not supported in combination with ephemeral runners.` +
`Please ensure you have enabled workflow_job events.`,
Expand Down

0 comments on commit e08bd36

Please sign in to comment.