05/22 Implement license version
This commit is contained in:
+67
-31
@@ -179,8 +179,12 @@ def create_app(config_name=None):
|
||||
except Exception:
|
||||
_zone = _ZI('UTC')
|
||||
now_local = _dt.now(_tz.utc).astimezone(_zone)
|
||||
license_status = current_app.config.get(
|
||||
'LICENSE_STATUS', {'valid': False, 'tier': 'community', 'reason': 'no_key'}
|
||||
)
|
||||
return dict(unread_notifications=unread, branding=branding,
|
||||
now_local=now_local, app_tz_name=_tz_name)
|
||||
now_local=now_local, app_tz_name=_tz_name,
|
||||
license=license_status)
|
||||
|
||||
# ── Security headers ──────────────────────────────────────────────────────
|
||||
@app.after_request
|
||||
@@ -199,6 +203,27 @@ def create_app(config_name=None):
|
||||
_seed_settings()
|
||||
_seed_ticket_templates()
|
||||
|
||||
# ── License validation ────────────────────────────────────────────────────
|
||||
# Validate on startup and cache the result in app.config so every request
|
||||
# can read it instantly without hitting disk or doing crypto again.
|
||||
# The result is also injected into Jinja2 templates via inject_globals.
|
||||
from app.services.license_service import validate_license
|
||||
_license_status = validate_license(app.config.get('LICENSE_KEY', ''))
|
||||
app.config['LICENSE_STATUS'] = _license_status
|
||||
_lic_tier = _license_status.get('tier', 'community')
|
||||
if _license_status.get('valid'):
|
||||
app.logger.info(
|
||||
f'[LICENSE] {_lic_tier.upper()} license valid for '
|
||||
f'{_license_status.get("customer")} — '
|
||||
f'expires {_license_status.get("expires_at")} '
|
||||
f'({_license_status.get("days_left")} days left)'
|
||||
)
|
||||
else:
|
||||
app.logger.warning(
|
||||
f'[LICENSE] No valid license ({_license_status.get("reason", "unknown")}) — '
|
||||
f'running in Community mode'
|
||||
)
|
||||
|
||||
# ── Background schedulers (SLA + Email Ingestion) ───────────────────────────
|
||||
# Both jobs share a single BackgroundScheduler instance to avoid duplicate
|
||||
# thread pools and simplify startup/shutdown. The scheduler is only started
|
||||
@@ -241,40 +266,51 @@ def _start_background_schedulers(app):
|
||||
|
||||
scheduler = BackgroundScheduler(daemon=True)
|
||||
|
||||
# SLA breach check — every 30 minutes
|
||||
scheduler.add_job(
|
||||
func = check_sla_breaches,
|
||||
trigger = IntervalTrigger(minutes=30),
|
||||
id = 'sla_check',
|
||||
name = 'SLA Breach Check',
|
||||
replace_existing = True,
|
||||
args = [app],
|
||||
)
|
||||
_lic = app.config.get('LICENSE_STATUS', {})
|
||||
_lic_tier = _lic.get('tier', 'community')
|
||||
_is_business = _lic_tier in ('business', 'enterprise')
|
||||
_is_enterprise = _lic_tier == 'enterprise'
|
||||
|
||||
# Inbound email ingestion — interval from SystemSetting
|
||||
scheduler.add_job(
|
||||
func = check_inbound_email,
|
||||
trigger = IntervalTrigger(minutes=email_interval),
|
||||
id = 'email_ingest',
|
||||
name = 'Inbound Email Ingestion',
|
||||
replace_existing = True,
|
||||
args = [app],
|
||||
)
|
||||
# SLA breach check — Business+ only, every 30 minutes
|
||||
if _is_business:
|
||||
scheduler.add_job(
|
||||
func = check_sla_breaches,
|
||||
trigger = IntervalTrigger(minutes=30),
|
||||
id = 'sla_check',
|
||||
name = 'SLA Breach Check',
|
||||
replace_existing = True,
|
||||
args = [app],
|
||||
)
|
||||
|
||||
# Weekly digest — every Monday at 08:00 UTC
|
||||
scheduler.add_job(
|
||||
func = send_weekly_digest,
|
||||
trigger = CronTrigger(day_of_week='mon', hour=8, minute=0),
|
||||
id = 'weekly_digest',
|
||||
name = 'Weekly IT Digest Email',
|
||||
replace_existing = True,
|
||||
args = [app],
|
||||
)
|
||||
# Inbound email ingestion — Enterprise only
|
||||
if _is_enterprise:
|
||||
scheduler.add_job(
|
||||
func = check_inbound_email,
|
||||
trigger = IntervalTrigger(minutes=email_interval),
|
||||
id = 'email_ingest',
|
||||
name = 'Inbound Email Ingestion',
|
||||
replace_existing = True,
|
||||
args = [app],
|
||||
)
|
||||
|
||||
# Weekly digest — Business+ only, every Monday at 08:00 UTC
|
||||
if _is_business:
|
||||
scheduler.add_job(
|
||||
func = send_weekly_digest,
|
||||
trigger = CronTrigger(day_of_week='mon', hour=8, minute=0),
|
||||
id = 'weekly_digest',
|
||||
name = 'Weekly IT Digest Email',
|
||||
replace_existing = True,
|
||||
args = [app],
|
||||
)
|
||||
|
||||
scheduler.start()
|
||||
app.logger.info('[SCHEDULER] APScheduler started — SLA check every 30 min, '
|
||||
f'email ingestion every {email_interval} min, '
|
||||
'weekly digest every Monday 08:00 UTC')
|
||||
app.logger.info(
|
||||
f'[SCHEDULER] APScheduler started — tier={_lic_tier} — '
|
||||
+ ('SLA check every 30 min, ' if _is_business else '')
|
||||
+ (f'email ingestion every {email_interval} min, ' if _is_enterprise else '')
|
||||
+ ('weekly digest every Monday 08:00 UTC' if _is_business else '')
|
||||
)
|
||||
except Exception as exc:
|
||||
app.logger.error(f'[SCHEDULER] Failed to start APScheduler: {exc}')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user