markExecuted method

  1. @override
Future<void> markExecuted(
  1. String id, {
  2. required DateTime scheduledFor,
  3. required DateTime executedAt,
  4. Duration? jitter,
  5. String? lastError,
  6. bool success = true,
  7. Duration? runDuration,
  8. DateTime? nextRunAt,
  9. Duration? drift,
})
override

Updates execution metadata for the entry id.

Implementation

@override
Future<void> markExecuted(
  String id, {
  required DateTime scheduledFor,
  required DateTime executedAt,
  Duration? jitter,
  String? lastError,
  bool success = true,
  Duration? runDuration,
  DateTime? nextRunAt,
  Duration? drift,
}) async {
  final state = _entries[id];
  if (state == null) return;
  final resolvedSuccess = success && lastError == null;
  final updatedEntry = state.entry.copyWith(
    lastRunAt: executedAt,
    lastError: lastError,
    lastJitter: jitter,
    lastSuccessAt: resolvedSuccess ? executedAt : state.entry.lastSuccessAt,
    lastErrorAt: resolvedSuccess ? state.entry.lastErrorAt : executedAt,
    totalRunCount: state.entry.totalRunCount + 1,
    drift: drift ?? state.entry.drift,
  );
  var next = nextRunAt;
  var enabled = updatedEntry.enabled;
  if (next == null && enabled) {
    try {
      next = _calculator.nextRun(
        updatedEntry,
        executedAt,
        includeJitter: false,
      );
    } on Exception {
      next = executedAt;
    }
  }
  if (updatedEntry.expireAt != null &&
      !executedAt.isBefore(updatedEntry.expireAt!)) {
    enabled = false;
  }
  if (updatedEntry.spec is ClockedScheduleSpec) {
    final spec = updatedEntry.spec as ClockedScheduleSpec;
    if (spec.runOnce && !executedAt.isBefore(spec.runAt)) {
      enabled = false;
    }
  }
  next ??= executedAt;
  final nextVersion = state.entry.version + 1;
  state
    ..entry = updatedEntry.copyWith(
      nextRunAt: next,
      enabled: enabled,
      version: nextVersion,
    )
    ..nextRun = next
    ..locked = false;
}