Add Worker service with isolated task management and integrate into app

This commit is contained in:
2025-09-22 19:30:18 +02:00
parent cfd38211a2
commit d374ff6bf9
5 changed files with 370 additions and 58 deletions

View File

@@ -0,0 +1,47 @@
import 'dart:async';
import 'package:fluttery/fluttery.dart';
abstract class Worker extends Service {
Future<T> spawn<T>(
String debugName,
FutureOr<T> Function() task, {
void Function()? preTask,
Duration? timeout, // per-job override
});
/// Currently running jobs.
List<WorkerInfo> getActiveWorkers();
/// All known jobs (active + completed + failed), up to a capped history.
List<WorkerInfo> getAllWorkers();
/// Optional: get a single worker by id.
WorkerInfo? getWorker(String id);
/// Remove completed/failed jobs older than [maxAge] from history.
void purge({Duration maxAge = const Duration(minutes: 30)});
}
enum WorkerStatus { running, completed, failed, timedOut }
class WorkerInfo {
WorkerInfo({
required this.id,
required this.name,
required this.startedAt,
required this.status,
this.endedAt,
this.error,
this.stackTrace,
});
final String id;
final String name;
final DateTime startedAt;
final WorkerStatus status;
final DateTime? endedAt;
final Object? error;
final StackTrace? stackTrace;
Duration get duration => ((endedAt ?? DateTime.now()).difference(startedAt));
}