perf: long-lived posthog threads (#31821)

* build: bump posthog to latest

* perf: Use long living posthog client
This commit is contained in:
Ankush Menat 2025-03-20 12:36:17 +05:30 committed by GitHub
parent 5eabc7ebac
commit 4fac934cec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 13 deletions

View file

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

View file

@ -718,16 +718,6 @@ 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.shutdown()
def _check_queue_size(q: Queue):
max_jobs = cint(frappe.conf.max_queued_jobs) or MAX_QUEUED_JOBS
# Workaround for arbitrarily sized benches,

View file

@ -5,6 +5,7 @@ removed without any warning.
"""
from contextlib import suppress
from functools import lru_cache
import frappe
from frappe.utils import getdate
@ -51,7 +52,22 @@ def init_telemetry():
return
with suppress(Exception):
frappe.local.posthog = Posthog(posthog_project_id, host=posthog_host, timeout=5, max_retries=3)
frappe.local.posthog = _get_posthog_instance(posthog_project_id, posthog_host)
# Background jobs might exit before flushing telemetry, so explicitly flush queue
if frappe.job:
frappe.job.after_job.add(flush_telemetry)
@lru_cache
def _get_posthog_instance(project_id, host):
return Posthog(project_id, host=host, timeout=5, max_retries=3)
def flush_telemetry():
ph: Posthog = getattr(frappe.local, "posthog", None)
with suppress(Exception):
ph and ph.flush()
def capture(event, app, **kwargs):

View file

@ -89,7 +89,7 @@ dependencies = [
"google-api-python-client~=2.2.0",
"google-auth-oauthlib~=0.4.4",
"google-auth~=1.29.0",
"posthog~=3.0.1",
"posthog~=3.21.0",
"vobject~=0.9.7",
"pycountry~=24.6.1",
]