diff --git a/frappe/hooks.py b/frappe/hooks.py index fdbdeda475..e1a43cbf58 100644 --- a/frappe/hooks.py +++ b/frappe/hooks.py @@ -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 = [ diff --git a/frappe/utils/background_jobs.py b/frappe/utils/background_jobs.py index 55ef61eeb6..480ee116c1 100644 --- a/frappe/utils/background_jobs.py +++ b/frappe/utils/background_jobs.py @@ -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, diff --git a/frappe/utils/telemetry.py b/frappe/utils/telemetry.py index 0490d98aa0..6578ca9319 100644 --- a/frappe/utils/telemetry.py +++ b/frappe/utils/telemetry.py @@ -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): diff --git a/pyproject.toml b/pyproject.toml index 8f6a7e07ee..ecde916cea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", ]