Skip to content

Commit

Permalink
[DOP-4171]: Add cache updater api stack
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Jan 23, 2024
1 parent 783f30d commit 7fa3cbc
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
20 changes: 16 additions & 4 deletions api/controllers/v2/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ function isRebuildRequest(body: unknown): body is RepoInfo[] {

async function runCacheRebuildJob(repos: RepoInfo[]) {
const TASK_DEFINITION = process.env.TASK_DEFINITION;
const CONTAINER_NAME = process.env.CONTAINER_NAME;
const CLUSTER = process.env.CLUSTER;

if (!TASK_DEFINITION) throw new Error('ERROR! process.env.TASK_DEFINITION is not defined');
if (!CONTAINER_NAME) throw new Error('ERROR! process.env.CONTAINER_NAME is not defined');
if (!CLUSTER) throw new Error('ERROR! process.env.CLUSTER is not defined');

const client = new ECSClient({
Expand All @@ -42,6 +44,7 @@ async function runCacheRebuildJob(repos: RepoInfo[]) {
overrides: {
containerOverrides: [
{
name: CONTAINER_NAME,
environment: [
{
name: 'REPOS',
Expand Down Expand Up @@ -83,8 +86,17 @@ export async function rebuildCacheHandler(event: APIGatewayEvent): Promise<APIGa
};
}

return {
statusCode: 200,
body: 'ok',
};
try {
await runCacheRebuildJob(rebuildRequest);
return {
statusCode: 200,
body: 'Cache rebuild job successfully created',
};
} catch (e) {
console.error(e);
return {
statusCode: 500,
body: 'Error occurred when starting cache rebuild job',
};
}
}
2 changes: 1 addition & 1 deletion cdk-infra/bin/cdk-infra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function main() {
env,
});

new CacheUpdaterStack(app, 'cache-updater', { vpc });
new CacheUpdaterStack(app, `${stackName}-cache`, { vpc });
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Duration } from 'aws-cdk-lib';
import { Cors, LambdaIntegration, LambdaRestApi } from 'aws-cdk-lib/aws-apigateway';
import { TaskDefinition } from 'aws-cdk-lib/aws-ecs';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Construct } from 'constructs';
import path from 'path';

interface CacheUpdaterApiConstructProps {
clusterName: string;
taskDefinition: TaskDefinition;
containerName: string;
}

const HANDLERS_PATH = path.join(__dirname, '/../../../../api/controllers/v2');

export class CacheUpdaterApiConstruct extends Construct {
constructor(
scope: Construct,
id: string,
{ clusterName, taskDefinition, containerName }: CacheUpdaterApiConstructProps
) {
super(scope, id);

const cacheWebhookLambda = new NodejsFunction(this, 'cacheUpdaterWebhookLambda', {
entry: `${HANDLERS_PATH}/cache.ts`,
handler: 'rebuildCacheHandler',
runtime: Runtime.NODEJS_18_X,
timeout: Duration.minutes(2),
memorySize: 1024,
environment: {
CLUSTER: clusterName,
TASK_DEFINITION: taskDefinition.taskDefinitionArn,
CONTAINER_NAME: containerName,
},
});

taskDefinition.grantRun(cacheWebhookLambda);

// generic handler for the root endpoint
const rootEndpointLambda = new Function(this, 'RootEndpointLambda', {
code: Code.fromInline('exports.default = (event) => { console.log("hello, world!!"); }'),
runtime: Runtime.NODEJS_18_X,
handler: 'RootEndpointLambda',
});

const restApi = new LambdaRestApi(this, 'cacheUpdaterRestApi', { handler: rootEndpointLambda, proxy: false });

restApi.root
.addResource('webhook', {
defaultCorsPreflightOptions: { allowOrigins: Cors.ALL_ORIGINS },
})
.addMethod('POST', new LambdaIntegration(cacheWebhookLambda));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IVpc } from 'aws-cdk-lib/aws-ec2';
import { Cluster, ContainerImage, FargateTaskDefinition, LogDrivers } from 'aws-cdk-lib/aws-ecs';
import { Cluster, ContainerImage, FargateTaskDefinition, LogDrivers, TaskDefinition } from 'aws-cdk-lib/aws-ecs';
import { Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { LogGroup } from 'aws-cdk-lib/aws-logs';
import { Bucket } from 'aws-cdk-lib/aws-s3';
Expand All @@ -13,7 +13,9 @@ interface CacheUpdaterWorkerConstructProps {
}

export class CacheUpdaterWorkerConstruct extends Construct {
clusterName: string;
readonly clusterName: string;
readonly taskDefinition: TaskDefinition;
readonly containerName: string;

constructor(scope: Construct, id: string, { vpc }: CacheUpdaterWorkerConstructProps) {
super(scope, id);
Expand All @@ -36,12 +38,12 @@ export class CacheUpdaterWorkerConstruct extends Construct {
taskRole,
});

const containerName = 'cacheUpdaterWorkerImage';
const taskDefLogGroup = new LogGroup(this, 'cacheUpdaterWorkerLogGroup');

taskDefinition.addContainer('cacheUpdaterWorkerImage', {
image: ContainerImage.fromAsset(path.join(__dirname, '../../../../'), {
file: 'src/cache-updater/Dockerfile.cacheUpdater',
buildArgs: { SNOOTY_PARSER_VERSION: '0.15.2' },
buildArgs: { SNOOTY_PARSER_VERSION: '0.15.2' }, // TODO: Update this to use a context variable
exclude: ['tests/', 'node_modules/', 'cdk-infra/'], // adding this just in case it doesn't pick up our dockerignore
}),
environment: {
Expand All @@ -54,5 +56,7 @@ export class CacheUpdaterWorkerConstruct extends Construct {
});

this.clusterName = cluster.clusterName;
this.taskDefinition = taskDefinition;
this.containerName = containerName;
}
}
13 changes: 12 additions & 1 deletion cdk-infra/lib/stacks/cache-updater-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { CacheUpdaterWorkerConstruct } from '../constructs/cache-updater/cache-updater-worker-construct';
import { IVpc } from 'aws-cdk-lib/aws-ec2';
import { CacheUpdaterApiConstruct } from '../constructs/cache-updater/cache-updater-api-construct';

interface CacheUpdaterStackProps {
vpc: IVpc;
Expand All @@ -10,6 +11,16 @@ export class CacheUpdaterStack extends Stack {
constructor(scope: Construct, id: string, { vpc }: CacheUpdaterStackProps) {
super(scope, id);

new CacheUpdaterWorkerConstruct(this, 'cache-updater-resources', { vpc });
const { clusterName, taskDefinition, containerName } = new CacheUpdaterWorkerConstruct(
this,
'cache-updater-resources',
{ vpc }
);

new CacheUpdaterApiConstruct(this, 'cache-updater-api', {
clusterName,
taskDefinition,
containerName,
});
}
}

0 comments on commit 7fa3cbc

Please sign in to comment.