This commit is contained in:
lzx
2026-01-23 16:11:20 +08:00
parent e4e0e44555
commit efce651809
10 changed files with 276 additions and 77 deletions

View File

@@ -15,6 +15,10 @@ class MiningManager {
MiningTaskInfo? _currentTask;
Timer? _taskMonitor;
final StreamController<String> _minerLogController = StreamController<String>.broadcast();
final StreamController<void> _processExitController = StreamController<void>.broadcast();
/// 进程退出事件流
Stream<void> get processExitStream => _processExitController.stream;
/// 启动挖矿
Future<bool> startMining(MiningTaskInfo task, MiningConfig config) async {
@@ -71,6 +75,7 @@ class MiningManager {
_currentTask = task;
_startTaskMonitor(task);
_startLogCapture();
_monitorProcessExit();
_logger.info('挖矿已启动 (PID: ${_currentProcess!.pid})');
return true;
@@ -183,6 +188,28 @@ class MiningManager {
});
}
/// 监控进程退出
void _monitorProcessExit() {
if (_currentProcess == null) return;
_currentProcess!.exitCode.then((exitCode) {
_logger.warning('挖矿进程已退出,退出码: $exitCode');
// 清理状态
_currentProcess = null;
final wasMining = _currentTask != null;
_currentTask = null;
_taskMonitor?.cancel();
_taskMonitor = null;
// 发送进程退出事件
if (wasMining) {
_processExitController.add(null);
}
}).catchError((e) {
_logger.severe('监控进程退出失败: $e');
});
}
/// 获取挖矿日志流
Stream<String> get minerLogStream => _minerLogController.stream;
@@ -192,6 +219,7 @@ class MiningManager {
/// 清理资源
void dispose() {
_minerLogController.close();
_processExitController.close();
}
}