Initial commit
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
35
lib/supabase_heartbeat.dart
Normal file
35
lib/supabase_heartbeat.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
// 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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user