11import 'package:background_fetch/background_fetch.dart' ;
2+ import 'package:threebotlogin/apps/notifications/notifications_user_data.dart' ;
3+ import 'package:threebotlogin/models/farm.dart' ;
24import 'package:threebotlogin/services/nodes_check_service.dart' ;
35import 'notification_service.dart' ;
6+ import 'package:threebotlogin/helpers/logger.dart' ;
47
58void backgroundFetchHeadlessTask (HeadlessTask task) async {
69 final String taskId = task.taskId;
@@ -10,50 +13,70 @@ void backgroundFetchHeadlessTask(HeadlessTask task) async {
1013 BackgroundFetch .finish (taskId);
1114 return ;
1215 }
13- await checkNodeStatus ();
16+ final bool notificationsEnabled = await isNodeStatusNotificationEnabled ();
1417
15- BackgroundFetch .finish (taskId);
18+ logger.i (
19+ 'Background Fetch Headless Task: $taskId , Notifications Enabled: $notificationsEnabled ' );
20+
21+ if (! notificationsEnabled) {
22+ logger.i (
23+ '[BackgroundFetch] Node status notifications are disabled. Finishing task: $taskId ' );
24+ BackgroundFetch .finish (taskId);
25+ return ;
26+ }
27+ await checkNodeStatus (taskId);
1628}
1729
18- Future <void > checkNodeStatus () async {
19- final offlineNodes = await NodeCheckService . pingNodesInBackground ();
20- if ( offlineNodes.isEmpty) return ;
30+ Future <void > checkNodeStatus (String taskId ) async {
31+ try {
32+ final offlineNodes = await NodeCheckService . pingNodesInBackground () ;
2133
22- final now = DateTime .now ().millisecondsSinceEpoch;
23- final sevenDaysAgoTimestamp =
24- DateTime .now ().subtract (const Duration (days: 7 )).millisecondsSinceEpoch;
34+ if (offlineNodes.isEmpty) return ;
2535
26- final nodesToNotify = offlineNodes.where ((node) {
27- final nodeUpdatedAtMs = node.updatedAt! * 1000 ;
28- if (nodeUpdatedAtMs <= sevenDaysAgoTimestamp) return false ;
36+ final StringBuffer bodyBuffer = StringBuffer ();
37+ final List <Node > nodesToNotify = [];
38+ final now = DateTime .now ();
39+ final nowInMs = now.millisecondsSinceEpoch;
40+ final sevenDaysAgoTimestampMs =
41+ now.subtract (const Duration (days: 7 )).millisecondsSinceEpoch;
2942
30- final downtime = Duration (milliseconds : now - nodeUpdatedAtMs);
31- final checkInterval = _getCheckInterval (downtime) ;
43+ for ( final node in offlineNodes) {
44+ final nodeUpdatedAtMs = node.updatedAt ! * 1000 ;
3245
33- return downtime.inMinutes % checkInterval.inMinutes < 15 ;
34- }).toList ();
46+ if (nodeUpdatedAtMs <= sevenDaysAgoTimestampMs) continue ;
3547
36- if (nodesToNotify.isEmpty) return ;
48+ final downtime = Duration (milliseconds : nowInMs - nodeUpdatedAtMs) ;
3749
38- const groupKey = 'offline_nodes' ;
39- final StringBuffer bodyBuffer = StringBuffer ();
50+ final checkInterval = _getCheckInterval (downtime);
4051
41- for ( final node in nodesToNotify) {
42- final nodeUpdatedAtMs = node.updatedAt ! * 1000 ;
43- final downtime =
44- _formatDowntime ( Duration (milliseconds : now - nodeUpdatedAtMs));
52+ bool passesIntervalCheck = false ;
53+ if (downtime.inMinutes > 0 && checkInterval.inMinutes > 0 ) {
54+ passesIntervalCheck = downtime.inMinutes % checkInterval.inMinutes < 15 ;
55+ }
4556
46- bodyBuffer.writeln ('Node ${node .nodeId }: offline for $downtime ' );
47- }
57+ if (passesIntervalCheck) {
58+ nodesToNotify.add (node);
59+ final formattedDowntime = _formatDowntime (downtime);
60+ bodyBuffer
61+ .writeln ('Node ${node .nodeId }: offline for $formattedDowntime ' );
62+ }
63+ }
64+
65+ if (nodesToNotify.isEmpty) return ;
4866
49- await NotificationService ().showNotification (
50- id: nodesToNotify.hashCode,
51- title: nodesToNotify.length == 1
52- ? 'Node Alert 🚨'
53- : '${nodesToNotify .length } Nodes Offline 🚨' ,
54- body: bodyBuffer.toString ().trim (),
55- groupKey: groupKey,
56- );
67+ await NotificationService ().showNotification (
68+ id: nodesToNotify.hashCode,
69+ title: nodesToNotify.length == 1
70+ ? 'Node Alert 🚨'
71+ : '${nodesToNotify .length } Nodes Offline 🚨' ,
72+ body: bodyBuffer.toString ().trim (),
73+ groupKey: 'offline_nodes' ,
74+ );
75+ } catch (e) {
76+ logger.e ('Error in checkNodeStatus for task $taskId : $e ' );
77+ } finally {
78+ BackgroundFetch .finish (taskId);
79+ }
5780}
5881
5982Duration _getCheckInterval (Duration downtime) {
0 commit comments