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,24 +2617,49 @@ 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
shops[shop] = shop_data # File Locking für sichere parallele Zugriffe
with open(ACTIVE_SHOPS_FILE, 'w') as f: lock_file = ACTIVE_SHOPS_FILE + '.lock'
json.dump(shops, f, indent=2) 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
with open(ACTIVE_SHOPS_FILE, 'w') as f:
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
try:
with open(ACTIVE_SHOPS_FILE, 'r') as f: # File Locking für sichere parallele Zugriffe
shops = json.load(f) lock_file = ACTIVE_SHOPS_FILE + '.lock'
if shop in shops: with open(lock_file, 'w') as lf:
del shops[shop] fcntl.flock(lf, fcntl.LOCK_EX) # Exklusiver Lock - wartet wenn gesperrt
with open(ACTIVE_SHOPS_FILE, 'w') as f: try:
json.dump(shops, f, indent=2) if not os.path.isfile(ACTIVE_SHOPS_FILE):
except: return
pass with open(ACTIVE_SHOPS_FILE, 'r') as f:
shops = json.load(f)
if shop in shops:
del shops[shop]
with open(ACTIVE_SHOPS_FILE, 'w') as f:
json.dump(shops, f, indent=2)
except:
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]: