From 4fb6a49b3344d3e29d42b19fd3e217d49cacc1d4 Mon Sep 17 00:00:00 2001 From: Andrew Burkett Date: Tue, 3 Dec 2019 11:25:13 -0800 Subject: [PATCH] Changed lock_config to lock_client. Moved locking to run_configuration --- borgmatic/commands/borgmatic.py | 24 ++++++++---------------- borgmatic/config/schema.yaml | 2 +- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index 4b68826e..18be8069 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -48,6 +48,14 @@ def run_configuration(config_filename, config, arguments): ) global_arguments = arguments['global'] + if location.get("lock_client",False): + lock_f = open(config_filename) + try: + fcntl.flock(lock_f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError: + logger.critical("Failed to acquire lock for {}".format(config_filename)) + sys.exit(1) + local_path = location.get('local_path', 'borg') remote_path = location.get('remote_path') borg_environment.initialize(storage) @@ -567,25 +575,9 @@ def main(): # pragma: no cover logger.debug('Ensuring legacy configuration is upgraded') convert.guard_configuration_upgraded(LEGACY_CONFIG_PATH, config_filenames) - locks = [] - for config_filename,config in configs.items(): - lock_config = config.get("location",dict()).get("lock_config",False) - if lock_config: - f = open(config_filename) - try: - fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) - except IOError: - logger.critical("Failed to acquire lock for {}".format(config_filename)) - sys.exit(0) - locks.append(f) - summary_logs = parse_logs + list(collect_configuration_run_summary_logs(configs, arguments)) summary_logs_max_level = max(log.levelno for log in summary_logs) - # this removes the reference to the open files which python will garbage collect the file objects - # and close them - locks = None - for message in ('', 'summary:'): log_record( levelno=summary_logs_max_level, diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index f8aa33bf..d8c663b2 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -32,7 +32,7 @@ map: type: bool desc: Stay in same file system (do not cross mount points). Defaults to false. example: true - lock_config: + lock_client: type: bool desc: Lock config when running borgmatic to prevent multiple instances from running simultaneously example: true