Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Last active November 22, 2020 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyaevseev/feab9a42e91b55cd063ccadd08a1e0e8 to your computer and use it in GitHub Desktop.
Save ilyaevseev/feab9a42e91b55cd063ccadd08a1e0e8 to your computer and use it in GitHub Desktop.
Delete old locks from Zookeeper
#!/usr/bin/python
import os
MAX_DEL = int(os.getenv('MAX_DEL','10'))
MAX_AGE = int(os.getenv('MAX_AGE', '7'))
DRY_RUN = int(os.getenv('DRY_RUN', '0'))
QUIET = int(os.getenv('QUIET', '0'))
import time
now = time.time()
import logging
logging.basicConfig()
from kazoo.client import KazooClient
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
locks = zk.get_children("/locks")
numDeleted = 0
for i, lkname in enumerate(locks):
if numDeleted >= MAX_DEL: break
lk = zk.get('/locks/' + lkname)
lkstat = lk[1]
nc = lkstat.numChildren
if (len(lk) != 2) or (nc != 0 and nc != 2) or (lkstat.ctime != lkstat.mtime):
print(i, "Invalid lock", lkname, lkstat)
continue
if now - (lkstat.ctime / 1000) <= MAX_AGE * 86400:
continue
if QUIET == 0: print(i, "Delete", numDeleted, lkname, lkstat)
if DRY_RUN == 0: zk.delete('/locks/' + lkname, recursive=True)
numDeleted += 1
print("Total %d locks, deleted %s locks." % (len(locks), numDeleted))
zk.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment