Public
Cron
Readme

MAD BOTS (Multi-functional Alerting & Diagnostics Bot Operations Tracking System)

This val runs every two minutes and makes sure a supabase realtime websocket listener is still alive.

  • on spawn, checks to see if acknowledged_by_server == true
    • if not, send a message to discord
  • update the row (id=1) in supabase with acknowledged_by_server: false to reset for next run
    • this will be responded to by the supabase handler. code snippet below:
    .on(
      "postgres_changes",
      {
        event: "UPDATE",
        schema: "public",
        table: "uptime",
      },
      async (payload) => {
        // this means we just acknowledged it
        if (payload.new.acknowledged_by_server) {
          console.log('uptime check already acknowledged - skipping');
          return;
        };

        console.log('incoming uptime check request', payload);

        if (!payload.new.acknowledged_by_server) {
          await supabaseAdminClient.from('uptime').update({ acknowledged_by_server: true, acknowledged_at: new Date() }).eq('id', payload.new.id);
          console.log('uptime check acknowledged');
        }
      }
    )

note: this will keep alerting every two minutes until acknowledged. something to resolve in the next version.

Runs every 10 min
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
const DISCORD_WEBHOOK = Deno.env.get("DISCORD_TANAKI_WEBHOOK");
export default async function(interval: Interval) {
const supabase = createClient(
Deno.env.get("TANAKI_SUPABASE_URL"),
Deno.env.get("TANAKI_SERVICE_KEY"),
);
const { data: latestCheck, error: latestCheckError } = await supabase
.from("uptime")
.select("*")
.eq("id", 1)
.single();
if (latestCheck.acknowledged_by_server == false) {
console.log("🚨");
await fetch(DISCORD_WEBHOOK, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content: "Tanaki mac mini listener is DOWN",
}),
});
return;
} else {
console.log("previous check is acknowledged");
}
const { data, error } = await supabase
.from("uptime")
.upsert({
id: 1,
acknowledged_at: null,
last_checked_at: new Date(),
acknowledged_by_server: false,
});
if (error) {
console.log({ error });
}
console.log("sent a check to the server");
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 7, 2024