From a7c5ca08066d457695c906bd6348b96a54636b86 Mon Sep 17 00:00:00 2001 From: Thiago da Silva Date: Wed, 15 Aug 2018 21:05:24 +0000 Subject: [PATCH] Fix locking in swift-recon-cron The previous locking method would leave the lock dir lying around if the process died unexpectedly, preventing others swift-recon-cron process from running sucessfuly and requiring a manual clean. Change-Id: Icb328b2766057a2a4d126f63e2d6dfa5163dd223 --- bin/swift-recon-cron | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/bin/swift-recon-cron b/bin/swift-recon-cron index 709bd9d5ec..d750ba4b28 100755 --- a/bin/swift-recon-cron +++ b/bin/swift-recon-cron @@ -19,9 +19,10 @@ swift-recon-cron.py import os import sys -from gettext import gettext as _ +from eventlet import Timeout -from swift.common.utils import get_logger, dump_recon_cache, readconf +from swift.common.utils import get_logger, dump_recon_cache, readconf, \ + lock_path from swift.obj.diskfile import ASYNCDIR_BASE @@ -62,21 +63,14 @@ def main(): conf['log_name'] = conf.get('log_name', 'recon-cron') logger = get_logger(conf, log_route='recon-cron') try: - os.mkdir(lock_dir) - except OSError as e: - logger.critical(str(e)) - print(str(e)) + with lock_path(lock_dir): + asyncs = get_async_count(device_dir, logger) + dump_recon_cache({'async_pending': asyncs}, cache_file, logger) + except (Exception, Timeout) as err: + msg = 'Exception during recon-cron while accessing devices' + logger.exception(msg) + print('%s: %s' % (msg, err)) sys.exit(1) - try: - asyncs = get_async_count(device_dir, logger) - dump_recon_cache({'async_pending': asyncs}, cache_file, logger) - except Exception: - logger.exception( - _('Exception during recon-cron while accessing devices')) - try: - os.rmdir(lock_dir) - except Exception: - logger.exception(_('Exception remove cronjob lock')) if __name__ == '__main__': main()