cleanupCompleted method

  1. @override
Future<int> cleanupCompleted({
  1. Duration maxAge = const Duration(days: 7),
})
override

Implementation

@override
Future<int> cleanupCompleted({
  Duration maxAge = const Duration(days: 7),
}) async => _locked(() async {
  final now = DateTime.now().toUtc();
  final all = await _allJobs();
  final state = await _syncState();
  var deletedCount = 0;
  final deletedIds = <String>[];

  for (final job in all) {
    if (job.status == RegenerationJobStatus.completed ||
        job.status == RegenerationJobStatus.cancelled) {
      if (now.difference(job.createdAt) > maxAge) {
        final file = _jobFile(job.id);
        if (await file.exists()) await file.delete();
        state.idempotency.remove(
          '${job.siteId}|${job.tenantId}|${job.idempotencyKey}',
        );
        state.pending.remove(job.id);
        state.processing.remove(job.id);
        deletedIds.add(job.id);
        deletedCount++;
      }
    }
  }

  if (deletedCount > 0) {
    await _appendJournal({
      'seq': ++state.lastSeq,
      'op': 'cleanup',
      'deletedIds': deletedIds,
    });
    await _checkpointInternal();
  }
  return deletedCount;
});