Add Worker service with isolated task management and integrate into app
This commit is contained in:
47
finlog_app/fluttery/lib/worker.dart
Normal file
47
finlog_app/fluttery/lib/worker.dart
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user