Skip to content

Commit 20519f6

Browse files
authored
Update zk-flock
adds minimum time for lock
1 parent 06cf651 commit 20519f6

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

zk-flock

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/python2
22
# -*- coding: utf-8 -*-
33
#
44
# Copyright (c) 2012 Tyurin Anton [email protected]
@@ -38,7 +38,6 @@ from functools import partial
3838
import json
3939
import logging
4040
import logging.handlers
41-
import os
4241
import optparse
4342
import shlex
4443
import subprocess
@@ -148,7 +147,7 @@ def notifier(cv):
148147

149148

150149
# ToDo: accept options as the last argument
151-
def main(cmd_arg, zk_cfg, period=None, exitcode=0, sequence=0, pdeathsig_num=0):
150+
def main(cmd_arg, zk_cfg, period=3, exitcode=0, sequence=0, pdeathsig_num=0, minlocktime=5):
152151
try:
153152
z = Zookeeper.ZKLockServer(**cfg)
154153
except Exception as err:
@@ -202,7 +201,8 @@ def main(cmd_arg, zk_cfg, period=None, exitcode=0, sequence=0, pdeathsig_num=0):
202201

203202
cv = Condition()
204203

205-
def sigterm_and_sigchld_handle(p, signum, frame):
204+
def sigterm_and_sigchld_handle(p, signum, frame, minlocktime=5):
205+
os.system("sleep %d" % minlocktime)
206206
# TBD - split by two handlers for each signal
207207
if signum == signal.SIGTERM:
208208
LOGGER.info("Stop work by SIGTERM")
@@ -260,7 +260,7 @@ def main(cmd_arg, zk_cfg, period=None, exitcode=0, sequence=0, pdeathsig_num=0):
260260

261261

262262
if __name__ == "__main__":
263-
usage = "Usage: %prog LOCKNAME COMMAND [-cdhs]"
263+
usage = "Usage: %prog LOCKNAME COMMAND [-cdhsl]"
264264
parser = optparse.OptionParser(usage)
265265
parser.add_option("-c", "--confpath", action="store",
266266
dest="confpath", default="/etc/distributed-flock.json",
@@ -289,6 +289,10 @@ if __name__ == "__main__":
289289
dest="sequence", default=0,
290290
help="Sequence of locks")
291291

292+
parser.add_option("-l", "--minlocktime", action="store", type=int,
293+
dest="minlocktime", default=5,
294+
help="Minimum time for lock")
295+
292296
if pdeathsig.support_pdeathsig():
293297
parser.add_option("-p", "--pdeathsig", action="store", type=int,
294298
dest="pdeathsig", default=signal.SIGTERM,
@@ -339,7 +343,7 @@ if __name__ == "__main__":
339343
daemon = Daemon()
340344
daemon.run = main
341345
daemon.start(cmd_arg, cfg, options.waittime,
342-
options.exitcode, options.sequence, pdeathsig_num)
346+
options.exitcode, options.sequence, pdeathsig_num, options.minlocktime)
343347
else:
344348
main(cmd_arg, cfg, options.waittime,
345-
options.exitcode, options.sequence, pdeathsig_num)
349+
options.exitcode, options.sequence, pdeathsig_num, options.minlocktime)

0 commit comments

Comments
 (0)