|
| 1 | +""" |
| 2 | +Utilities for passing pghistory context to Celery tasks. |
| 3 | +
|
| 4 | +pghistory uses thread-local storage, so context is lost when tasks run |
| 5 | +in Celery workers. These utilities allow capturing context in the sender |
| 6 | +process and recreating it in the worker. |
| 7 | +""" |
| 8 | +import uuid |
| 9 | +from contextlib import nullcontext |
| 10 | + |
| 11 | +from pghistory import runtime as pghistory_runtime |
| 12 | + |
| 13 | + |
| 14 | +def get_serializable_pghistory_context(): |
| 15 | + """ |
| 16 | + Capture the current pghistory context for passing to Celery tasks. |
| 17 | +
|
| 18 | + Returns a JSON-serializable dict with context id and metadata, |
| 19 | + or None if no context is active. |
| 20 | + """ |
| 21 | + if hasattr(pghistory_runtime._tracker, "value"): |
| 22 | + ctx = pghistory_runtime._tracker.value |
| 23 | + return { |
| 24 | + "id": str(ctx.id), |
| 25 | + "metadata": ctx.metadata.copy(), |
| 26 | + } |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +class PgHistoryContextFromTask: |
| 31 | + |
| 32 | + """ |
| 33 | + Context manager to apply pghistory context received from a Celery task. |
| 34 | +
|
| 35 | + This recreates the exact same context (with the same UUID) that was |
| 36 | + active when the task was dispatched, ensuring all events share the |
| 37 | + same pgh_context_id. |
| 38 | +
|
| 39 | + Usage: |
| 40 | + pgh_context = kwargs.pop("_pgh_context", None) |
| 41 | + with PgHistoryContextFromTask(pgh_context): |
| 42 | + # Task body runs here with context applied |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, context_data): |
| 46 | + """ |
| 47 | + Initialize with context data from Celery kwargs. |
| 48 | +
|
| 49 | + Args: |
| 50 | + context_data: Dict with "id" (UUID string) and "metadata" (dict), |
| 51 | + or None for no-op behavior. |
| 52 | +
|
| 53 | + """ |
| 54 | + self.context_data = context_data |
| 55 | + self._pre_execute_hook = None |
| 56 | + self._owns_context = False |
| 57 | + |
| 58 | + def __enter__(self): |
| 59 | + if not self.context_data: |
| 60 | + return None |
| 61 | + |
| 62 | + from django.db import connection # noqa: PLC0415 |
| 63 | + |
| 64 | + context_id = uuid.UUID(self.context_data["id"]) |
| 65 | + metadata = self.context_data["metadata"] |
| 66 | + |
| 67 | + # Only create a new context if one doesn't already exist |
| 68 | + if not hasattr(pghistory_runtime._tracker, "value"): |
| 69 | + self._pre_execute_hook = connection.execute_wrapper( |
| 70 | + pghistory_runtime._inject_history_context, |
| 71 | + ) |
| 72 | + self._pre_execute_hook.__enter__() |
| 73 | + pghistory_runtime._tracker.value = pghistory_runtime.Context( |
| 74 | + id=context_id, |
| 75 | + metadata=metadata, |
| 76 | + ) |
| 77 | + self._owns_context = True |
| 78 | + else: |
| 79 | + # Context already exists, just merge metadata |
| 80 | + pghistory_runtime._tracker.value.metadata.update(metadata) |
| 81 | + |
| 82 | + return pghistory_runtime._tracker.value |
| 83 | + |
| 84 | + def __exit__(self, *exc): |
| 85 | + if self._owns_context and self._pre_execute_hook: |
| 86 | + delattr(pghistory_runtime._tracker, "value") |
| 87 | + self._pre_execute_hook.__exit__(*exc) |
| 88 | + |
| 89 | + |
| 90 | +def get_pghistory_context_manager(context_data): |
| 91 | + """ |
| 92 | + Return appropriate context manager for the given context data. |
| 93 | +
|
| 94 | + Returns PgHistoryContextFromTask if context_data is provided, |
| 95 | + otherwise returns a no-op nullcontext. |
| 96 | + """ |
| 97 | + if context_data: |
| 98 | + return PgHistoryContextFromTask(context_data) |
| 99 | + return nullcontext() |
0 commit comments