From b03b48d44bf6245a61d8c3458d09361ec9200a29 Mon Sep 17 00:00:00 2001 From: Matthias Klumpp Date: Sat, 26 Aug 2023 20:30:55 +0200 Subject: [PATCH] synchrotron: Print nicer message if lock can not be acquired Resolves: #25 --- src/laniakea/utils/__init__.py | 2 ++ src/laniakea/utils/misc.py | 6 +++++- src/spears/excuses.py | 2 +- src/synchrotron/cli.py | 25 +++++++++++++++++-------- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/laniakea/utils/__init__.py b/src/laniakea/utils/__init__.py index afb39067..23d5b7ac 100644 --- a/src/laniakea/utils/__init__.py +++ b/src/laniakea/utils/__init__.py @@ -6,6 +6,7 @@ from laniakea.utils.json import json_compact_dump from laniakea.utils.misc import ( + LockError, cd, listify, stringify, @@ -46,6 +47,7 @@ 'safe_run', 'run_forwarded', 'safe_run_forwarded', + 'LockError', 'cd', 'listify', 'stringify', diff --git a/src/laniakea/utils/misc.py b/src/laniakea/utils/misc.py index 40610de4..dfea6d16 100644 --- a/src/laniakea/utils/misc.py +++ b/src/laniakea/utils/misc.py @@ -123,6 +123,10 @@ def datetime_to_rfc2822_string(dt: datetime): return utils.format_datetime(dt) +class LockError(Exception): + """An error happened while acquiring a file lock.""" + + class ProcessFileLock: """ Simple wy to prevent multiple processes from executing the same code via a file lock. @@ -169,7 +173,7 @@ def acquire(self, raise_error=True) -> bool: self._lock_file_fd = -1 os.close(fd) if raise_error: - raise Exception( + raise LockError( 'Unable to acquire lock "{}": Lock held by other instance or thread!'.format(self.lock_filename) ) return False diff --git a/src/spears/excuses.py b/src/spears/excuses.py index f32a8dce..b95ac6f0 100644 --- a/src/spears/excuses.py +++ b/src/spears/excuses.py @@ -91,7 +91,7 @@ def get_excuses(self) -> Dict[str, SpearsExcuse]: 'Additional info:', ' days old (needed ', ) - debian_buildd_link_re = r'([^<]+)' + debian_buildd_link_re = r'([^<]+)' # noqa: E501 # get log data loginfo = self._process_log_data() diff --git a/src/synchrotron/cli.py b/src/synchrotron/cli.py index 99d06a01..5906bbfa 100644 --- a/src/synchrotron/cli.py +++ b/src/synchrotron/cli.py @@ -9,6 +9,7 @@ from argparse import ArgumentParser from laniakea.db import SynchrotronConfig, session_scope +from laniakea.utils import LockError from laniakea.logging import log from .syncengine import SyncEngine, SyncSetupError @@ -26,12 +27,16 @@ def command_sync(options): try: engine = SyncEngine(options.repo_name, options.dest_suite, options.src_os, options.src_suite) except SyncSetupError as e: - print('Unable to setup synchronization:', str(e)) + print('Unable to setup synchronization:', str(e), file=sys.stderr) sys.exit(1) - ret = engine.sync_packages(options.component, options.packages, options.force) - if not ret: - sys.exit(2) + try: + ret = engine.sync_packages(options.component, options.packages, options.force) + if not ret: + sys.exit(2) + except LockError as e: + print('Locking failed:', str(e), file=sys.stderr) + sys.exit(4) def command_autosync(options): @@ -65,12 +70,16 @@ def command_autosync(options): autosync.source.suite_name, ) except SyncSetupError as e: - print('Unable to setup synchronization:', str(e)) + print('Unable to setup synchronization:', str(e), file=sys.stderr) sys.exit(1) - ret = engine.autosync(autosync.auto_cruft_remove) - if not ret: - sys.exit(2) + try: + ret = engine.autosync(autosync.auto_cruft_remove) + if not ret: + sys.exit(2) + except LockError as e: + print('Locking failed:', str(e), file=sys.stderr) + sys.exit(4) # commit pending changes session.commit()