Files
waylume_server/lib/supabase_heartbeat.dart
ImBenji e126e6ff94 Initial commit
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-04 15:38:46 +01:00

35 lines
857 B
Dart

// Run a heartbeat to let supabase know that the client is still active.
import 'dart:io';
import 'dart:isolate';
import 'dart:math';
import 'package:supabase/supabase.dart';
import 'package:waylume_server/main.dart';
void initHeartbeat() {
// Run this on a separate thread.
Isolate.spawn((_) async {
// To avoid server deadlock
await Future.delayed(Duration(seconds: Random().nextInt(5)));
while (true) {
DateTime now = DateTime.now().toUtc();
await SUPABASE_CLIENT
.from("waylume_servers")
.update({ "last_heartbeat": now.toIso8601String() })
.eq("id", fromEnivronment("SERVER_ID")!);
print("Heartbeat sent to Supabase at ${now.toIso8601String()}");
// Wait 30 seconds before sending the next heartbeat
await Future.delayed(Duration(seconds: 30));
}
}, null);
}