- def available(self) -> bool:
- """Is it available currently?"""
- return not os.path.exists(self.lockfile)
+ def _try_acquire_local_filesystem_lock(self) -> bool:
+ """Attempt to create the lockfile. These flags cause os.open
+ to raise an OSError if the file already exists.
+ """
+ try:
+ logger.debug("Trying to acquire local lock %s.", self.lockfile)
+ fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
+ with os.fdopen(fd, "a") as f:
+ contents = self._construct_local_lockfile_contents()
+ logger.debug(contents)
+ f.write(contents)
+ return True
+ except OSError:
+ logger.warning("Couldn't acquire local lock %s.", self.lockfile)
+ return False
+
+ def _try_acquire_zk_lock(self) -> bool:
+ assert self.expiration_timestamp
+ self.zk_lease = zookeeper.RenewableReleasableLease(
+ self.zk_client,
+ self.lockfile,
+ datetime.timedelta(seconds=self.expiration_timestamp),
+ f"Pyutils lockfile pid={os.getpid()}",
+ )
+ return self.zk_lease