start method

Future start ({int numberOfInstances: 1, bool consoleLogging: false })

Starts this application, allowing it to handle HTTP requests.

This method spawns numberOfInstances isolates, instantiates your application channel for each of these isolates, and opens an HTTP listener that sends requests to these instances.

The Future returned from this method will complete once all isolates have successfully started and are available to handle requests.

If your application channel implements ApplicationChannel.initializeApplication, it will be invoked prior to any isolate being spawned.

See also startOnCurrentIsolate for starting an application when running automated tests.

Implementation

Future start({int numberOfInstances = 1, bool consoleLogging = false}) async {
  if (server != null || supervisors.isNotEmpty) {
    throw StateError(
        "Application error. Cannot invoke 'start' on already running Aqueduct application.");
  }

  if (options.address == null) {
    if (options.isIpv6Only) {
      options.address = InternetAddress.anyIPv6;
    } else {
      options.address = InternetAddress.anyIPv4;
    }
  }

  final channelType = reflectClass(T);
  try {
    await _globalStart(channelType, options);

    for (var i = 0; i < numberOfInstances; i++) {
      final supervisor = await _spawn(channelType, options, i + 1,
          logToConsole: consoleLogging);
      supervisors.add(supervisor);
      await supervisor.resume();
    }
  } catch (e, st) {
    logger.severe("$e", this, st);
    await stop().timeout(Duration(seconds: 5));
    rethrow;
  }
  supervisors.forEach((sup) => sup.sendPendingMessages());
  _hasFinishedLaunching = true;
}