diff --git a/pyproject.toml b/pyproject.toml index 17d024031..e04492861 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta" [[tool.mypy.overrides]] module = [ "ansible.*", - "daemon.*", + "fasteners.*", "pexpect", ] ignore_missing_imports = true diff --git a/src/ansible_runner/__main__.py b/src/ansible_runner/__main__.py index 49705d611..bbea6ae18 100644 --- a/src/ansible_runner/__main__.py +++ b/src/ansible_runner/__main__.py @@ -34,8 +34,6 @@ from contextlib import contextmanager from uuid import uuid4 -import daemon -from daemon.pidfile import TimeoutPIDLockFile from yaml import safe_dump, safe_load from ansible_runner import run @@ -44,6 +42,7 @@ from ansible_runner.utils import dump_artifact, Bunch, register_for_cleanup from ansible_runner.utils.capacity import get_cpu_count, get_mem_in_bytes, ensure_uuid from ansible_runner.utils.importlib_compat import importlib_metadata +from ansible_runner.utils.locks import TimeoutProcessLock from ansible_runner.runner import Runner from ansible_runner.exceptions import AnsibleRunnerException @@ -824,7 +823,7 @@ def main(sys_args=None): if vargs.get('command') in ('start', 'run', 'transmit', 'worker', 'process'): if vargs.get('command') == 'start': - context = daemon.DaemonContext(pidfile=TimeoutPIDLockFile(pidfile)) + context = TimeoutProcessLock(pidfile).locked(5) else: context = threading.Lock() diff --git a/src/ansible_runner/exceptions.py b/src/ansible_runner/exceptions.py index d2cf705ab..b031e6608 100644 --- a/src/ansible_runner/exceptions.py +++ b/src/ansible_runner/exceptions.py @@ -1,11 +1,14 @@ - class AnsibleRunnerException(Exception): - """ Generic Runner Error """ + """Generic Runner Error""" class ConfigurationError(AnsibleRunnerException): - """ Misconfiguration of Runner """ + """Misconfiguration of Runner""" class CallbackError(AnsibleRunnerException): - """ Exception occurred in Callback """ + """Exception occurred in Callback""" + + +class ProcessLockException(Exception): + """Exception occurred in Locking process using fasteners""" diff --git a/src/ansible_runner/utils/locks.py b/src/ansible_runner/utils/locks.py new file mode 100644 index 000000000..7cc98d160 --- /dev/null +++ b/src/ansible_runner/utils/locks.py @@ -0,0 +1,16 @@ +from contextlib import contextmanager +from fasteners import InterProcessLock +from ansible_runner.exceptions import ProcessLockException + + +class TimeoutProcessLock(InterProcessLock): + + @contextmanager + def locked(self, timeout): + ok = self.acquire(timeout=timeout) + if not ok: + raise ProcessLockException() + try: + yield + finally: + self.release()