04/06 adding some additional functions
This commit is contained in:
@@ -144,14 +144,59 @@ def create_app(config_name=None):
|
||||
_seed_admin(app)
|
||||
_seed_settings()
|
||||
|
||||
# ── SLA background scheduler ──────────────────────────────────────────────
|
||||
# APScheduler runs inside the gunicorn worker process (single-worker
|
||||
# eventlet setup), so no cross-process coordination is needed.
|
||||
# The scheduler is only started in the main process — not during Flask's
|
||||
# reloader child process — to prevent duplicate job execution.
|
||||
_start_sla_scheduler(app)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def _start_sla_scheduler(app):
|
||||
"""Start the APScheduler background job that checks for SLA breaches.
|
||||
|
||||
Safe to call on every app startup: the scheduler is idempotent and
|
||||
jobstore deduplication prevents double-registration on hot-reloads.
|
||||
Under gunicorn with preload_app=False each worker calls create_app()
|
||||
once, so there is exactly one scheduler per worker.
|
||||
"""
|
||||
# Skip inside Flask's reloader subprocess (identified by the env var it sets).
|
||||
import os as _os
|
||||
if _os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
|
||||
# Reloader is active — the child process will start its own scheduler.
|
||||
return
|
||||
|
||||
try:
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from apscheduler.triggers.interval import IntervalTrigger
|
||||
from app.services.sla_service import check_sla_breaches
|
||||
|
||||
scheduler = BackgroundScheduler(daemon=True)
|
||||
scheduler.add_job(
|
||||
func = check_sla_breaches,
|
||||
trigger = IntervalTrigger(minutes=30),
|
||||
id = 'sla_check',
|
||||
name = 'SLA Breach Check',
|
||||
replace_existing = True,
|
||||
args = [app],
|
||||
)
|
||||
scheduler.start()
|
||||
app.logger.info('[SLA] APScheduler started — SLA breach check every 30 minutes')
|
||||
except Exception as exc:
|
||||
app.logger.error(f'[SLA] Failed to start APScheduler: {exc}')
|
||||
|
||||
|
||||
def _seed_settings():
|
||||
"""Ensure all required system settings exist with safe defaults."""
|
||||
from app.models import SystemSetting
|
||||
defaults = [
|
||||
('registration_enabled', 'true', 'Allow new users to self-register via /auth/register'),
|
||||
('sla_critical_hours', '4', 'Hours before a CRITICAL ticket is considered overdue'),
|
||||
('sla_high_hours', '8', 'Hours before a HIGH ticket is considered overdue'),
|
||||
('sla_medium_hours', '48', 'Hours before a MEDIUM ticket is considered overdue'),
|
||||
('sla_low_hours', '120', 'Hours before a LOW ticket is considered overdue'),
|
||||
('app_name', 'TechDesk', 'Application name shown in the sidebar and page titles'),
|
||||
('app_subtitle', 'IT Helpdesk System', 'Subtitle shown below the app name in the sidebar'),
|
||||
('company_name', '', 'Company name shown on the login and register pages'),
|
||||
|
||||
Reference in New Issue
Block a user