feat: implement service failure handling in background streaming

- Added a method to send failure notifications to Flutter when the background service fails to enter the foreground.
- Implemented a broadcast receiver to handle service failure notifications and notify Flutter about the failure.
- Enhanced the persistent streaming service to attempt recovery for failed streams.
- Introduced heartbeat monitoring for SSE streams to detect stale connections and trigger recovery actions.
This commit is contained in:
cogwheel0
2025-10-28 13:59:17 +05:30
parent 81eb38dc52
commit 7fb199b2e4
7 changed files with 265 additions and 25 deletions

View File

@@ -27,9 +27,11 @@ class BackgroundStreamingHandler {
void Function(List<String> streamIds)? onStreamsSuspending;
void Function()? onBackgroundTaskExpiring;
void Function(List<String> streamIds, int estimatedSeconds)?
onBackgroundTaskExtended;
onBackgroundTaskExtended;
void Function()? onBackgroundKeepAlive;
bool Function()? shouldContinueInBackground;
void Function(String error, String errorType, List<String> streamIds)?
onServiceFailed;
void _setupMethodCallHandler() {
_channel.setMethodCallHandler((call) async {
@@ -79,6 +81,31 @@ class BackgroundStreamingHandler {
DebugLogger.stream('keepalive-signal', scope: 'background');
onBackgroundKeepAlive?.call();
break;
case 'serviceFailed':
final Map<String, dynamic> args =
call.arguments as Map<String, dynamic>;
final String error = args['error'] as String? ?? 'Unknown error';
final String errorType = args['errorType'] as String? ?? 'Exception';
final List<String> streamIds =
(args['streamIds'] as List?)?.cast<String>() ?? [];
DebugLogger.error(
'service-failed',
scope: 'background',
error: error,
data: {'type': errorType, 'streams': streamIds.length},
);
// Notify callback about service failure
onServiceFailed?.call(error, errorType, streamIds);
// Clean up failed streams
for (final streamId in streamIds) {
_activeStreamIds.remove(streamId);
_streamStates.remove(streamId);
}
break;
}
});
}