Update index.ts

This commit is contained in:
Nicolas 2024-07-12 19:12:56 -04:00
parent fd18f2269b
commit fc3328f3d1

View File

@ -2,54 +2,58 @@ import { getWebScraperQueue } from "../queue-service";
import { sendSlackWebhook } from "./slack"; import { sendSlackWebhook } from "./slack";
export function initAlerts() { export function initAlerts() {
if ( try {
process.env.SLACK_WEBHOOK_URL && if (
process.env.ENV === "production" && process.env.SLACK_WEBHOOK_URL &&
process.env.ALERT_NUM_ACTIVE_JOBS && process.env.ENV === "production" &&
process.env.ALERT_NUM_WAITING_JOBS process.env.ALERT_NUM_ACTIVE_JOBS &&
) { process.env.ALERT_NUM_WAITING_JOBS
console.info("Initializing alerts"); ) {
const checkActiveJobs = async () => { console.info("Initializing alerts");
try { const checkActiveJobs = async () => {
try {
const webScraperQueue = getWebScraperQueue();
const activeJobs = await webScraperQueue.getActiveCount();
if (activeJobs > Number(process.env.ALERT_NUM_ACTIVE_JOBS)) {
console.warn(
`Alert: Number of active jobs is over ${process.env.ALERT_NUM_ACTIVE_JOBS}. Current active jobs: ${activeJobs}.`
);
sendSlackWebhook(
`Alert: Number of active jobs is over ${process.env.ALERT_NUM_ACTIVE_JOBS}. Current active jobs: ${activeJobs}`,
true
);
} else {
console.info(
`Number of active jobs is under ${process.env.ALERT_NUM_ACTIVE_JOBS}. Current active jobs: ${activeJobs}`
);
}
} catch (error) {
console.error("Failed to check active jobs:", error);
}
};
const checkWaitingQueue = async () => {
const webScraperQueue = getWebScraperQueue(); const webScraperQueue = getWebScraperQueue();
const activeJobs = await webScraperQueue.getActiveCount(); const waitingJobs = await webScraperQueue.getWaitingCount();
if (activeJobs > Number(process.env.ALERT_NUM_ACTIVE_JOBS)) { if (waitingJobs > Number(process.env.ALERT_NUM_WAITING_JOBS)) {
console.warn( console.warn(
`Alert: Number of active jobs is over ${process.env.ALERT_NUM_ACTIVE_JOBS}. Current active jobs: ${activeJobs}.` `Alert: Number of waiting jobs is over ${process.env.ALERT_NUM_WAITING_JOBS}. Current waiting jobs: ${waitingJobs}.`
); );
sendSlackWebhook( sendSlackWebhook(
`Alert: Number of active jobs is over ${process.env.ALERT_NUM_ACTIVE_JOBS}. Current active jobs: ${activeJobs}`, `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`,
true true
); );
} else {
console.info(
`Number of active jobs is under ${process.env.ALERT_NUM_ACTIVE_JOBS}. Current active jobs: ${activeJobs}`
);
} }
} catch (error) { };
console.error("Failed to check active jobs:", error);
}
};
const checkWaitingQueue = async () => { const checkAll = async () => {
const webScraperQueue = getWebScraperQueue(); await checkActiveJobs();
const waitingJobs = await webScraperQueue.getWaitingCount(); await checkWaitingQueue();
if (waitingJobs > Number(process.env.ALERT_NUM_WAITING_JOBS)) { };
console.warn(
`Alert: Number of waiting jobs is over ${process.env.ALERT_NUM_WAITING_JOBS}. Current waiting jobs: ${waitingJobs}.`
);
sendSlackWebhook(
`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`,
true
);
}
};
const checkAll = async () => { setInterval(checkAll, 5 * 60 * 1000); // Run every 5 minutes
await checkActiveJobs(); }
await checkWaitingQueue(); } catch (error) {
}; console.error("Failed to initialize alerts:", error);
setInterval(checkAll, 5 * 60 * 1000); // Run every 5 minutes
} }
} }