run method

Future<void> run(
  1. dynamic scheduleNext(
    1. Duration
    )
)

Runs the task

Implementation

Future<void> run(Function(Duration) scheduleNext) async {
  if (_state.isRunning || _state.isPaused) return;

  _state.markRunning();
  final stopwatch = Stopwatch()..start();

  try {
    await job.execute();
    _state.recordSuccess();
  } catch (e, s) {
    _state.recordFailure();
    Khadem.logger.error('❌ Error in [$name]: $e\n$s');

    if (retryOnFail && _state.retryCount < maxRetries) {
      _state.incrementRetry();
      Future.delayed(
        Duration(seconds: 5 * _state.retryCount), // Exponential backoff
        () => run(scheduleNext),
      );
      return;
    }
  } finally {
    stopwatch.stop();
    _state.recordExecutionTime(stopwatch.elapsed);
    _state.markIdle();
  }

  if (!runOnce && !_state.isPaused) {
    _scheduleNext(scheduleNext);
  }
}