cleanupStaleIdeLockfiles method

Future<void> cleanupStaleIdeLockfiles()

Cleans up stale IDE lockfiles.

Implementation

Future<void> cleanupStaleIdeLockfiles() async {
  try {
    final lockfiles = await getSortedIdeLockfiles();

    for (final lockfilePath in lockfiles) {
      final lockfileInfo = await readIdeLockfile(lockfilePath);

      if (lockfileInfo == null) {
        try {
          await File(lockfilePath).delete();
        } catch (e) {
          _logError(e);
        }
        continue;
      }

      final host = await detectHostIP(
        lockfileInfo.runningInWindows,
        lockfileInfo.port,
      );
      bool shouldDelete = false;

      if (lockfileInfo.pid != null) {
        if (!isProcessRunning(lockfileInfo.pid!)) {
          shouldDelete = true;
        }
      } else {
        final isResponding = await checkIdeConnection(
          host,
          lockfileInfo.port,
        );
        if (!isResponding) {
          shouldDelete = true;
        }
      }

      if (shouldDelete) {
        try {
          await File(lockfilePath).delete();
        } catch (e) {
          _logError(e);
        }
      }
    }
  } catch (e) {
    _logError(e);
  }
}