trigger method

Future<void> trigger()

Trigger the task, if it's not already running.

This attempts to claim the status and trigger the task, if it is not already running.

Returns a Future that completes when the task is done.

Implementation

Future<void> trigger() async {
  _log.finest(() => 'NeatPeriodicTaskScheduler "$_name" trigger() called');
  final status = await _getStatus();

  // Find time elapsed since last time the task was started.
  final now = DateTime.now().toUtc();
  final elapsed = now.difference(status.started);

  // If not running, or timed-out we run the task again.
  if (status.state != 'running' || elapsed > _timeout) {
    await _claimAndRun(status.update(
      owner: Slugid.nice().toString(),
      state: 'running',
      started: now,
    ));
  } else {
    _log.info('trigger() call on "$_name" ignored, as task is running');
  }
}