start method

Future start({
  1. int numberOfInstances = 1,
  2. 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 (supervisors.isNotEmpty) {
    throw StateError(
      "Application error. Cannot invoke 'start' on already running Conduit application.",
    );
  }

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

  try {
    await _runtime.runGlobalInitialization(options);

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