stop method

Future<void> stop()

Gracefully stops the worker. Waits for in-flight jobs to finish.

Implementation

Future<void> stop() async {
  if (!_running) return;
  _running = false;
  _pollTimer?.cancel();
  _rateLimitResetTimer?.cancel();

  Logger.staticInfo('⚙️  Worker stopping — waiting for $_activeJobs active job(s)...');

  // Wait for active jobs to drain (with a safety timeout)
  final deadline = DateTime.now().add(const Duration(seconds: 30));
  while (_activeJobs > 0 && DateTime.now().isBefore(deadline)) {
    await Future.delayed(const Duration(milliseconds: 100));
  }

  if (_activeJobs > 0) {
    Logger.staticInfo('⚠️  Worker force-stopped with $_activeJobs job(s) still active');
  }

  Logger.staticInfo('⚙️  Worker stopped.');
  if (_stopCompleter != null && !_stopCompleter!.isCompleted) {
    _stopCompleter!.complete();
  }
}