observe method

Returns a point-in-time observation of this lock.

Observation does not acquire the lock and cannot prevent another process from acquiring or releasing it. Do not use this method to coordinate access to a resource; use withLockAsync for that purpose.

A lock is reported as held only when its owner PID and process-start identity identify a currently running process. Orphaned and malformed lock files are ignored but are not deleted by this read-only operation.

Implementation

NamedLockObservation observe() {
  if (!exists(_lockPath)) {
    return NamedLockObservation.notHeld(name: name);
  }

  final locks = find(
    '*.$name',
    workingDirectory: _lockPath,
    includeHidden: true,
    recursive: false,
  ).toList();

  for (final lock in locks) {
    final parts = _lockFileParts(lock);
    if (parts == null || !_isOwnerLive(lock, parts.pid)) {
      continue;
    }

    final lockStat = stat(lock);
    if (lockStat.type == FileSystemEntityType.notFound) {
      continue;
    }
    return NamedLockObservation.held(
      name: name,
      ownerPid: parts.pid,
      ownerIsolateId: parts.isolateId,
      acquiredAt: lockStat.modified,
    );
  }

  return NamedLockObservation.notHeld(name: name);
}