2024-11-07 20:57:33 +01:00
import { logger } from "../../../src/lib/logger" ;
2024-08-13 21:03:24 +02:00
import { getScrapeQueue } from "../queue-service" ;
2024-07-12 19:07:59 -04:00
import { sendSlackWebhook } from "./slack" ;
2024-07-12 19:49:38 -04:00
export async function checkAlerts() {
2024-07-12 19:12:56 -04:00
try {
if (
process . env . SLACK_WEBHOOK_URL &&
process . env . ENV === "production" &&
process . env . ALERT_NUM_ACTIVE_JOBS &&
process . env . ALERT_NUM_WAITING_JOBS
) {
2024-11-07 20:57:33 +01:00
logger . info ( "Initializing alerts" ) ;
2024-07-12 19:12:56 -04:00
const checkActiveJobs = async ( ) = > {
try {
2024-08-13 21:03:24 +02:00
const scrapeQueue = getScrapeQueue ( ) ;
const activeJobs = await scrapeQueue . getActiveCount ( ) ;
2024-07-12 19:12:56 -04:00
if ( activeJobs > Number ( process . env . ALERT_NUM_ACTIVE_JOBS ) ) {
2024-11-07 20:57:33 +01:00
logger . warn (
2024-12-11 19:51:08 -03:00
` Alert: Number of active jobs is over ${ process . env . ALERT_NUM_ACTIVE_JOBS } . Current active jobs: ${ activeJobs } . ` ,
2024-07-12 19:12:56 -04:00
) ;
sendSlackWebhook (
` Alert: Number of active jobs is over ${ process . env . ALERT_NUM_ACTIVE_JOBS } . Current active jobs: ${ activeJobs } ` ,
2024-12-11 19:51:08 -03:00
true ,
2024-07-12 19:12:56 -04:00
) ;
} else {
2024-11-07 20:57:33 +01:00
logger . info (
2024-12-11 19:51:08 -03:00
` Number of active jobs is under ${ process . env . ALERT_NUM_ACTIVE_JOBS } . Current active jobs: ${ activeJobs } ` ,
2024-07-12 19:12:56 -04:00
) ;
}
} catch ( error ) {
2024-11-07 20:57:33 +01:00
logger . error ( ` Failed to check active jobs: ${ error } ` ) ;
2024-07-12 19:12:56 -04:00
}
} ;
const checkWaitingQueue = async ( ) = > {
2024-08-13 21:03:24 +02:00
const scrapeQueue = getScrapeQueue ( ) ;
const waitingJobs = await scrapeQueue . getWaitingCount ( ) ;
2024-07-12 19:57:12 -04:00
2024-07-30 13:27:23 -04:00
if ( waitingJobs > Number ( process . env . ALERT_NUM_WAITING_JOBS ) ) {
2024-11-07 20:57:33 +01:00
logger . warn (
2024-12-11 19:51:08 -03:00
` Alert: Number of waiting jobs is over ${ process . env . ALERT_NUM_WAITING_JOBS } . Current waiting jobs: ${ waitingJobs } . ` ,
2024-07-12 19:07:59 -04:00
) ;
sendSlackWebhook (
2024-07-12 19:12:56 -04:00
` Alert: Number of waiting jobs is over ${ process . env . ALERT_NUM_WAITING_JOBS } . Current waiting jobs: ${ waitingJobs } . Scale up the number of workers with fly scale count worker=20 ` ,
2024-12-11 19:51:08 -03:00
true ,
2024-07-12 19:07:59 -04:00
) ;
}
2024-07-12 19:12:56 -04:00
} ;
2024-07-12 19:07:59 -04:00
2024-07-12 19:12:56 -04:00
const checkAll = async ( ) = > {
2024-08-16 22:17:38 -04:00
await checkActiveJobs ( ) ;
2024-07-12 19:12:56 -04:00
await checkWaitingQueue ( ) ;
} ;
2024-07-12 19:07:59 -04:00
2024-07-12 19:49:38 -04:00
await checkAll ( ) ;
2024-12-11 19:46:11 -03:00
// setInterval(checkAll, 10000); // Run every
2024-07-12 19:12:56 -04:00
}
} catch ( error ) {
2024-11-07 20:57:33 +01:00
logger . error ( ` Failed to initialize alerts: ${ error } ` ) ;
2024-07-12 19:07:59 -04:00
}
}