runLogbookService function

Future<int> runLogbookService(
  1. Map<String, String> env, {
  2. IOSink? out,
  3. IOSink? err,
  4. Future<void>? stop,
})

Runs a logbook's one writer (see package:is_logbook/service.dart) as env configures it, until stop completes, and returns the exit code: 0 once it has closed, cannotStart when it could not start, having written why to err.

Once listening, it writes to out where it writes and listens, then code <hash> approved or code <hash> NOT approved: whether its code hash is a line of the logbook's approved file. That is a warning, never a refusal: a missing line must not stop every writer.

Without stop, it stops on SIGTERM or SIGINT, watched from the start, so a stop sent once the start lines are out closes it cleanly.

Implementation

Future<int> runLogbookService(
  Map<String, String> env, {
  IOSink? out,
  IOSink? err,
  Future<void>? stop,
}) async {
  out ??= stdout;
  err ??= stderr;
  final signals = <StreamSubscription<ProcessSignal>>[];
  if (stop == null) {
    final stopped = Completer<void>();
    for (final signal in [ProcessSignal.sigterm, ProcessSignal.sigint]) {
      signals.add(
        signal.watch().listen((_) {
          if (!stopped.isCompleted) stopped.complete();
        }),
      );
    }
    stop = stopped.future;
  }
  try {
    return await _run(env, out, err, stop);
  } finally {
    for (final s in signals) {
      await s.cancel();
    }
  }
}