spawn method
create the isolates 2 to count
await DoxIsolateGroups(snapshot).spawn(3)
Implementation
@override
Future<void> spawn(int count) async {
stopped = false;
firstStart = true;
try {
for (int i = 1; i < count; i++) {
await spawnOne(i + 1);
}
// a group runs `main()` of the app. When that does not reach
// `startServer()`, the isolate ends and the caller must know.
// one time limit for all of them, they start at the same time
DateTime end = DateTime.now().add(startTimeout);
for (int id = 2; id <= count; id++) {
// null: it ended, and it could not start again
Completer<bool>? isReady = ready[id];
bool serves = false;
if (isReady != null) {
Duration left = end.difference(DateTime.now());
try {
serves = await isReady.future.timeout(
left.isNegative ? Duration.zero : left,
);
} on TimeoutException {
throw IsolateSpawnException(
'isolate $id did not serve in ${startTimeout.inSeconds} '
'seconds. main() must reach startServer() in every isolate',
);
}
}
if (!serves) {
throw IsolateSpawnException(
'isolate $id ended before it served. See its error above. '
'main() must reach startServer() in every isolate',
);
}
}
} finally {
firstStart = false;
}
}