Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import logging
  2. import os
  3. import threading
  4. import time
  5.  
  6. import fasteners
  7. from six.moves import range as compat_range
  8.  
  9. LOG = logging.getLogger(__name__)
  10.  
  11.  
  12. def _walk_maybe_paths(lock_dir, recurse=False, filter_func=os.path.isfile):
  13. if os.path.isdir(lock_dir):
  14. if recurse:
  15. for (dir_path, _dir_names, file_names) in os.walk(lock_dir):
  16. for fn in file_names:
  17. fn_path = os.path.join(dir_path, fn)
  18. if filter_func(fn_path):
  19. yield fn_path
  20. else:
  21. for fn in os.listdir(lock_dir):
  22. fn_path = os.path.join(lock_dir, fn)
  23. if filter_func(fn_path):
  24. yield fn_path
  25.  
  26.  
  27. class CleanLocksJanitor(object):
  28. def __init__(self, lock_dir, clean_delay=60.0,
  29. acquire_timeout=0.1, acquire_attempts=3,
  30. recurse=False, logger=None):
  31. self.lock_dir = lock_dir
  32. self.acquire_timeout = acquire_timeout
  33. self.acquire_attempts = acquire_attempts
  34. self.recurse = recurse
  35. self.dead = threading.Event()
  36. self.clean_delay = clean_delay
  37. self.logger = logger or LOG
  38.  
  39. def _try_clean(self, lk_path):
  40. self.logger.info("Attempting to clean lock path '%s'", lk_path)
  41. lk = fasteners.Lock(lk_path)
  42. for i in compat_range(0, self.acquire_attempts):
  43. gotten = lk.acquire(blocking=True,
  44. timeout=self.acquire_timeout)
  45. if not gotten:
  46. self.logger.debug("Failed acquiring lock path '%s' for the"
  47. " %s time (%s attempts are left)", lk_path,
  48. i + 1, self.acquire_attempts - (i + 1))
  49. try:
  50. if gotten:
  51. try:
  52. st = os.stat(lk_path)
  53. os.unlink(lk_path)
  54. except (OSError, IOError):
  55. self.logger.info("Failed destroying lock path '%s'"
  56. " even after lock acquisition"
  57. " succeeded", exc_info=True)
  58. else:
  59. self.logger.info("Destroyed lock path '%s', it has"
  60. " existed for %0.2f seconds",
  61. lk_path, time.time() - st.st_ctime)
  62. return True
  63. finally:
  64. if gotten:
  65. lk.release()
  66. return False
  67.  
  68. def run_forever(self):
  69. while not self.dead.is_set():
  70. for lk_path in _walk_maybe_paths(self.lock_dir,
  71. recurse=self.recurse):
  72. cleaned = self._try_clean(lk_path)
  73. if cleaned:
  74. self.logger.debug("Cleaned lock path '%s'", lk_path)
  75. self.dead.wait(self.clean_delay)
  76.  
  77.  
  78. def main():
  79. import logging
  80. import sys
  81. logging.basicConfig(level=logging.DEBUG)
  82. cj = CleanLocksJanitor(os.path.abspath(sys.argv[1]))
  83. t = threading.Thread(target=cj.run_forever)
  84. t.start()
  85.  
  86.  
  87. if __name__ == "__main__":
  88. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement