Skip to content

Commit

Permalink
Exposed new polling system to invoke, added process duration timer
Browse files Browse the repository at this point in the history
  • Loading branch information
azdak committed Nov 25, 2024
1 parent 57f4747 commit f93c199
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/internal/pollOutstandingScans.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { chunk, db, isStaging, sleep, hashStringToUuid } from '#src/utils';
import { ingestScanData } from './ingestScanData';

const MAX_RUNNING_DURATION = 1000*58; // run for 58 seconds

export const pollOutstandingScans = async ({ request, reply }) => {
console.log(`START POLLING OPEN SCANS`);
const startTime = new Date().getTime();
Expand All @@ -16,6 +19,9 @@ export const pollOutstandingScans = async ({ request, reply }) => {
for(const job of sortedJobIds){
// check scan for result
try{
const currentTime = new Date().getTime();
if(currentTime-startTime > MAX_RUNNING_DURATION) break; // end the process after MAX_RUNNING_TIME

const scanResults = await fetch(`https://scan${isStaging ? '-dev' : ''}.equalify.app/results/${job.job_id}`, { signal: AbortSignal.timeout(10000) });
const { result, status } = await scanResults.json();

Expand Down
5 changes: 4 additions & 1 deletion src/scheduled.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { runEveryFifteenMinutes, runEveryMinute } from '#src/scheduled/index';
import { pollEveryMinute, runEveryFifteenMinutes, runEveryMinute } from '#src/scheduled/index';

export const scheduled = async (event) => {
if (event.path.endsWith('/runEveryMinute')) {
Expand All @@ -7,4 +7,7 @@ export const scheduled = async (event) => {
else if (event.path.endsWith('/runEveryFifteenMinutes')) {
return runEveryFifteenMinutes();
}
else if (event.path.endsWith('/pollEveryMinute')) {
return pollEveryMinute();
}
}
3 changes: 2 additions & 1 deletion src/scheduled/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './runEveryMinute'
export * from './runEveryFifteenMinutes'
export * from './runEveryFifteenMinutes'
export * from './pollEveryMinute'
15 changes: 15 additions & 0 deletions src/scheduled/pollEveryMinute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { db, isStaging } from '#src/utils';
import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda';
const lambda = new LambdaClient();

export const pollEveryMinute = async () => {

lambda.send(new InvokeCommand({
FunctionName: `equalify-api${isStaging ? '-staging' : ''}`,
InvocationType: "Event",
Payload: Buffer.from(JSON.stringify({
path: '/internal/pollOutstandingScans'
})),
}));
return;
}

0 comments on commit f93c199

Please sign in to comment.