start method
Starts the agent and resolves once it is first connected and registered.
Throws an AuthException if authentication is rejected (the agent does not reconnect after an auth failure — a rejected credential is not fixed by trying again). Transient transport failures are retried with backoff.
Implementation
Future<void> start() async {
if (_running) {
throw StateError('agent already started');
}
_running = true;
final runtime = omnyhub.NodeRuntime(
_buildConfig(),
logger: _runtimeLogger(),
);
_runtime = runtime;
final ready = Completer<void>();
_runtimeStates = runtime.states.listen((state) {
_setState(_mapState(state));
if (state == omnyhub.NodeState.ready) {
// Heartbeats are periodic, so the first one — and the status snapshot it
// carries — is a full interval away. Push a snapshot now instead, or the
// Hub reports no status at all for a node that just came up (15s by
// default). Fires on every (re)registration, not just the first.
unawaited(reportStatus());
}
if (state == omnyhub.NodeState.ready && !ready.isCompleted) {
_log('registered as ${config.nodeId}');
ready.complete();
}
// A terminal failure ends the runtime; surface it to whoever is waiting on
// start() instead of leaving them hanging on a node that will never come up.
if (state == omnyhub.NodeState.stopped && !ready.isCompleted) {
final error = runtime.terminalError;
ready.completeError(
error ?? const TransportException('node stopped before registering'),
);
}
});
await runtime.start();
try {
await ready.future;
} on Object {
_running = false;
rethrow;
}
}