perf: avoid importing posthog if not required
Avoids 7MB of overhead and cleanup costs for each background job 🎉
This commit is contained in:
parent
8e3175f3e8
commit
322671766d
3 changed files with 12 additions and 11 deletions
|
|
@ -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 = [
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue