Openstatus www.openstatus.dev

๐Ÿ› Bug notifications (#983)

* ๐Ÿ› notifications

* ๐Ÿ› notifications

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

authored by

Thibault Le Ouay
autofix-ci[bot]
and committed by
GitHub
6e01156a f17eb2da

+19 -6
+12
apps/server/src/checker/alerting.ts
··· 6 6 } from "@openstatus/db/src/schema"; 7 7 8 8 import type { MonitorFlyRegion } from "@openstatus/db/src/schema/constants"; 9 + import { Redis } from "@openstatus/upstash"; 9 10 import { checkerAudit } from "../utils/audit-log"; 10 11 import { providerToFunction } from "./utils"; 12 + 13 + const redis = Redis.fromEnv(); 11 14 12 15 export const triggerNotifications = async ({ 13 16 monitorId, ··· 37 40 .where(eq(schema.monitor.id, Number(monitorId))) 38 41 .all(); 39 42 for (const notif of notifications) { 43 + const key = `${monitorId}:${incidentId}:${notif.notification.provider}:${notifType}`; 44 + const r = await redis.setnx(key, "1"); 45 + if (r === 0) { 46 + console.log(`๐Ÿค” notification already sent for ${key}`); 47 + continue; 48 + } 49 + 50 + await redis.expire(key, 60 * 60); 51 + 40 52 console.log( 41 53 `๐Ÿ’Œ sending notification for ${monitorId} and chanel ${notif.notification.provider} for ${notifType}`, 42 54 );
+7 -6
apps/server/src/checker/index.ts
··· 87 87 // We add the new region to the set 88 88 await redis.sadd(redisKey, region); 89 89 // let's add an expire to the set 90 - await redis.expire(redisKey, 60 * 60 * 24); 91 90 // We get the number of regions affected 92 91 const nbAffectedRegion = await redis.scard(redisKey); 92 + await redis.expire(redisKey, 60 * 60 * 24); 93 93 94 94 const monitor = selectMonitorSchema.parse(currentMonitor); 95 95 96 96 const numberOfRegions = monitor.regions.length; 97 97 98 - if (nbAffectedRegion > numberOfRegions / 2) { 98 + if (nbAffectedRegion >= numberOfRegions / 2) { 99 99 await triggerNotifications({ 100 100 monitorId, 101 101 statusCode, ··· 126 126 // We add the new region to the set 127 127 await redis.sadd(redisKey, region); 128 128 // let's add an expire to the set 129 - await redis.expire(redisKey, 60 * 60 * 24); 130 129 // We get the number of regions affected 131 130 const nbAffectedRegion = await redis.scard(redisKey); 131 + await redis.expire(redisKey, 60 * 60 * 24); 132 132 133 133 const currentMonitor = await db 134 134 .select() ··· 145 145 ); 146 146 // If the number of affected regions is greater than half of the total region, we trigger the alerting 147 147 // 4 of 6 monitor need to fail to trigger an alerting 148 - if (nbAffectedRegion > numberOfRegions / 2) { 148 + if (nbAffectedRegion >= numberOfRegions / 2) { 149 149 // let's refetch the incident to avoid race condition 150 150 const incident = await db 151 151 .select() ··· 218 218 // // We add the new region to the set 219 219 await redis.sadd(redisKey, region); 220 220 // // let's add an expire to the set 221 - await redis.expire(redisKey, 60 * 60 * 24); 222 221 // // We get the number of regions affected 223 222 const nbAffectedRegion = await redis.scard(redisKey); 223 + await redis.expire(redisKey, 60 * 60 * 24); 224 224 225 225 const currentMonitor = await db 226 226 .select() ··· 237 237 ); 238 238 // // If the number of affected regions is greater than half of the total region, we trigger the alerting 239 239 // // 4 of 6 monitor need to fail to trigger an alerting 240 - if (nbAffectedRegion > numberOfRegions / 2) { 240 + if (nbAffectedRegion >= numberOfRegions / 2) { 241 241 const incident = await db 242 242 .select() 243 243 .from(incidentTable) ··· 259 259 }) 260 260 .where(eq(incidentTable.id, incident.id)) 261 261 .run(); 262 + 262 263 await triggerNotifications({ 263 264 monitorId, 264 265 statusCode,