resolveLaunchPid function

Future<int?> resolveLaunchPid(
  1. String vmUri, {
  2. Future<int?> queryVmPid(
    1. String vmUri
    ) = queryVmPidFromUri,
})

Resolves the PID to report once the VM service at vmUri is up.

Prefers the PID files written by the controller. If none is there yet, asks the VM service directly through queryVmPid and saves the answer to appPidFile so fdb status reports the same PID. A PID of 0 or less (DWDS on web reports -1) counts as unknown. Returns null when neither source knows the PID.

Implementation

Future<int?> resolveLaunchPid(
  String vmUri, {
  Future<int?> Function(String vmUri) queryVmPid = queryVmPidFromUri,
}) async {
  final pid = readLaunchPid();
  if (pid != null && pid > 0) return pid;

  final int? vmPid;
  try {
    vmPid = await queryVmPid(vmUri);
  } catch (_) {
    return null;
  }
  if (vmPid == null || vmPid <= 0) return null;

  try {
    File(appPidFile).writeAsStringSync(vmPid.toString());
  } catch (_) {
    // Not fatal: the PID is still reported, status just cannot reuse it.
  }
  return vmPid;
}