claimRun method

  1. @override
Future<bool> claimRun(
  1. String runId, {
  2. required String ownerId,
  3. Duration leaseDuration = const Duration(seconds: 30),
})
override

Attempts to claim a run for execution with a lease.

Returns true when the claim succeeds, or false if another worker holds an active lease.

Implementation

@override
Future<bool> claimRun(
  String runId, {
  required String ownerId,
  Duration leaseDuration = const Duration(seconds: 30),
}) async {
  final state = _runs[runId];
  if (state == null) return false;
  if (state.status != WorkflowStatus.running) return false;
  if (state.waitTopic != null) return false;
  final now = _clock.now();
  final currentOwner = state.ownerId;
  if (currentOwner != null &&
      currentOwner.isNotEmpty &&
      currentOwner != ownerId &&
      !_leaseExpired(state, now)) {
    return false;
  }
  _runs[runId] = state.copyWith(
    ownerId: ownerId,
    leaseExpiresAt: now.add(leaseDuration),
    updatedAt: now,
  );
  return true;
}