···66} from "@openstatus/db/src/schema";
7788import type { MonitorFlyRegion } from "@openstatus/db/src/schema/constants";
99+import { Redis } from "@openstatus/upstash";
910import { checkerAudit } from "../utils/audit-log";
1011import { providerToFunction } from "./utils";
1212+1313+const redis = Redis.fromEnv();
11141215export const triggerNotifications = async ({
1316 monitorId,
···3740 .where(eq(schema.monitor.id, Number(monitorId)))
3841 .all();
3942 for (const notif of notifications) {
4343+ const key = `${monitorId}:${incidentId}:${notif.notification.provider}:${notifType}`;
4444+ const r = await redis.setnx(key, "1");
4545+ if (r === 0) {
4646+ console.log(`๐ค notification already sent for ${key}`);
4747+ continue;
4848+ }
4949+5050+ await redis.expire(key, 60 * 60);
5151+4052 console.log(
4153 `๐ sending notification for ${monitorId} and chanel ${notif.notification.provider} for ${notifType}`,
4254 );
+7-6
apps/server/src/checker/index.ts
···8787 // We add the new region to the set
8888 await redis.sadd(redisKey, region);
8989 // let's add an expire to the set
9090- await redis.expire(redisKey, 60 * 60 * 24);
9190 // We get the number of regions affected
9291 const nbAffectedRegion = await redis.scard(redisKey);
9292+ await redis.expire(redisKey, 60 * 60 * 24);
93939494 const monitor = selectMonitorSchema.parse(currentMonitor);
95959696 const numberOfRegions = monitor.regions.length;
97979898- if (nbAffectedRegion > numberOfRegions / 2) {
9898+ if (nbAffectedRegion >= numberOfRegions / 2) {
9999 await triggerNotifications({
100100 monitorId,
101101 statusCode,
···126126 // We add the new region to the set
127127 await redis.sadd(redisKey, region);
128128 // let's add an expire to the set
129129- await redis.expire(redisKey, 60 * 60 * 24);
130129 // We get the number of regions affected
131130 const nbAffectedRegion = await redis.scard(redisKey);
131131+ await redis.expire(redisKey, 60 * 60 * 24);
132132133133 const currentMonitor = await db
134134 .select()
···145145 );
146146 // If the number of affected regions is greater than half of the total region, we trigger the alerting
147147 // 4 of 6 monitor need to fail to trigger an alerting
148148- if (nbAffectedRegion > numberOfRegions / 2) {
148148+ if (nbAffectedRegion >= numberOfRegions / 2) {
149149 // let's refetch the incident to avoid race condition
150150 const incident = await db
151151 .select()
···218218 // // We add the new region to the set
219219 await redis.sadd(redisKey, region);
220220 // // let's add an expire to the set
221221- await redis.expire(redisKey, 60 * 60 * 24);
222221 // // We get the number of regions affected
223222 const nbAffectedRegion = await redis.scard(redisKey);
223223+ await redis.expire(redisKey, 60 * 60 * 24);
224224225225 const currentMonitor = await db
226226 .select()
···237237 );
238238 // // If the number of affected regions is greater than half of the total region, we trigger the alerting
239239 // // 4 of 6 monitor need to fail to trigger an alerting
240240- if (nbAffectedRegion > numberOfRegions / 2) {
240240+ if (nbAffectedRegion >= numberOfRegions / 2) {
241241 const incident = await db
242242 .select()
243243 .from(incidentTable)
···259259 })
260260 .where(eq(incidentTable.id, incident.id))
261261 .run();
262262+262263 await triggerNotifications({
263264 monitorId,
264265 statusCode,