update.py: add an option --timeout for lockfile

We have an update.py running periodically in background, but we also
need to run it manually, for example, run it to update actual_branch,
the manually run usually failed because can't get lockfile, we have to
run it again and again. A timeout option helps a lot in such a case. Now
the following command can make sure we can run the command successfully:

$ update.py -b master -a actual_branch -t 2000

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Robert Yang 2018-04-03 16:19:53 +08:00 committed by Paul Eggleton
parent f4f2146370
commit f7e63f1814
2 changed files with 15 additions and 4 deletions

View File

@ -156,6 +156,9 @@ def main():
parser.add_option("-l", "--layer", parser.add_option("-l", "--layer",
help = "Specify layers to update (use commas to separate multiple). Default is all published layers.", help = "Specify layers to update (use commas to separate multiple). Default is all published layers.",
action="store", dest="layers") action="store", dest="layers")
parser.add_option("-t", "--timeout",
help = "Specify timeout in seconds to get layerindex.lock. Default is 30 seconds.",
type="int", action="store", dest="timeout", default=30)
parser.add_option("-r", "--reload", parser.add_option("-r", "--reload",
help = "Reload recipe data instead of updating since last update", help = "Reload recipe data instead of updating since last update",
action="store_true", dest="reload") action="store_true", dest="reload")
@ -265,7 +268,7 @@ def main():
update.save() update.save()
try: try:
lockfn = os.path.join(fetchdir, "layerindex.lock") lockfn = os.path.join(fetchdir, "layerindex.lock")
lockfile = utils.lock_file(lockfn) lockfile = utils.lock_file(lockfn, options.timeout, logger)
if not lockfile: if not lockfile:
logger.error("Layer index lock timeout expired") logger.error("Layer index lock timeout expired")
sys.exit(1) sys.exit(1)

View File

@ -309,8 +309,10 @@ class ListHandler(logging.Handler):
return log return log
def lock_file(fn): def lock_file(fn, timeout=30, logger=None):
starttime = time.time() start = time.time()
last = start
counter = 1
while True: while True:
lock = open(fn, 'w') lock = open(fn, 'w')
try: try:
@ -318,8 +320,14 @@ def lock_file(fn):
return lock return lock
except IOError: except IOError:
lock.close() lock.close()
if time.time() - starttime > 30: current = time.time()
if current - start > timeout:
return None return None
# Print a message in every 5 seconds
if logger and (current - last > 5):
last = current
logger.info('Trying to get lock on %s (tried %s seconds) ...' % (fn, (5 * counter)))
counter += 1
def unlock_file(lock): def unlock_file(lock):
fcntl.flock(lock, fcntl.LOCK_UN) fcntl.flock(lock, fcntl.LOCK_UN)