Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
[Feat] Server health check + slack message
rafaelsideguidea680c7ce8498
diff --git a/apps/api/.env.example b/apps/api/.env.exampleindex 34e24b1fb..3cd40c1ee 100644--- a/apps/api/.env.example+++ b/apps/api/.env.example@@ -21,4 +21,5 @@ OPENAI_API_KEY= # add for LLM dependednt features (image alt generation, etc.)BULL_AUTH_KEY= #LOGTAIL_KEY= # Use if you're configuring basic logging with logtailPLAYWRIGHT_MICROSERVICE_URL= # set if you'd like to run a playwright fallback-LLAMAPARSE_API_KEY= #Set if you have a llamaparse key you'd like to use to parse pdfs\ No newline at end of file+LLAMAPARSE_API_KEY= #Set if you have a llamaparse key you'd like to use to parse pdfs+SLACK_WEBHOOK_URL= # set if you'd like to send slack server health status messages\ No newline at end of filediff --git a/apps/api/requests.http b/apps/api/requests.httpindex 23501369f..751ba5eed 100644--- a/apps/api/requests.http+++ b/apps/api/requests.http@@ -49,4 +49,13 @@ content-type: application/json### Check Job StatusGET https://api.firecrawl.dev/v0/crawl/status/cfcb71ac-23a3-4da5-bd85-d4e58b871d66-Authorization: Bearer\ No newline at end of file+Authorization: Bearer++### Get Active Jobs Count+GET http://localhost:3002/serverHealthCheck+content-type: application/json++### Notify Server Health Check+GET http://localhost:3002/serverHealthCheck/notify+content-type: application/json+diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex a2e5c517e..6417f3602 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -87,6 +87,76 @@ app.get(`/admin/${process.env.BULL_AUTH_KEY}/queues`, async (req, res) => {}});+app.get(`/serverHealthCheck`, async (req, res) => {+ try {+ const webScraperQueue = getWebScraperQueue();+ const [activeJobs] = await Promise.all([+ webScraperQueue.getActiveCount(),+ ]);++ const noActiveJobs = activeJobs === 0;+ // 200 if no active jobs, 503 if there are active jobs+ return res.status(noActiveJobs ? 200 : 500).json({+ activeJobs,+ });+ } catch (error) {+ console.error(error);+ return res.status(500).json({ error: error.message });+ }+});++app.get('/serverHealthCheck/notify', async (req, res) => {+ if (process.env.SLACK_WEBHOOK_URL) {+ const treshold = 5; // The treshold value for the active jobs+ const timeout = 60000; // 1 minute // The timeout value for the check in milliseconds++ const getActiveJobs = async () => {+ const webScraperQueue = getWebScraperQueue();+ const [activeJobs] = await Promise.all([+ webScraperQueue.getActiveCount(),+ ]);++ return activeJobs;+ };++ res.status(200).json({ message: "Check initiated" });++ const checkActiveJobs = async () => {+ try {+ let activeJobs = await getActiveJobs();+ if (activeJobs >= treshold) {+ setTimeout(async () => {+ activeJobs = await getActiveJobs(); // Re-check the active jobs count+ if (activeJobs >= treshold) {+ const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;+ const message = {+ text: `⚠️ Warning: The number of active jobs (${activeJobs}) has exceeded the threshold (${treshold}) for more than ${timeout/60000} minute(s).`,+ };++ const response = await fetch(slackWebhookUrl, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ },+ body: JSON.stringify(message),+ })++ if (!response.ok) {+ console.error('Failed to send Slack notification')+ }+ }+ }, timeout);+ }+ } catch (error) {+ console.error(error);+ }+ };++ checkActiveJobs();+ }+});++app.get("/is-production", (req, res) => {res.send({ isProduction: global.isProduction });});