Skip to content

Commit

Permalink
Starting scans, storing jobId, polling for finished jobs w/ processScans
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Jun 9, 2024
1 parent 4920e85 commit 39e9108
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import awsLambdaFastify from "@fastify/aws-lambda";
import { fastify } from "./app.js";
import { cognito } from './cognito.js';
import { scheduled } from './scheduled.js';
const proxy = awsLambdaFastify(fastify);

export async function handler(event: any, context: any) {
if (event.triggerSource) {
return await cognito(event);
}
else if (event.internalPath?.startsWith('/scheduled')) {
return await scheduled(event);
}
return await proxy(event, context);
}
16 changes: 12 additions & 4 deletions src/routes/addScans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ export const addScans = async ({ request, reply }) => {
}
}
else {
console.log(JSON.stringify({ url }));
const scanResponse = await (await fetch(`https://scan.equalify.app/generate/url`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url })
})).json();
console.log(JSON.stringify({ scanResponse }));
await pgClient.query(`
INSERT INTO "scans" ("user_id", "property_id", "url_id") VALUES ($1, $2, $3) RETURNING "id"
`, [jwtClaims.sub, propertyId, id]);
INSERT INTO "scans" ("user_id", "property_id", "url_id", "job_id") VALUES ($1, $2, $3, $4) RETURNING "id"
`, [jwtClaims.sub, propertyId, id, parseInt(scanResponse?.jobID)]);
}
}
}
Expand All @@ -49,9 +56,10 @@ export const addScans = async ({ request, reply }) => {
}
}
else {
const jobId = (await (await fetch(`https://scan.equalify.app/generate/url`, { method: 'POST', body: JSON.stringify({ url }) })).json())?.jobID;
await pgClient.query(`
INSERT INTO "scans" ("user_id", "url_id") VALUES ($1, $2) RETURNING "id"
`, [jwtClaims.sub, urlId]);
INSERT INTO "scans" ("user_id", "url_id", "job_id") VALUES ($1, $2, $3) RETURNING "id"
`, [jwtClaims.sub, urlId, jobId]);
}
}
await pgClient.clean();
Expand Down
4 changes: 4 additions & 0 deletions src/routes/getScans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const getScans = async ({ request, reply }) => {
) {
nodes {
id
createdAt
processing
jobId
results
}
totalCount
}
Expand Down
7 changes: 7 additions & 0 deletions src/scheduled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { processScans } from './scheduled/index.js';

export const scheduled = async (event) => {
if (event.internalPath.endsWith('/processScans')) {
return processScans();
}
}
1 change: 1 addition & 0 deletions src/scheduled/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './processScans'
14 changes: 14 additions & 0 deletions src/scheduled/processScans.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { pgClient } from 'utils';

export const processScans = async () => {
await pgClient.connect();
const scans = (await pgClient.query(`SELECT "id", "job_id" FROM "scans" WHERE "processing"=TRUE ORDER BY "created_at" DESC`)).rows;
for (const scan of scans) {
const response = await (await fetch(`https://scan.equalify.app/results/${scan.job_id}`)).json();
if (response?.status === 'completed') {
await pgClient.query(`UPDATE "scans" SET "processing"=FALSE, "results"=$1`, [response?.result]);
}
}
await pgClient.clean();
return;
}

0 comments on commit 39e9108

Please sign in to comment.