perf: avoid importing posthog if not required

Avoids 7MB of overhead and cleanup costs for each background job 🎉
This commit is contained in:
Ankush Menat 2024-03-15 10:50:18 +05:30
parent 8e3175f3e8
commit 322671766d
3 changed files with 12 additions and 11 deletions

View file

@ -441,7 +441,7 @@ after_job = [
"frappe.recorder.dump",
"frappe.monitor.stop",
"frappe.utils.file_lock.release_document_locks",
"frappe.utils.telemetry.flush",
"frappe.utils.background_jobs.flush_telemetry",
]
extend_bootinfo = [

View file

@ -4,6 +4,7 @@ import socket
import time
from collections import defaultdict
from collections.abc import Callable
from contextlib import suppress
from functools import lru_cache
from typing import Any, NoReturn
from uuid import uuid4
@ -606,6 +607,16 @@ def truncate_failed_registry(job, connection, type, value, traceback):
job_obj and fail_registry.remove(job_obj, delete_job=True)
def flush_telemetry():
"""Forcefully flush pending events.
This is required in context of background jobs where process might die before posthog gets time
to push events."""
ph = getattr(frappe.local, "posthog", None)
with suppress(Exception):
ph and ph.flush()
def _start_sentry():
sentry_dsn = os.getenv("FRAPPE_SENTRY_DSN")
if not sentry_dsn:

View file

@ -60,16 +60,6 @@ def capture(event, app, **kwargs):
ph and ph.capture(distinct_id=frappe.local.site, event=f"{app}_{event}", **kwargs)
def flush():
"""Forcefully flush pending events.
This is required in context of background jobs where process might die before posthog gets time
to push events."""
ph: Posthog = getattr(frappe.local, "posthog", None)
with suppress(Exception):
ph and ph.flush()
def capture_doc(doc, action):
with suppress(Exception):
age = site_age()