Refactor heartbeat initialization to accept operational seed and remove redundant registration logic

This commit is contained in:
ImBenji
2025-08-28 21:16:04 +01:00
parent 44346589cc
commit 654fbc30c2

View File

@@ -10,26 +10,19 @@ import 'package:waylume_server/config/supabase_config.dart';
import 'package:waylume_server/services/rolling_codes_service.dart'; import 'package:waylume_server/services/rolling_codes_service.dart';
import 'package:waylume_server/core/utils.dart'; import 'package:waylume_server/core/utils.dart';
void initHeartbeat() { void initHeartbeat(String operationalSeed) {
// Run this on a separate thread. // Run this on a separate thread.
Isolate.spawn((_) async { Isolate.spawn((String seed) async {
// Initialize rolling codes service in this isolate // Initialize rolling codes service in this isolate
await RollingCodesService.initialize(); await RollingCodesService.initialize();
// Set the operational seed that was passed from main isolate
RollingCodesService.setOperationalSeed(seed);
// To avoid server deadlock and wait for main registration // To avoid server deadlock and wait for main registration
await Future.delayed(Duration(seconds: 10 + Random().nextInt(5))); await Future.delayed(Duration(seconds: 10 + Random().nextInt(5)));
// The heartbeat isolate needs to have the operational seed
// Since isolates don't share memory, we need to register again in this isolate
// This will get the same operational seed since the server_id is the same
try {
await _registerInHeartbeatIsolate();
} catch (e) {
print("Failed to register in heartbeat isolate: $e");
return;
}
while (true) { while (true) {
DateTime now = DateTime.now().toUtc(); DateTime now = DateTime.now().toUtc();
@@ -85,43 +78,10 @@ void initHeartbeat() {
await Future.delayed(Duration(seconds: 90)); await Future.delayed(Duration(seconds: 90));
} }
}, null); }, operationalSeed);
} }
Future<void> _registerInHeartbeatIsolate() async {
// Generate registration rolling code
String registrationAuth = RollingCodesService.generateRegistrationCode();
// Call server-manager registration endpoint
String serverManagerUrl = 'https://lsdrctuvnwdrzrdyoqzu.supabase.co/functions/v1/server-manager';
Map<String, dynamic> requestBody = {
'server_id': fromEnivronment('SERVER_ID')!,
'registration_auth': registrationAuth,
};
final response = await http.post(
Uri.parse(serverManagerUrl),
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode(requestBody),
);
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
if (responseData['success']) {
// Store operational seed in memory for this isolate
RollingCodesService.setOperationalSeed(responseData['operational_seed']);
print('Heartbeat isolate registered successfully');
} else {
throw Exception('Heartbeat isolate registration failed: ${responseData['error']}');
}
} else {
throw Exception('Heartbeat isolate registration request failed: ${response.statusCode}');
}
}
Future<Map<String, dynamic>> _getServerStatus() async { Future<Map<String, dynamic>> _getServerStatus() async {
int activeConnections = 0; int activeConnections = 0;