mnemo_cards/mnemo_cards_backend/lib/cron/cron_executor.dart
Dmitry 550b56276e
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Mobile App CI / test (push) Waiting to run
Mobile App CI / build-android (push) Blocked by required conditions
Mobile App CI / build-ios (push) Blocked by required conditions
Web App CI / test (push) Waiting to run
Web App CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
postgress fix
2025-12-13 23:55:50 +03:00

52 lines
1.4 KiB
Dart

import 'dart:developer';
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
import 'package:neat_periodic_task/neat_periodic_task.dart';
import 'task.dart';
class CronManager {
final Map<String, Task> _tasks;
List<NeatPeriodicTaskScheduler>? _schedulers;
CronManager(List<Task> tasks)
: _tasks = Map.fromIterable(tasks, key: (v) => v.name);
Future<void> init() async {
_schedulers = [];
// Tasks are loaded from database via TaskDao if needed
final tasksToAdd = _tasks.keys;
final models = tasksToAdd.map((name) => TaskModel.create(
name: name,
interval: Duration(seconds: _tasks[name]!.intervalSeconds),
));
for (final model in models) {
if (_tasks[model.name] != null) {
_schedulers!.add(
NeatPeriodicTaskScheduler(
interval: model.interval,
name: model.name,
timeout: model.timeout,
task: _tasks[model.name]!.run,
minCycle: model.minCycle,
maxCycle: model.maxCycle,
),
);
}
}
for (final scheduler in _schedulers!) {
scheduler.start();
}
print(
'Cron executor inited with ${_schedulers?.length} tasks (${tasksToAdd.length} new)',
);
}
Future<void> dispose() async {
if (_schedulers != null) {
for (final scheduler in _schedulers!) {
await scheduler.stop().catchError((err) => log(err));
}
}
}
}