cancelTimers method

Future<void> cancelTimers(
  1. String workflowExecutionId
)

Cancels all pending timers for a given execution.

Implementation

Future<void> cancelTimers(String workflowExecutionId) async {
  final pendingTimers = await _store.loadPendingTimers();
  for (final timer in pendingTimers) {
    if (timer.workflowExecutionId != workflowExecutionId) continue;

    final timerId = timer.workflowTimerId;

    await _store.saveTimer(
      timer.copyWith(status: TimerStatus.cancelled),
    );

    _dartTimers[timerId]?.cancel();
    _dartTimers.remove(timerId);

    // Complete the completer with WorkflowCancelledException so the
    // engine's run() method catches it properly.
    final completer = _completers.remove(timerId);
    if (completer != null && !completer.isCompleted) {
      completer.completeError(
        WorkflowCancelledException(workflowExecutionId),
      );
    }
  }
}