From 2b43901bb9bb4a64bbfb89375c5361f68af3e781 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 4 May 2022 17:05:31 +0530 Subject: [PATCH] Remove python-daemon dependency remove python-daemon since it uses unsupported dependency 'lockfile' Fixes: #379 Signed-off-by: Abhijeet Kasurde --- ansible_runner/__main__.py | 5 ++--- ansible_runner/exceptions.py | 11 +++++++---- ansible_runner/utils/locks.py | 16 ++++++++++++++++ requirements.txt | 2 +- tools/requirements.txt | 2 +- 5 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 ansible_runner/utils/locks.py diff --git a/ansible_runner/__main__.py b/ansible_runner/__main__.py index 2c38b8320..9e1f69481 100644 --- a/ansible_runner/__main__.py +++ b/ansible_runner/__main__.py @@ -42,6 +42,7 @@ from ansible_runner import cleanup 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.locks import TimeoutProcessLock from ansible_runner.runner import Runner from ansible_runner.exceptions import AnsibleRunnerException @@ -814,9 +815,7 @@ def main(sys_args=None): if vargs.get('command') in ('start', 'run', 'transmit', 'worker', 'process'): if vargs.get('command') == 'start': - import daemon - from daemon.pidfile import TimeoutPIDLockFile - context = daemon.DaemonContext(pidfile=TimeoutPIDLockFile(pidfile)) + context = TimeoutProcessLock(pidfile).locked(5) else: context = threading.Lock() diff --git a/ansible_runner/exceptions.py b/ansible_runner/exceptions.py index d2cf705ab..b031e6608 100644 --- a/ansible_runner/exceptions.py +++ b/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/ansible_runner/utils/locks.py b/ansible_runner/utils/locks.py new file mode 100644 index 000000000..7cc98d160 --- /dev/null +++ b/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() diff --git a/requirements.txt b/requirements.txt index 09cf067a3..59aecbf5f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ pexpect>=4.5 packaging -python-daemon +fasteners>=0.16 pyyaml six diff --git a/tools/requirements.txt b/tools/requirements.txt index 73ba04fbf..c38d2ef75 100644 --- a/tools/requirements.txt +++ b/tools/requirements.txt @@ -6,6 +6,6 @@ pypsrp[kerberos,credssp] pywinrm[kerberos,credssp] toml pexpect>=4.5 -python-daemon +fasteners>=0.16 pyyaml six