Feat: Add ntfy notifications for important events

This commit is contained in:
2025-11-06 18:39:32 -08:00
parent 61f04c5bf6
commit aae0150de8

View File

@@ -56,7 +56,6 @@ export class MyDurableObject {
this.env = env;
this.sockets = new Set();
this.reset();
this.notify(`DO instance created`, 2, ['construction_worker']);
}
reset() {
@@ -85,11 +84,10 @@ export class MyDurableObject {
notify(msg, pri = 3, tags = []) {
if (!this.env.NTFY_TOPIC) return;
const title = `Sune ORP [${this.state.id.toString().slice(-8)}]`;
this.state.waitUntil(fetch(`https://ntfy.sh/${this.env.NTFY_TOPIC}`, {
method: 'POST',
body: msg,
headers: { 'Title': title, 'Priority': `${pri}`, 'Tags': tags.join(',') },
headers: { 'Title': 'Sune ORP', 'Priority': `${pri}`, 'Tags': tags.join(',') },
}).catch(e => console.error('ntfy failed:', e)));
}
@@ -156,11 +154,7 @@ export class MyDurableObject {
const [client, server] = Object.values(new WebSocketPair());
server.accept();
this.sockets.add(server);
this.notify(`WS connected. Sockets: ${this.sockets.size}`, 2, ['wave']);
server.addEventListener('close', () => {
this.sockets.delete(server);
this.notify(`WS disconnected. Sockets: ${this.sockets.size}`, 2, ['end']);
});
server.addEventListener('close', () => this.sockets.delete(server));
server.addEventListener('message', e => this.state.waitUntil(this.onMessage(server, e)));
return new Response(null, { status: 101, webSocket: client });
}
@@ -342,7 +336,6 @@ export class MyDurableObject {
try { this.oaStream?.controller?.abort(); } catch {}
this.saveSnapshot();
this.bcast({ type: 'done' });
this.notify(`Run ${this.rid} finished successfully`, 3, ['tada']);
this.state.waitUntil(this.stopHeartbeat());
}
@@ -366,9 +359,7 @@ export class MyDurableObject {
}
async stopHeartbeat() {
if (!this.hbActive) return;
this.hbActive = false;
this.notify(`Heartbeat stopped`, 2, ['stop_sign']);
await this.state.storage.setAlarm(null).catch(() => {});
}