claimRun method
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;
}