Skip to content

Commit f8df930

Browse files
committed
fix: fail non-200 codes
1 parent 63df6af commit f8df930

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

env.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ import * as dotenv from "dotenv";
33

44
dotenv.config(); // loads in .env file if present
55
console.log(process.env);
6+
export interface ISiteTrackingInfo {
7+
url: string;
8+
doNotPing: boolean;
9+
}
610
const envSchema = z.object({
711
SLACK_APP_TOKEN: z.string(),
812
SLACK_BOT_TOKEN: z.string(),
913
CMUEATS_CHANNEL_ID: z.string(),
10-
MONITORED_URLS: z.string().transform((str) => str.split(",")),
14+
MONITORED_URLS: z.string().transform((str) =>
15+
str.split(",").map((site) => {
16+
const [url, ignore] = site.split("|");
17+
if (ignore !== undefined) {
18+
console.log("Not pinging", url);
19+
}
20+
return {
21+
url,
22+
doNotPing: ignore !== undefined,
23+
} satisfies ISiteTrackingInfo;
24+
})
25+
),
1126
/** Number of ms between pings on the same site */
1227
MONITOR_INTERVAL_MS: z.coerce.number().default(10000),
1328
/** Timeout for a single ping */

intervals/siteMonitor.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DateTime } from "luxon";
2-
import { env } from "../env";
2+
import { env, ISiteTrackingInfo } from "../env";
33

44
interface SiteData {
55
downState?: {
@@ -12,22 +12,24 @@ interface SiteData {
1212
}
1313
export class SiteMonitor {
1414
siteStatus: Record<string, SiteData> = {};
15+
doNotPing: Record<string, boolean> = {};
1516
sendMessage: (msg: string) => void;
1617
startupTime = DateTime.local({ zone: "America/New_York" });
1718
alertThresholdMs: number;
1819
pingThresholdMs: number;
1920

2021
constructor(
21-
sites: string[],
22+
sites: ISiteTrackingInfo[],
2223
sendMessage: (msg: string) => void,
2324
alertThresholdMs: number,
2425
pingThresholdMs: number
2526
) {
26-
for (const siteUrl of sites) {
27-
this.siteStatus[siteUrl] = {
27+
for (const site of sites) {
28+
this.siteStatus[site.url] = {
2829
successfulFetchCount: 0,
2930
failedFetchCount: 0,
3031
};
32+
this.doNotPing[site.url] = site.doNotPing;
3133
}
3234
this.sendMessage = sendMessage;
3335
this.alertThresholdMs = alertThresholdMs;
@@ -60,13 +62,14 @@ export class SiteMonitor {
6062
}
6163
if (
6264
+new Date() - downState.firstDownTimestamp >= this.pingThresholdMs &&
63-
downState.alertStage === "WARNED"
65+
downState.alertStage === "WARNED" &&
66+
!this.doNotPing[siteUrl]
6467
) {
6568
downState.alertStage = "PINGED";
6669
this.sendMessage(
6770
`<!channel>! ${siteUrl} has been down for the past ${
6871
this.pingThresholdMs / 1000
69-
} seconds with the following errors: ${downState.failErrors.join(", ")}`
72+
} seconds with ${downState.failErrors.length} errors`
7073
);
7174
}
7275
}

intervals/uptimeChecker.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const checkSite = (
1010
) => {
1111
console.log(`Running check for ${url}`);
1212
fetch(url, { signal: AbortSignal.timeout(env.URL_TIMEOUT_MS) })
13-
.then(onSuccess)
13+
.then((res) => {
14+
if (res.status !== 200) onError(res);
15+
else onSuccess(res);
16+
})
1417
.catch(onError);
1518
};
1619

@@ -26,14 +29,14 @@ export const setUpUptimeChecker = (
2629
setInterval(() => {
2730
env.MONITORED_URLS.forEach((site) =>
2831
checkSite(
29-
site,
32+
site.url,
3033
(error) => {
31-
console.log(`check failed for ${site}`, error);
32-
siteMonitor?.siteDown(site, error);
34+
console.log(`check failed for ${site.url}`, error);
35+
siteMonitor?.siteDown(site.url, error);
3336
},
34-
() => {
35-
console.log(`check successful for ${site}`);
36-
siteMonitor?.siteUp(site);
37+
(res) => {
38+
console.log(`check successful for ${site.url}`);
39+
siteMonitor?.siteUp(site.url);
3740
}
3841
)
3942
);

0 commit comments

Comments
 (0)