From 7c4df23565a438fe318e78c34d45ea7e3e0317d8 Mon Sep 17 00:00:00 2001 From: Josephine-Rutten <144329243+Josephine-Rutten@users.noreply.github.com> Date: Mon, 21 Oct 2024 08:55:41 +0200 Subject: [PATCH] Replace fcntl lib to portalocker (#371) * replace fcntl lib to portalocker * replace the fcntl locker with portalocker to make os independent --------- Co-authored-by: Josephine.Rutten --- requirements.txt | 1 + src/cnaas_nms/scheduler/scheduler.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0640c812..0f0b134b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,3 +36,4 @@ greenlet==3.0.3 pydantic_settings==2.4.0 Authlib==1.3.1 python-jose==3.3.0 +portalocker==2.8.2 diff --git a/src/cnaas_nms/scheduler/scheduler.py b/src/cnaas_nms/scheduler/scheduler.py index a671b242..ac4e5cbe 100644 --- a/src/cnaas_nms/scheduler/scheduler.py +++ b/src/cnaas_nms/scheduler/scheduler.py @@ -1,11 +1,11 @@ import datetime -import fcntl import inspect import json import os from types import FunctionType from typing import Optional, Union +import portalocker from apscheduler.executors.pool import ThreadPoolExecutor from apscheduler.jobstores.memory import MemoryJobStore from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore @@ -36,8 +36,8 @@ def __init__(self): # If scheduler is already started, use uwsgi ipc to send job to mule process self.lock_f = open("/tmp/scheduler.lock", "w") try: - fcntl.lockf(self.lock_f, fcntl.LOCK_EX | fcntl.LOCK_NB) - except BlockingIOError: + portalocker.lock(self.lock_f, flags=portalocker.LOCK_EX | portalocker.LOCK_NB) + except portalocker.exceptions.LockException: try: import uwsgi # noqa: F401 except Exception: @@ -80,7 +80,7 @@ def __init__(self): def __del__(self): if self.lock_f: - fcntl.lockf(self.lock_f, fcntl.LOCK_UN) + portalocker.unlock(self.lock_f) self.lock_f.close() os.unlink("/tmp/scheduler.lock")