Namenskonsistenz + Ablauf der Lizenzen
Dieser Commit ist enthalten in:
@@ -5,7 +5,7 @@ import logging
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
import config
|
||||
from utils.backup import create_backup
|
||||
from utils.db_utils import get_connection
|
||||
from db import get_connection
|
||||
|
||||
|
||||
def scheduled_backup():
|
||||
@@ -64,6 +64,66 @@ def cleanup_expired_sessions():
|
||||
conn.rollback()
|
||||
|
||||
|
||||
def deactivate_expired_licenses():
|
||||
"""Deactivate licenses that have expired"""
|
||||
try:
|
||||
conn = get_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
# Find active licenses that have expired
|
||||
# Check valid_until < today (at midnight)
|
||||
cur.execute("""
|
||||
SELECT id, license_key, customer_id, valid_until
|
||||
FROM licenses
|
||||
WHERE is_active = true
|
||||
AND valid_until IS NOT NULL
|
||||
AND valid_until < CURRENT_DATE
|
||||
AND is_fake = false
|
||||
""")
|
||||
|
||||
expired_licenses = cur.fetchall()
|
||||
|
||||
if expired_licenses:
|
||||
logging.info(f"Found {len(expired_licenses)} expired licenses to deactivate")
|
||||
|
||||
for license in expired_licenses:
|
||||
license_id, license_key, customer_id, valid_until = license
|
||||
|
||||
# Deactivate the license
|
||||
cur.execute("""
|
||||
UPDATE licenses
|
||||
SET is_active = false,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = %s
|
||||
""", (license_id,))
|
||||
|
||||
# Log to audit trail
|
||||
cur.execute("""
|
||||
INSERT INTO audit_log
|
||||
(timestamp, username, action, entity_type, entity_id,
|
||||
old_values, new_values, additional_info)
|
||||
VALUES (CURRENT_TIMESTAMP, 'system', 'DEACTIVATE', 'license', %s,
|
||||
jsonb_build_object('is_active', true),
|
||||
jsonb_build_object('is_active', false),
|
||||
%s)
|
||||
""", (license_id, f"License automatically deactivated due to expiration. Valid until: {valid_until}"))
|
||||
|
||||
logging.info(f"Deactivated expired license: {license_key} (ID: {license_id})")
|
||||
|
||||
conn.commit()
|
||||
logging.info(f"Successfully deactivated {len(expired_licenses)} expired licenses")
|
||||
else:
|
||||
logging.debug("No expired licenses found to deactivate")
|
||||
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error deactivating expired licenses: {str(e)}")
|
||||
if 'conn' in locals():
|
||||
conn.rollback()
|
||||
|
||||
|
||||
def init_scheduler():
|
||||
"""Initialize and configure the scheduler"""
|
||||
scheduler = BackgroundScheduler()
|
||||
@@ -87,8 +147,19 @@ def init_scheduler():
|
||||
replace_existing=True
|
||||
)
|
||||
|
||||
# Configure license expiration job - runs daily at midnight
|
||||
scheduler.add_job(
|
||||
deactivate_expired_licenses,
|
||||
'cron',
|
||||
hour=0,
|
||||
minute=0,
|
||||
id='license_expiration_check',
|
||||
replace_existing=True
|
||||
)
|
||||
|
||||
scheduler.start()
|
||||
logging.info(f"Scheduler started. Daily backup scheduled at {config.SCHEDULER_CONFIG['backup_hour']:02d}:{config.SCHEDULER_CONFIG['backup_minute']:02d}")
|
||||
logging.info("Session cleanup job scheduled to run every 60 seconds")
|
||||
logging.info("License expiration check scheduled to run daily at midnight")
|
||||
|
||||
return scheduler
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren