diff --git a/requirements-test.txt b/requirements-test.txt --- a/requirements-test.txt +++ b/requirements-test.txt @@ -4,3 +4,4 @@ types-click types-pyyaml types-requests +types-filelock diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ beautifulsoup4 launchpadlib tenacity +filelock diff --git a/swh/lister/opam/lister.py b/swh/lister/opam/lister.py --- a/swh/lister/opam/lister.py +++ b/swh/lister/opam/lister.py @@ -9,6 +9,8 @@ from subprocess import PIPE, Popen, call from typing import Any, Dict, Iterator, Optional +from filelock import FileLock, Timeout + from swh.lister.pattern import StatelessLister from swh.scheduler.interface import SchedulerInterface from swh.scheduler.model import ListedOrigin @@ -137,5 +139,13 @@ instance, url, ] - # Actually execute the command - call(command, env=env) + try: + lock_name = "swh-lister-opam-maintenance.txt" + lock_path = os.path.join(opam_root, lock_name) + with FileLock(lock_path).acquire(timeout=10): + open(lock_path, "a").write("maintenance update") + # Actually execute the command + call(command, env=env) + except Timeout: + logger.warning("Another instance of this application currently holds the lock.") + raise