Skip to content

Commit 4628499

Browse files
ProcessMonitorProvider updates:
- Improved the formatting of the swap command for better readability. - Added a timeout for the swap command to prevent indefinite waiting. - Included a specific TimeoutException to handle command timeouts.
1 parent cbe8f7e commit 4628499

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lib/providers/process_monitor_provider.dart

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
23
import 'package:flutter/foundation.dart';
34
import 'package:flutter_riverpod/flutter_riverpod.dart';
45
import 'package:sysadmin/providers/ssh_state.dart';
@@ -139,14 +140,15 @@ class ProcessMonitorNotifier extends StateNotifier<ProcessMonitorState> {
139140
final memOutput = String.fromCharCodes(memResult).trim();
140141

141142
// For swap, we use a more efficient command
142-
final swapResult = await sshClient
143-
.run('grep VmSwap /proc/[0-9]*/status 2>/dev/null | ' +
144-
'sort -nr -k2 | head -n 5 | ' +
145-
'sed -e "s/[^0-9]\\+\\([0-9]\\+\\)[^0-9]\\+\\([0-9]\\+\\).*/\\1 \\2/" | ' +
146-
'while read pid swap; do comm=`cat /proc/\$pid/comm 2>/dev/null`; echo "\$pid \$swap \$comm"; done')
147-
.timeout(const Duration(seconds: 5), onTimeout: () {
148-
throw TimeoutException('Command timed out');
149-
});
143+
final swapResult = await sshClient.run(
144+
'grep VmSwap /proc/[0-9]*/status 2>/dev/null | '
145+
'sort -nr -k2 | head -n 5 | '
146+
'sed -e "s/[^0-9]\\+\\([0-9]\\+\\)[^0-9]\\+\\([0-9]\\+\\).*/\\1 \\2/" | '
147+
'while read pid swap; do comm=`cat /proc/\$pid/comm 2>/dev/null`; echo "\$pid \$swap \$comm"; done'
148+
).timeout(
149+
const Duration(seconds: 5),
150+
onTimeout: () => throw TimeoutException('Command timed out')
151+
);
150152

151153
if (_disposed) {
152154
_isRefreshing = false;
@@ -170,15 +172,17 @@ class ProcessMonitorNotifier extends StateNotifier<ProcessMonitorState> {
170172
error: null,
171173
);
172174
}
173-
} catch (e) {
175+
}
176+
catch (e) {
174177
if (!_disposed) {
175178
debugPrint('Error fetching process data: $e');
176179
state = state.copyWith(
177180
isLoading: false,
178181
error: 'Failed to fetch process data',
179182
);
180183
}
181-
} finally {
184+
}
185+
finally {
182186
_isRefreshing = false;
183187
}
184188
}
@@ -213,7 +217,8 @@ class ProcessMonitorNotifier extends StateNotifier<ProcessMonitorState> {
213217
memoryMB: memoryMB,
214218
swapMB: 0, // We don't have swap info here
215219
));
216-
} catch (e) {
220+
}
221+
catch (e) {
217222
debugPrint('Error parsing process line: $line - $e');
218223
}
219224
}
@@ -247,7 +252,8 @@ class ProcessMonitorNotifier extends StateNotifier<ProcessMonitorState> {
247252
memoryMB: 0, // We don't have memory info here
248253
swapMB: swapMB,
249254
));
250-
} catch (e) {
255+
}
256+
catch (e) {
251257
debugPrint('Error parsing swap process line: $line - $e');
252258
}
253259
}

0 commit comments

Comments
 (0)