35 lines
875 B
Dart
35 lines
875 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/config/supabase_config.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);
|
|
|
|
} |