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
This commit is contained in:
Thiago da Silva 2018-08-15 21:05:24 +00:00 committed by Tim Burke
parent 75bfc79d2d
commit a7c5ca0806

View File

@ -19,9 +19,10 @@ swift-recon-cron.py
import os import os
import sys 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 from swift.obj.diskfile import ASYNCDIR_BASE
@ -62,21 +63,14 @@ def main():
conf['log_name'] = conf.get('log_name', 'recon-cron') conf['log_name'] = conf.get('log_name', 'recon-cron')
logger = get_logger(conf, log_route='recon-cron') logger = get_logger(conf, log_route='recon-cron')
try: try:
os.mkdir(lock_dir) with lock_path(lock_dir):
except OSError as e: asyncs = get_async_count(device_dir, logger)
logger.critical(str(e)) dump_recon_cache({'async_pending': asyncs}, cache_file, logger)
print(str(e)) except (Exception, Timeout) as err:
msg = 'Exception during recon-cron while accessing devices'
logger.exception(msg)
print('%s: %s' % (msg, err))
sys.exit(1) 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__': if __name__ == '__main__':
main() main()