from .handler import BaseHandlerError, BaseHandler
-__version__ = '0.2.2'
+__version__ = '0.2.3'
-log = logging.getLogger(__name__)
+LOG = logging.getLogger(__name__)
# Module variables
DEFAULT_LOCKRETRY_DELAY_START = 0.1
msg = "Closing file descriptor {} ...".format(self.fd)
if self.silent:
if self.verbose >= 2:
- log.debug(msg)
+ LOG.debug(msg)
else:
- log.debug(msg)
+ LOG.debug(msg)
os.close(self.fd)
self._fd = None
msg = "Automatic removing of {!r} ...".format(self.lockfile)
if self.silent:
if self.verbose >= 2:
- log.debug(msg)
+ LOG.debug(msg)
else:
- log.info(msg)
+ LOG.info(msg)
if not self.simulate:
os.remove(self.lockfile)
"""
msg = "Refreshing atime and mtime of {!r} to the current timestamp.".format(self.lockfile)
- log.debug(msg)
+ LOG.debug(msg)
if not self.simulate:
os.utime(self.lockfile, None)
lockfile = os.path.normpath(os.path.join(self.lockdir, lockfile))
lockdir = os.path.dirname(lockfile)
- log.debug("Trying to lock lockfile {!r} ...".format(lockfile))
+ LOG.debug("Trying to lock lockfile {!r} ...".format(lockfile))
if self.verbose > 1:
- log.debug("Using lock directory {!r} ...".format(lockdir))
+ LOG.debug("Using lock directory {!r} ...".format(lockdir))
if not os.path.isdir(lockdir):
raise LockdirNotExistsError(lockdir)
if not os.access(lockdir, os.W_OK):
msg = "Locking directory {!r} isn't writeable.".format(lockdir)
if self.simulate:
- log.error(msg)
+ LOG.error(msg)
else:
raise LockdirNotWriteableError(lockdir)
counter += 1
if self.verbose > 3:
- log.debug("Current time difference: {:0.3f} seconds.".format(time_diff))
+ LOG.debug("Current time difference: {:0.3f} seconds.".format(time_diff))
if time_diff >= max_delay:
break
# Try creating lockfile exclusive
- log.debug("Try {try_nr} on creating lockfile {lfile!r} ...".format(
+ LOG.debug("Try {try_nr} on creating lockfile {lfile!r} ...".format(
try_nr=counter, lfile=lockfile))
ctime = datetime.datetime.utcnow()
fd = self._create_lockfile(lockfile)
if not self.check_lockfile(lockfile, max_age, use_pid):
# No other process is using this lockfile
if os.path.exists(lockfile):
- log.info("Removing lockfile {!r} ...".format(lockfile))
+ LOG.info("Removing lockfile {!r} ...".format(lockfile))
try:
if not self.simulate:
os.remove(lockfile)
except Exception as e:
msg = "Error on removing lockfile {lfile!r): {err}".format(
lfile=lockfile, err=e)
- log.error(msg)
+ LOG.error(msg)
time.sleep(delay)
delay += delay_increase
continue
# No success, then retry later
if self.verbose > 2:
- log.debug("Sleeping for {:0.1f} seconds.".format(float(delay)))
+ LOG.debug("Sleeping for {:0.1f} seconds.".format(float(delay)))
time.sleep(delay)
delay += delay_increase
if raise_on_fail:
raise e
else:
- log.error(msg)
+ LOG.error(msg)
return None
# or an int for success
msg = "Got a lock for lockfile {!r}.".format(lockfile)
if self.silent:
- log.debug(msg)
+ LOG.debug(msg)
else:
- log.info(msg)
+ LOG.info(msg)
out = to_utf8("{}\n".format(pid))
- log.debug("Write {what!r} in lockfile {lfile!r} ...".format(
+ LOG.debug("Write {what!r} in lockfile {lfile!r} ...".format(
what=out, lfile=lockfile))
finally:
os.write(fd, out)
if stay_opened:
- log.debug("Seeking and syncing {!r} ...".format(lockfile))
+ LOG.debug("Seeking and syncing {!r} ...".format(lockfile))
os.lseek(fd, 0, 0)
os.fsync(fd)
else:
- log.debug("Closing {!r} ...".format(lockfile))
+ LOG.debug("Closing {!r} ...".format(lockfile))
os.close(fd)
fd = None
"""
if self.verbose > 1:
- log.debug("Trying to open {!r} exclusive ...".format(lockfile))
+ LOG.debug("Trying to open {!r} exclusive ...".format(lockfile))
if self.simulate:
- log.debug("Simulation mode, no real creation of a lockfile.")
+ LOG.debug("Simulation mode, no real creation of a lockfile.")
return -1
fd = None
msg = "Error on creating lockfile {lfile!r}: {err}".format(
lfile=lockfile, err=e)
if e.errno == errno.EEXIST:
- log.debug(msg)
+ LOG.debug(msg)
return None
else:
error_tuple = sys.exc_info()
lockfile = os.path.normpath(os.path.join(self.lockdir, lockfile))
if not os.path.exists(lockfile):
- log.debug("Lockfile {!r} to remove doesn't exists.".format(lockfile))
+ LOG.debug("Lockfile {!r} to remove doesn't exists.".format(lockfile))
return True
- log.info("Removing lockfile {!r} ...".format(lockfile))
+ LOG.info("Removing lockfile {!r} ...".format(lockfile))
if self.simulate:
- log.debug("Simulation mode - lockfile won't removed.")
+ LOG.debug("Simulation mode - lockfile won't removed.")
return True
try:
os.remove(lockfile)
except Exception as e:
- log.error("Error on removing lockfile {lfile!r}: {err}".format(lfile=lockfile, err=e))
+ LOG.error("Error on removing lockfile {lfile!r}: {err}".format(lfile=lockfile, err=e))
if self.verbose:
tb = traceback.format_exc()
- log.debug("Stacktrace:\n" + tb)
+ LOG.debug("Stacktrace:\n" + tb)
return False
return True
val=max_age, what='max_age')
raise LockHandlerError(msg)
- log.debug("Checking lockfile {!r} ...".format(lockfile))
+ LOG.debug("Checking lockfile {!r} ...".format(lockfile))
if not os.path.exists(lockfile):
if self.verbose > 2:
- log.debug("Lockfile {!r} doesn't exists.".format(lockfile))
+ LOG.debug("Lockfile {!r} doesn't exists.".format(lockfile))
return False
if not os.access(lockfile, os.R_OK):
- log.warn("No read access for lockfile {!r}.".format(lockfile))
+ LOG.warn("No read access for lockfile {!r}.".format(lockfile))
return True
if not os.access(lockfile, os.W_OK):
- log.warn("No write access for lockfile {!r}.".format(lockfile))
+ LOG.warn("No write access for lockfile {!r}.".format(lockfile))
return True
if use_pid:
pid = self.get_pid_from_file(lockfile, True)
if pid is None:
- log.warn("Unusable lockfile {!r}.".format(lockfile))
+ LOG.warn("Unusable lockfile {!r}.".format(lockfile))
else:
if self.dead(pid):
- log.warn("Process with PID {} is unfortunately dead.".format(pid))
+ LOG.warn("Process with PID {} is unfortunately dead.".format(pid))
return False
else:
- log.debug("Process with PID {} is still running.".format(pid))
+ LOG.debug("Process with PID {} is still running.".format(pid))
return True
fstat = None
fstat = os.stat(lockfile)
except OSError as e:
if e.errno == errno.ENOENT:
- log.info("Could not stat for file {lfile!r}: {err}".format(
+ LOG.info("Could not stat for file {lfile!r}: {err}".format(
lfile=lockfile, err=e.strerror))
return False
raise
age = time.time() - fstat.st_mtime
if age >= max_age:
- log.debug("Lockfile {lfile!r} is older than {max} seconds ({age} seconds).".format(
+ LOG.debug("Lockfile {lfile!r} is older than {max} seconds ({age} seconds).".format(
lfile=lockfile, max=max_age, age=age))
return False
msg = "Lockfile {lfile!r} is {age} seconds old, but not old enough ({max}seconds).".format(
lfile=lockfile, max=max_age, age=age)
- log.debug(msg)
+ LOG.debug(msg)
return True
# -------------------------------------------------------------------------
"""
if self.verbose > 1:
- log.debug("Trying to open pidfile {!r} ...".format(pidfile))
+ LOG.debug("Trying to open pidfile {!r} ...".format(pidfile))
try:
fh = open(pidfile, "rb")
except Exception as e:
msg = "Could not open pidfile {!r} for reading: ".format(pidfile)
msg += str(e)
if force:
- log.warn(msg)
+ LOG.warn(msg)
return None
else:
raise LockHandlerError(str(e))
if content == "":
msg = "First line of pidfile {!r} was empty.".format(pidfile)
if force:
- log.warn(msg)
+ LOG.warn(msg)
return None
else:
raise LockHandlerError(msg)
msg = "Could not interprete {cont!r} as a PID from {file!r}: {err}".format(
cont=content, file=pidfile, err=e)
if force:
- log.warn(msg)
+ LOG.warn(msg)
return None
else:
raise LockHandlerError(msg)
if pid <= 0:
msg = "Invalid PID {pid} in {file!r} found.".format(pid=pid, file=pidfile)
if force:
- log.warn(msg)
+ LOG.warn(msg)
return None
else:
raise LockHandlerError(msg)