filelock for activate.json

This commit is contained in:
Thomas Ciesla
2026-01-08 10:16:28 +01:00
parent 7fe88b27a0
commit 2e7d935603

View File

@@ -31,6 +31,7 @@ import ipaddress
import signal import signal
import platform import platform
import threading import threading
import fcntl
import urllib.request import urllib.request
import urllib.error import urllib.error
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
@@ -2596,13 +2597,6 @@ def add_shop_to_active(shop: str,
monitor_only: Nur Monitoring, kein Blocking monitor_only: Nur Monitoring, kein Blocking
""" """
os.makedirs(os.path.dirname(ACTIVE_SHOPS_FILE), exist_ok=True) os.makedirs(os.path.dirname(ACTIVE_SHOPS_FILE), exist_ok=True)
shops = {}
if os.path.isfile(ACTIVE_SHOPS_FILE):
try:
with open(ACTIVE_SHOPS_FILE, 'r') as f:
shops = json.load(f)
except:
shops = {}
shop_data = { shop_data = {
"activated": datetime.now().isoformat(), "activated": datetime.now().isoformat(),
@@ -2623,16 +2617,39 @@ def add_shop_to_active(shop: str,
if unlimited_countries: if unlimited_countries:
shop_data["unlimited_countries"] = unlimited_countries shop_data["unlimited_countries"] = unlimited_countries
# File Locking für sichere parallele Zugriffe
lock_file = ACTIVE_SHOPS_FILE + '.lock'
with open(lock_file, 'w') as lf:
fcntl.flock(lf, fcntl.LOCK_EX) # Exklusiver Lock - wartet wenn gesperrt
try:
shops = {}
if os.path.isfile(ACTIVE_SHOPS_FILE):
try:
with open(ACTIVE_SHOPS_FILE, 'r') as f:
shops = json.load(f)
except:
shops = {}
shops[shop] = shop_data shops[shop] = shop_data
with open(ACTIVE_SHOPS_FILE, 'w') as f: with open(ACTIVE_SHOPS_FILE, 'w') as f:
json.dump(shops, f, indent=2) json.dump(shops, f, indent=2)
finally:
fcntl.flock(lf, fcntl.LOCK_UN) # Lock freigeben
def remove_shop_from_active(shop: str): def remove_shop_from_active(shop: str):
"""Entfernt einen Shop aus der aktiven Liste.""" """Entfernt einen Shop aus der aktiven Liste."""
if not os.path.isfile(ACTIVE_SHOPS_FILE): if not os.path.isfile(ACTIVE_SHOPS_FILE):
return return
# File Locking für sichere parallele Zugriffe
lock_file = ACTIVE_SHOPS_FILE + '.lock'
with open(lock_file, 'w') as lf:
fcntl.flock(lf, fcntl.LOCK_EX) # Exklusiver Lock - wartet wenn gesperrt
try: try:
if not os.path.isfile(ACTIVE_SHOPS_FILE):
return
with open(ACTIVE_SHOPS_FILE, 'r') as f: with open(ACTIVE_SHOPS_FILE, 'r') as f:
shops = json.load(f) shops = json.load(f)
if shop in shops: if shop in shops:
@@ -2641,6 +2658,8 @@ def remove_shop_from_active(shop: str):
json.dump(shops, f, indent=2) json.dump(shops, f, indent=2)
except: except:
pass pass
finally:
fcntl.flock(lf, fcntl.LOCK_UN) # Lock freigeben
def get_shop_config(shop: str) -> Dict[str, Any]: def get_shop_config(shop: str) -> Dict[str, Any]: