start method

Future<void> start([
  1. CancellationToken? cancellationToken
])

Start the program.

Implementation

Future<void> start([
  CancellationToken? cancellationToken,
]) async {
  _logger.starting();

  cancellationToken ??= CancellationToken.none;

  final combinedCancellationTokenSource =
      CancellationTokenSource.createLinkedTokenSource(
    [
      cancellationToken,
      _applicationLifetime.applicationStopping,
    ],
  );
  final combinedCancellationToken = combinedCancellationTokenSource.token;

  await _hostLifetime.waitForStart(combinedCancellationToken);

  combinedCancellationToken.throwIfCancellationRequested();
  _hostedServices = services.getServices<HostedService>();

  for (var hostedService in _hostedServices!) {
    // Fire HostedService.start
    await hostedService.start(combinedCancellationToken);

    if (hostedService is BackgroundService) {
      await _tryExecuteBackgroundService(hostedService);
    }
  }

  // Fire HostApplicationLifetime.started
  _applicationLifetime.notifyStarted();
  _logger.started();
}