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

Commit

Permalink
fix(webhook): Don't log warning when secondary job queue is empty (#3…
Browse files Browse the repository at this point in the history
…942)

Right now the Terraform module is causing `${SQS_WORKFLOW_JOB_QUEUE}` to
be set to an empty string. Since we pass this through currently, and
explicitly check for `!== undefined` - not any falsy value - we end up
trying to send to an empty queue and logging a warning.

Doesn't break anything, but it's noisy in the logs.

Fix this by checking for any falsy value instead, and also using `||`
instead of `??` when setting the variable in the first place, so an
empty string ends up as `undefined`. Also, modify the testsuite to check
for the `SQS` being created at all, since that happens earlier on and
reproduces the failure.

This is a companion to #3943. This one stops the warning, and that one
fixes the original cause by not setting the env var in the first place.
Both could be merged, ideally.

Co-authored-by: Niek Palm <[email protected]>
  • Loading branch information
iainlane and npalm authored Jun 28, 2024
1 parent 6c48dff commit ef25bd4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lambdas/functions/webhook/src/ConfigResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Config {
Config.matcherConfig = JSON.parse(matcherConfigVal) as Array<RunnerMatcherConfig>;
logger.debug('Loaded queues config', { matcherConfig: Config.matcherConfig });
}
const workflowJobEventSecondaryQueue = process.env.SQS_WORKFLOW_JOB_QUEUE ?? undefined;
const workflowJobEventSecondaryQueue = process.env.SQS_WORKFLOW_JOB_QUEUE || undefined;
return new Config(repositoryAllowList, workflowJobEventSecondaryQueue);
}

Expand Down
18 changes: 16 additions & 2 deletions lambdas/functions/webhook/src/sqs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jest.mock('@aws-sdk/client-sqs', () => ({
}));
jest.mock('@terraform-aws-github-runner/aws-ssm-util');

import { SQS } from '@aws-sdk/client-sqs';

describe('Test sending message to SQS.', () => {
const queueUrl = 'https://sqs.eu-west-1.amazonaws.com/123456789/queued-builds';
const message = {
Expand Down Expand Up @@ -98,15 +100,27 @@ describe('Test sending message to SQS.', () => {
expect(result).resolves;
});

it('Does not send webhook events to workflow job event copy queue', async () => {
it('Does not send webhook events to workflow job event copy queue when job queue is not in environment', async () => {
// Arrange
delete process.env.SQS_WORKFLOW_JOB_QUEUE;
const config = await Config.load();

// Act
await sendWebhookEventToWorkflowJobQueue(message, config);

// Assert
expect(SQS).not.toHaveBeenCalled();
});

it('Does not send webhook events to workflow job event copy queue when job queue is set to empty string', async () => {
// Arrange
process.env.SQS_WORKFLOW_JOB_QUEUE = '';
const config = await Config.load();
// Act
await sendWebhookEventToWorkflowJobQueue(message, config);

// Assert
expect(mockSQS.sendMessage).not.toHaveBeenCalledWith(sqsMessage);
expect(SQS).not.toHaveBeenCalled();
});

it('Catch the exception when even copy queue throws exception', async () => {
Expand Down
28 changes: 16 additions & 12 deletions lambdas/functions/webhook/src/sqs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ export const sendActionRequest = async (message: ActionRequestMessage): Promise<
};

export async function sendWebhookEventToWorkflowJobQueue(message: GithubWorkflowEvent, config: Config): Promise<void> {
if (config.workflowJobEventSecondaryQueue != undefined) {
const sqs = new SQS({ region: process.env.AWS_REGION });
const sqsMessage: SendMessageCommandInput = {
QueueUrl: String(config.workflowJobEventSecondaryQueue),
MessageBody: JSON.stringify(message),
};
logger.debug(`Sending Webhook events to the workflow job queue: ${config.workflowJobEventSecondaryQueue}`);
try {
await sqs.sendMessage(sqsMessage);
} catch (e) {
logger.warn(`Error in sending webhook events to workflow job queue: ${(e as Error).message}`);
}
if (!config.workflowJobEventSecondaryQueue) {
return;
}

const sqs = new SQS({ region: process.env.AWS_REGION });
const sqsMessage: SendMessageCommandInput = {
QueueUrl: String(config.workflowJobEventSecondaryQueue),
MessageBody: JSON.stringify(message),
};

logger.debug(`Sending Webhook events to the workflow job queue: ${config.workflowJobEventSecondaryQueue}`);

try {
await sqs.sendMessage(sqsMessage);
} catch (e) {
logger.warn(`Error in sending webhook events to workflow job queue: ${(e as Error).message}`);
}
}

0 comments on commit ef25bd4

Please sign in to comment.