Skip to content

Commit

Permalink
add limit to geo-event-fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
sagararyal committed Jul 2, 2023
1 parent c5c6204 commit de0d9d5
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions apps/server/src/pages/api/cron/geo-event-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,49 @@ export default async function alertFetcher(req: NextApiRequest, res: NextApiResp
}
}

// get all active providers where now fetch frequenncy + last run is greater than current time
// Set Limit to 7 if not provided or greater than 10
// Extract limit from query
const rawLimit = req.query['limit'];

// Initialize final limit as number
let limit: number;

if (typeof rawLimit === 'string') {
const parsedLimit = parseInt(rawLimit, 10);
if (isNaN(parsedLimit) || parsedLimit > 15) {
limit = 15;
} else {
limit = parsedLimit;
}
} else {
limit = 4;
}



// get all active providers where now fetch frequency + last run is greater than current time


let newSiteAlertCount = 0;
let processedProviders = 0;

while (processedProviders <= 7) {
// while (processedProviders <= limit) {
const activeProviders: GeoEventProvider[] = await prisma.$queryRaw`
SELECT *
FROM "GeoEventProvider"
WHERE "isActive" = true
AND "fetchFrequency" IS NOT NULL
AND ("lastRun" + ("fetchFrequency" || ' minutes')::INTERVAL) < (current_timestamp AT TIME ZONE 'UTC')
LIMIT 4;
LIMIT ${limit};
`;
// Filter out those active providers whose last (run date + fetchFrequency (in minutes) > current time
// Break the loop if there are no active providers
if (activeProviders.length === 0) {
logger(`Nothing to process anymore activeProviders.length = 0`, "info");
break;
}

// if (activeProviders.length === 0) {
// logger(`Nothing to process anymore activeProviders.length = 0`, "info");
// break;
// }

logger(`Running Geo Event Fetcher. Taking ${activeProviders.length} eligible providers.`, "info");

Expand Down Expand Up @@ -78,12 +100,12 @@ export default async function alertFetcher(req: NextApiRequest, res: NextApiResp
// This helps in creating SiteAlerts for unprocessed geoEvents from past runs, if fetch fails for some reason

// and then create site Alerts

//if (eventCount > 0) {
const alertCount = await createSiteAlerts(geoEventProviderId, geoEventProviderClientId as GeoEventProviderClientId, slice)
logger(`${breadcrumbPrefix} Created ${alertCount} Site Alerts.`, "info");
const alertCount = await createSiteAlerts(geoEventProviderId, geoEventProviderClientId as GeoEventProviderClientId, slice)
logger(`${breadcrumbPrefix} Created ${alertCount} Site Alerts.`, "info");

newSiteAlertCount += alertCount
newSiteAlertCount += alertCount
// }

// Update lastRun value of the provider to the current Date()
Expand All @@ -101,12 +123,12 @@ export default async function alertFetcher(req: NextApiRequest, res: NextApiResp
processedProviders += activeProviders.length;

await Promise.all(promises).catch(error => logger(`Something went wrong before creating notifications. ${error}`, "error"));
}
//} // end of while loop

// let notificationCount;
//if (newSiteAlertCount > 0) {
const notificationCount = await createNotifications();
logger(`Added ${notificationCount} notifications for ${newSiteAlertCount} alerts`, "info");
const notificationCount = await createNotifications();
logger(`Added ${notificationCount} notifications for ${newSiteAlertCount} alerts`, "info");
// }
// else {
// logger(`All done. ${newSiteAlertCount} Alerts. No new notifications. Waving Goodbye!`, "info");
Expand Down

1 comment on commit de0d9d5

@vercel
Copy link

@vercel vercel bot commented on de0d9d5 Jul 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.