2
This commit is contained in:
1 parent
5ac164b407
commit
f93f6095af
94 files changed
+7877
-4018
No files matched your search
@@ -0,0 +1,123 @@
|
||||
import '../../models/distro_manifest.dart';
|
||||
import '../../models/notice_item.dart';
|
||||
import '../distro_manager.dart';
|
||||
import '../internal_engine_service.dart';
|
||||
import 'background_task.dart';
|
||||
|
||||
/// Ubuntu APT 软件包安装与卸载任务
|
||||
///
|
||||
/// 运行在 [TaskExecutionScope.packageManager] 互斥域内,
|
||||
/// 保证同一时刻全局只有一个 apt-get/dpkg 命令执行,杜绝锁冲突。
|
||||
class AptPackageTask extends BackgroundTask<bool> {
|
||||
final String packageName;
|
||||
final String componentDisplayName;
|
||||
final bool isUninstall;
|
||||
@override
|
||||
final TaskPriority priority;
|
||||
|
||||
AptPackageTask({
|
||||
required this.packageName,
|
||||
required this.componentDisplayName,
|
||||
this.isUninstall = false,
|
||||
this.priority = TaskPriority.userInitiated,
|
||||
});
|
||||
|
||||
@override
|
||||
String get id => 'pkg:${isUninstall ? 'remove' : 'install'}:$packageName';
|
||||
|
||||
@override
|
||||
TaskExecutionScope get scope => TaskExecutionScope.packageManager;
|
||||
|
||||
@override
|
||||
NoticeText? createNoticeText(
|
||||
BackgroundTaskState state, {
|
||||
String? detail,
|
||||
String? error,
|
||||
}) {
|
||||
switch (state) {
|
||||
case BackgroundTaskState.queued:
|
||||
return ComponentInstallPhaseText(
|
||||
componentName: componentDisplayName,
|
||||
isUninstall: isUninstall,
|
||||
isQueued: true,
|
||||
);
|
||||
case BackgroundTaskState.running:
|
||||
return ComponentInstallPhaseText(
|
||||
componentName: componentDisplayName,
|
||||
isUninstall: isUninstall,
|
||||
isQueued: false,
|
||||
detail: detail,
|
||||
);
|
||||
case BackgroundTaskState.completed:
|
||||
return ComponentInstallDoneText(
|
||||
componentName: componentDisplayName,
|
||||
isUninstall: isUninstall,
|
||||
);
|
||||
case BackgroundTaskState.failed:
|
||||
return ComponentInstallFailureText(
|
||||
componentName: componentDisplayName,
|
||||
isUninstall: isUninstall,
|
||||
error: error,
|
||||
);
|
||||
case BackgroundTaskState.cancelled:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> execute(TaskExecutionContext context) async {
|
||||
final engine = InternalEngineService.instance;
|
||||
if (!await engine.isEngineInstalled() || packageName.trim().isEmpty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final rootfs = await engine.getRootfsDir();
|
||||
|
||||
final cmd = isUninstall
|
||||
? 'export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true; '
|
||||
'dpkg --configure -a 2>/dev/null || true; '
|
||||
'apt-get remove --purge -y ${packageName.trim()} && '
|
||||
'apt-get autoremove -y'
|
||||
: 'export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true; '
|
||||
'dpkg --configure -a 2>/dev/null || true; '
|
||||
'apt-get update && '
|
||||
'apt-get install -y --no-install-recommends ${packageName.trim()}';
|
||||
|
||||
final res = await DistroManager().runHeadlessCommand(
|
||||
systemName: DistroRepository.defaultSystemName,
|
||||
customRootDir: rootfs,
|
||||
command: cmd,
|
||||
timeout: isUninstall ? const Duration(minutes: 5) : const Duration(minutes: 10),
|
||||
cancelToken: context.cancelToken,
|
||||
onStdout: (chunk) {
|
||||
final line = chunk.trim();
|
||||
if (line.isNotEmpty) {
|
||||
context.updateDetail(line);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if (context.isCancelled) {
|
||||
// 被取消后,进行一次后台锁自愈
|
||||
_healDpkgLock(rootfs);
|
||||
return false;
|
||||
}
|
||||
|
||||
final success = res != null && res.exitCode == 0;
|
||||
if (!success) {
|
||||
throw Exception(
|
||||
'apt process exited with code ${res?.exitCode ?? -1}: ${res?.stderr}',
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void _healDpkgLock(dynamic rootfs) {
|
||||
DistroManager().runHeadlessCommand(
|
||||
systemName: DistroRepository.defaultSystemName,
|
||||
customRootDir: rootfs,
|
||||
command: 'dpkg --configure -a 2>/dev/null || true',
|
||||
timeout: const Duration(seconds: 30),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import '../../models/notice_item.dart';
|
||||
import '../distro_manager.dart';
|
||||
|
||||
/// 任务执行范围(互斥域)
|
||||
enum TaskExecutionScope {
|
||||
/// 包管理器域:严格单线程串行(Ubuntu 下 apt-get / dpkg 独占锁保护)
|
||||
packageManager,
|
||||
|
||||
/// 独占域:暂停所有其它任务,独自运行
|
||||
exclusive,
|
||||
|
||||
/// 共享只读域:允许与其他非 exclusive 任务并发运行(如无副作用的检查、扫描)
|
||||
shared,
|
||||
}
|
||||
|
||||
/// 任务优先级(值越大越靠前出队)
|
||||
enum TaskPriority implements Comparable<TaskPriority> {
|
||||
/// 用户主动触发(如用户点击安装/卸载组件):最高优先
|
||||
userInitiated(100),
|
||||
|
||||
/// 项目生命周期触发(如打开工程后的模块依赖探测):标准优先级
|
||||
projectLifecycle(50),
|
||||
|
||||
/// 静默预热/后台空闲:低优先级
|
||||
backgroundIdle(10);
|
||||
|
||||
final int value;
|
||||
const TaskPriority(this.value);
|
||||
|
||||
@override
|
||||
int compareTo(TaskPriority other) => other.value.compareTo(value);
|
||||
}
|
||||
|
||||
/// 任务执行状态
|
||||
enum BackgroundTaskState {
|
||||
queued,
|
||||
running,
|
||||
completed,
|
||||
failed,
|
||||
cancelled,
|
||||
}
|
||||
|
||||
/// 任务执行上下文
|
||||
class TaskExecutionContext {
|
||||
/// 用于中断底层进程的取消令牌
|
||||
final HeadlessCancelToken cancelToken;
|
||||
|
||||
/// 实时输出行/副标题更新
|
||||
final void Function(String line) updateDetail;
|
||||
|
||||
/// 进度百分比更新(0.0 ~ 1.0)
|
||||
final void Function(double? progress) updateProgress;
|
||||
|
||||
const TaskExecutionContext({
|
||||
required this.cancelToken,
|
||||
required this.updateDetail,
|
||||
required this.updateProgress,
|
||||
});
|
||||
|
||||
bool get isCancelled => cancelToken.isCancelled;
|
||||
}
|
||||
|
||||
/// 全局后台任务抽象接口
|
||||
abstract class BackgroundTask<T> {
|
||||
/// 全局唯一任务 ID(同时作为右上角通知 NoticeItem 的 id)
|
||||
String get id;
|
||||
|
||||
/// 互斥作用域
|
||||
TaskExecutionScope get scope;
|
||||
|
||||
/// 优先级
|
||||
TaskPriority get priority => TaskPriority.projectLifecycle;
|
||||
|
||||
/// 是否在 NoticeCenter 中展示右上角悬浮通知
|
||||
bool get showNotice => true;
|
||||
|
||||
/// 根据状态生成对应的结构化通知文案(无硬编码)
|
||||
NoticeText? createNoticeText(BackgroundTaskState state, {String? detail, String? error});
|
||||
|
||||
/// 核心执行体
|
||||
Future<T> execute(TaskExecutionContext context);
|
||||
|
||||
/// 取消回调
|
||||
void onCancelled() {}
|
||||
|
||||
/// 失败回调
|
||||
void onFailure(Object error, StackTrace stackTrace) {}
|
||||
|
||||
/// 成功回调
|
||||
void onSuccess(T result) {}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../models/notice_item.dart';
|
||||
import '../../providers/notice_center.dart';
|
||||
import '../distro_manager.dart';
|
||||
import 'background_task.dart';
|
||||
|
||||
/// 待执行任务包装条目
|
||||
class _QueuedTaskEntry<T> {
|
||||
final BackgroundTask<T> task;
|
||||
final Completer<T> completer;
|
||||
final HeadlessCancelToken cancelToken;
|
||||
|
||||
_QueuedTaskEntry({
|
||||
required this.task,
|
||||
required this.completer,
|
||||
required this.cancelToken,
|
||||
});
|
||||
}
|
||||
|
||||
/// 统一后台任务调度中心
|
||||
///
|
||||
/// 核心职责:
|
||||
/// 1. 任务排队与优先级排序;
|
||||
/// 2. [TaskExecutionScope] 资源互斥域管控(针对 Ubuntu 单底座 apt/dpkg 独占锁,严格串行 packageManager 域);
|
||||
/// 3. 与 [NoticeCenter] 双向联动(反映入队、执行中、成功淡出、失败驻留,UI 取消联动底层进程中断);
|
||||
/// 4. 任务取消与失败时的环境自愈保护。
|
||||
class BackgroundTaskScheduler {
|
||||
BackgroundTaskScheduler._();
|
||||
static final BackgroundTaskScheduler instance = BackgroundTaskScheduler._();
|
||||
|
||||
NoticeCenter? _noticeCenter;
|
||||
|
||||
/// 待执行任务队列
|
||||
final List<_QueuedTaskEntry> _queue = [];
|
||||
|
||||
/// 正在运行的任务集合
|
||||
final Map<String, _QueuedTaskEntry> _runningTasks = {};
|
||||
|
||||
/// 绑定全局通知中心
|
||||
void attachNoticeCenter(NoticeCenter noticeCenter) {
|
||||
_noticeCenter = noticeCenter;
|
||||
}
|
||||
|
||||
/// 提交一个后台任务
|
||||
Future<T> submit<T>(BackgroundTask<T> task) {
|
||||
final completer = Completer<T>();
|
||||
final cancelToken = HeadlessCancelToken();
|
||||
|
||||
final entry = _QueuedTaskEntry<T>(
|
||||
task: task,
|
||||
completer: completer,
|
||||
cancelToken: cancelToken,
|
||||
);
|
||||
|
||||
// 插入并按优先级降序排序(优先级高在先)
|
||||
_queue.add(entry);
|
||||
_queue.sort((a, b) => a.task.priority.compareTo(b.task.priority));
|
||||
|
||||
// 通知中心同步:若任务配置了通知,则先发入队卡片
|
||||
if (task.showNotice && _noticeCenter != null) {
|
||||
final text = task.createNoticeText(BackgroundTaskState.queued);
|
||||
if (text != null) {
|
||||
_noticeCenter!.push(
|
||||
NoticeItem(
|
||||
id: task.id,
|
||||
kind: NoticeKind.progress,
|
||||
text: text,
|
||||
onUserDismiss: () => cancel(task.id),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_scheduleNext();
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
/// 取消指定 ID 的任务(若在队列中则移除,若在运行中则触发 cancelToken)
|
||||
void cancel(String taskId) {
|
||||
// 1. 检查队列中尚未启动的任务
|
||||
final qIndex = _queue.indexWhere((e) => e.task.id == taskId);
|
||||
if (qIndex != -1) {
|
||||
final entry = _queue.removeAt(qIndex);
|
||||
entry.cancelToken.cancel();
|
||||
try {
|
||||
entry.task.onCancelled();
|
||||
} catch (e) {
|
||||
debugPrint('[BackgroundTaskScheduler] onCancelled 异常: ');
|
||||
}
|
||||
if (!entry.completer.isCompleted) {
|
||||
entry.completer.completeError(
|
||||
const CancellationException('Task cancelled before start'),
|
||||
);
|
||||
}
|
||||
_noticeCenter?.dismiss(taskId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 检查正在运行的任务
|
||||
final running = _runningTasks[taskId];
|
||||
if (running != null) {
|
||||
running.cancelToken.cancel();
|
||||
try {
|
||||
running.task.onCancelled();
|
||||
} catch (e) {
|
||||
debugPrint('[BackgroundTaskScheduler] onCancelled 异常: ');
|
||||
}
|
||||
// 不直接 dismiss,等待执行体退出抛出/退出时自然结算
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否有任务正在占用 packageManager 锁
|
||||
bool get isPackageManagerBusy {
|
||||
return _runningTasks.values
|
||||
.any((e) => e.task.scope == TaskExecutionScope.packageManager);
|
||||
}
|
||||
|
||||
/// 是否有独占任务正在运行
|
||||
bool get isExclusiveBusy {
|
||||
return _runningTasks.values
|
||||
.any((e) => e.task.scope == TaskExecutionScope.exclusive);
|
||||
}
|
||||
|
||||
/// 正在运行的任务总数
|
||||
int get runningCount => _runningTasks.length;
|
||||
|
||||
/// 等待队列中的任务总数
|
||||
int get queuedCount => _queue.length;
|
||||
|
||||
/// 核心调度分配逻辑
|
||||
void _scheduleNext() {
|
||||
if (_queue.isEmpty) return;
|
||||
|
||||
// 若当前有 exclusive 任务正在运行,阻塞一切新任务
|
||||
if (isExclusiveBusy) return;
|
||||
|
||||
for (int i = 0; i < _queue.length; i++) {
|
||||
final candidate = _queue[i];
|
||||
final scope = candidate.task.scope;
|
||||
|
||||
if (scope == TaskExecutionScope.exclusive) {
|
||||
// 独占任务必须等待当前所有运行中的任务全部结束才能起跑
|
||||
if (_runningTasks.isEmpty) {
|
||||
_queue.removeAt(i);
|
||||
_executeTask(candidate);
|
||||
}
|
||||
return;
|
||||
} else if (scope == TaskExecutionScope.packageManager) {
|
||||
// 包管理器任务要求单线排他
|
||||
if (!isPackageManagerBusy) {
|
||||
_queue.removeAt(i);
|
||||
_executeTask(candidate);
|
||||
// 继续检查后续队列中是否有可以并发执行的 shared 任务
|
||||
_scheduleNext();
|
||||
return;
|
||||
}
|
||||
} else if (scope == TaskExecutionScope.shared) {
|
||||
// 共享任务可以并发运行(受最大并发限制,如 4)
|
||||
if (_runningTasks.length < 4) {
|
||||
_queue.removeAt(i);
|
||||
_executeTask(candidate);
|
||||
_scheduleNext();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _executeTask(_QueuedTaskEntry entry) async {
|
||||
final task = entry.task;
|
||||
_runningTasks[task.id] = entry;
|
||||
|
||||
// 更新为运行中通知
|
||||
if (task.showNotice && _noticeCenter != null) {
|
||||
final text = task.createNoticeText(BackgroundTaskState.running);
|
||||
if (text != null) {
|
||||
_noticeCenter!.update(
|
||||
task.id,
|
||||
kind: NoticeKind.progress,
|
||||
text: text,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final context = TaskExecutionContext(
|
||||
cancelToken: entry.cancelToken,
|
||||
updateDetail: (detail) {
|
||||
if (!entry.cancelToken.isCancelled && task.showNotice && _noticeCenter != null) {
|
||||
final text = task.createNoticeText(BackgroundTaskState.running, detail: detail);
|
||||
if (text != null) {
|
||||
_noticeCenter!.update(task.id, text: text);
|
||||
}
|
||||
}
|
||||
},
|
||||
updateProgress: (progress) {
|
||||
if (!entry.cancelToken.isCancelled && task.showNotice && _noticeCenter != null) {
|
||||
_noticeCenter!.update(task.id, progress: progress);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
final result = await task.execute(context);
|
||||
_runningTasks.remove(task.id);
|
||||
|
||||
if (entry.cancelToken.isCancelled) {
|
||||
try {
|
||||
task.onCancelled();
|
||||
} catch (e) {
|
||||
debugPrint('[BackgroundTaskScheduler] onCancelled 异常: ');
|
||||
}
|
||||
_noticeCenter?.dismiss(task.id);
|
||||
if (!entry.completer.isCompleted) {
|
||||
entry.completer.completeError(
|
||||
const CancellationException('Task was cancelled during execution'),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
task.onSuccess(result);
|
||||
} catch (e) {
|
||||
debugPrint('[BackgroundTaskScheduler] onSuccess 异常: ');
|
||||
}
|
||||
|
||||
if (task.showNotice && _noticeCenter != null) {
|
||||
final doneText = task.createNoticeText(BackgroundTaskState.completed);
|
||||
if (doneText != null) {
|
||||
_noticeCenter!.complete(
|
||||
task.id,
|
||||
kind: NoticeKind.success,
|
||||
text: doneText,
|
||||
autoCloseAfter: const Duration(seconds: 3),
|
||||
);
|
||||
} else {
|
||||
_noticeCenter!.dismiss(task.id);
|
||||
}
|
||||
}
|
||||
|
||||
if (!entry.completer.isCompleted) {
|
||||
entry.completer.complete(result);
|
||||
}
|
||||
}
|
||||
} catch (e, stack) {
|
||||
_runningTasks.remove(task.id);
|
||||
|
||||
if (entry.cancelToken.isCancelled) {
|
||||
try {
|
||||
task.onCancelled();
|
||||
} catch (err) {
|
||||
debugPrint('[BackgroundTaskScheduler] onCancelled 异常: ');
|
||||
}
|
||||
_noticeCenter?.dismiss(task.id);
|
||||
if (!entry.completer.isCompleted) {
|
||||
entry.completer.completeError(
|
||||
const CancellationException('Task was cancelled'),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
task.onFailure(e, stack);
|
||||
} catch (err) {
|
||||
debugPrint('[BackgroundTaskScheduler] onFailure 异常: ');
|
||||
}
|
||||
|
||||
if (task.showNotice && _noticeCenter != null) {
|
||||
final failText = task.createNoticeText(
|
||||
BackgroundTaskState.failed,
|
||||
error: e.toString(),
|
||||
);
|
||||
if (failText != null) {
|
||||
_noticeCenter!.complete(
|
||||
task.id,
|
||||
kind: NoticeKind.failure,
|
||||
text: failText,
|
||||
sticky: true,
|
||||
);
|
||||
} else {
|
||||
_noticeCenter!.dismiss(task.id);
|
||||
}
|
||||
}
|
||||
|
||||
if (!entry.completer.isCompleted) {
|
||||
entry.completer.completeError(e, stack);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// 触发后续调度
|
||||
_scheduleNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 取消异常定义
|
||||
class CancellationException implements Exception {
|
||||
final String message;
|
||||
const CancellationException([this.message = 'Operation was cancelled']);
|
||||
|
||||
@override
|
||||
String toString() => 'CancellationException: ';
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import '../../models/notice_item.dart';
|
||||
import 'background_task.dart';
|
||||
|
||||
/// 包装一个函数执行体到后台调度系统(如探测中的依赖自动安装等)
|
||||
class GenericBackgroundTask<T> extends BackgroundTask<T> {
|
||||
@override
|
||||
final String id;
|
||||
@override
|
||||
final TaskExecutionScope scope;
|
||||
@override
|
||||
final TaskPriority priority;
|
||||
@override
|
||||
final bool showNotice;
|
||||
|
||||
final Future<T> Function(TaskExecutionContext context) runner;
|
||||
final NoticeText? Function(BackgroundTaskState state, {String? detail, String? error})? noticeTextBuilder;
|
||||
|
||||
GenericBackgroundTask({
|
||||
required this.id,
|
||||
required this.scope,
|
||||
this.priority = TaskPriority.projectLifecycle,
|
||||
this.showNotice = false,
|
||||
required this.runner,
|
||||
this.noticeTextBuilder,
|
||||
});
|
||||
|
||||
@override
|
||||
NoticeText? createNoticeText(BackgroundTaskState state, {String? detail, String? error}) {
|
||||
return noticeTextBuilder?.call(state, detail: detail, error: error);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> execute(TaskExecutionContext context) {
|
||||
return runner(context);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user