|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from contextlib import contextmanager |
| 4 | +from typing import Any, Mapping, Protocol, Type |
| 5 | + |
| 6 | +import temporalio.activity |
| 7 | +import temporalio.api.common.v1 |
| 8 | +import temporalio.client |
| 9 | +import temporalio.converter |
| 10 | +import temporalio.worker |
| 11 | +import temporalio.workflow |
| 12 | + |
| 13 | +with temporalio.workflow.unsafe.imports_passed_through(): |
| 14 | + from context_propagation.shared import HEADER_KEY, user_id |
| 15 | + |
| 16 | + |
| 17 | +class _InputWithHeaders(Protocol): |
| 18 | + headers: Mapping[str, temporalio.api.common.v1.Payload] |
| 19 | + |
| 20 | + |
| 21 | +def set_header_from_context( |
| 22 | + input: _InputWithHeaders, payload_converter: temporalio.converter.PayloadConverter |
| 23 | +) -> None: |
| 24 | + user_id_val = user_id.get() |
| 25 | + if user_id_val: |
| 26 | + input.headers = { |
| 27 | + **input.headers, |
| 28 | + HEADER_KEY: payload_converter.to_payload(user_id_val), |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | +@contextmanager |
| 33 | +def context_from_header( |
| 34 | + input: _InputWithHeaders, payload_converter: temporalio.converter.PayloadConverter |
| 35 | +): |
| 36 | + payload = input.headers.get(HEADER_KEY) |
| 37 | + token = ( |
| 38 | + user_id.set(payload_converter.from_payload(payload, str)) if payload else None |
| 39 | + ) |
| 40 | + try: |
| 41 | + yield |
| 42 | + finally: |
| 43 | + if token: |
| 44 | + user_id.reset(token) |
| 45 | + |
| 46 | + |
| 47 | +class ContextPropagationInterceptor( |
| 48 | + temporalio.client.Interceptor, temporalio.worker.Interceptor |
| 49 | +): |
| 50 | + """Interceptor that can serialize/deserialize contexts.""" |
| 51 | + |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + payload_converter: temporalio.converter.PayloadConverter = temporalio.converter.default().payload_converter, |
| 55 | + ) -> None: |
| 56 | + self._payload_converter = payload_converter |
| 57 | + |
| 58 | + def intercept_client( |
| 59 | + self, next: temporalio.client.OutboundInterceptor |
| 60 | + ) -> temporalio.client.OutboundInterceptor: |
| 61 | + return _ContextPropagationClientOutboundInterceptor( |
| 62 | + next, self._payload_converter |
| 63 | + ) |
| 64 | + |
| 65 | + def intercept_activity( |
| 66 | + self, next: temporalio.worker.ActivityInboundInterceptor |
| 67 | + ) -> temporalio.worker.ActivityInboundInterceptor: |
| 68 | + return _ContextPropagationActivityInboundInterceptor(next) |
| 69 | + |
| 70 | + def workflow_interceptor_class( |
| 71 | + self, input: temporalio.worker.WorkflowInterceptorClassInput |
| 72 | + ) -> Type[_ContextPropagationWorkflowInboundInterceptor]: |
| 73 | + return _ContextPropagationWorkflowInboundInterceptor |
| 74 | + |
| 75 | + |
| 76 | +class _ContextPropagationClientOutboundInterceptor( |
| 77 | + temporalio.client.OutboundInterceptor |
| 78 | +): |
| 79 | + def __init__( |
| 80 | + self, |
| 81 | + next: temporalio.client.OutboundInterceptor, |
| 82 | + payload_converter: temporalio.converter.PayloadConverter, |
| 83 | + ) -> None: |
| 84 | + super().__init__(next) |
| 85 | + self._payload_converter = payload_converter |
| 86 | + |
| 87 | + async def start_workflow( |
| 88 | + self, input: temporalio.client.StartWorkflowInput |
| 89 | + ) -> temporalio.client.WorkflowHandle[Any, Any]: |
| 90 | + set_header_from_context(input, self._payload_converter) |
| 91 | + return await super().start_workflow(input) |
| 92 | + |
| 93 | + async def query_workflow(self, input: temporalio.client.QueryWorkflowInput) -> Any: |
| 94 | + set_header_from_context(input, self._payload_converter) |
| 95 | + return await super().query_workflow(input) |
| 96 | + |
| 97 | + async def signal_workflow( |
| 98 | + self, input: temporalio.client.SignalWorkflowInput |
| 99 | + ) -> None: |
| 100 | + set_header_from_context(input, self._payload_converter) |
| 101 | + await super().signal_workflow(input) |
| 102 | + |
| 103 | + async def start_workflow_update( |
| 104 | + self, input: temporalio.client.StartWorkflowUpdateInput |
| 105 | + ) -> temporalio.client.WorkflowUpdateHandle[Any]: |
| 106 | + set_header_from_context(input, self._payload_converter) |
| 107 | + return await self.next.start_workflow_update(input) |
| 108 | + |
| 109 | + |
| 110 | +class _ContextPropagationActivityInboundInterceptor( |
| 111 | + temporalio.worker.ActivityInboundInterceptor |
| 112 | +): |
| 113 | + async def execute_activity( |
| 114 | + self, input: temporalio.worker.ExecuteActivityInput |
| 115 | + ) -> Any: |
| 116 | + with context_from_header(input, temporalio.activity.payload_converter()): |
| 117 | + return await self.next.execute_activity(input) |
| 118 | + |
| 119 | + |
| 120 | +class _ContextPropagationWorkflowInboundInterceptor( |
| 121 | + temporalio.worker.WorkflowInboundInterceptor |
| 122 | +): |
| 123 | + def init(self, outbound: temporalio.worker.WorkflowOutboundInterceptor) -> None: |
| 124 | + self.next.init(_ContextPropagationWorkflowOutboundInterceptor(outbound)) |
| 125 | + |
| 126 | + async def execute_workflow( |
| 127 | + self, input: temporalio.worker.ExecuteWorkflowInput |
| 128 | + ) -> Any: |
| 129 | + with context_from_header(input, temporalio.workflow.payload_converter()): |
| 130 | + return await self.next.execute_workflow(input) |
| 131 | + |
| 132 | + async def handle_signal(self, input: temporalio.worker.HandleSignalInput) -> None: |
| 133 | + with context_from_header(input, temporalio.workflow.payload_converter()): |
| 134 | + return await self.next.handle_signal(input) |
| 135 | + |
| 136 | + async def handle_query(self, input: temporalio.worker.HandleQueryInput) -> Any: |
| 137 | + with context_from_header(input, temporalio.workflow.payload_converter()): |
| 138 | + return await self.next.handle_query(input) |
| 139 | + |
| 140 | + def handle_update_validator( |
| 141 | + self, input: temporalio.worker.HandleUpdateInput |
| 142 | + ) -> None: |
| 143 | + with context_from_header(input, temporalio.workflow.payload_converter()): |
| 144 | + self.next.handle_update_validator(input) |
| 145 | + |
| 146 | + async def handle_update_handler( |
| 147 | + self, input: temporalio.worker.HandleUpdateInput |
| 148 | + ) -> Any: |
| 149 | + with context_from_header(input, temporalio.workflow.payload_converter()): |
| 150 | + return await self.next.handle_update_handler(input) |
| 151 | + |
| 152 | + |
| 153 | +class _ContextPropagationWorkflowOutboundInterceptor( |
| 154 | + temporalio.worker.WorkflowOutboundInterceptor |
| 155 | +): |
| 156 | + async def signal_child_workflow( |
| 157 | + self, input: temporalio.worker.SignalChildWorkflowInput |
| 158 | + ) -> None: |
| 159 | + set_header_from_context(input, temporalio.workflow.payload_converter()) |
| 160 | + return await self.next.signal_child_workflow(input) |
| 161 | + |
| 162 | + async def signal_external_workflow( |
| 163 | + self, input: temporalio.worker.SignalExternalWorkflowInput |
| 164 | + ) -> None: |
| 165 | + set_header_from_context(input, temporalio.workflow.payload_converter()) |
| 166 | + return await self.next.signal_external_workflow(input) |
| 167 | + |
| 168 | + def start_activity( |
| 169 | + self, input: temporalio.worker.StartActivityInput |
| 170 | + ) -> temporalio.workflow.ActivityHandle: |
| 171 | + set_header_from_context(input, temporalio.workflow.payload_converter()) |
| 172 | + return self.next.start_activity(input) |
| 173 | + |
| 174 | + async def start_child_workflow( |
| 175 | + self, input: temporalio.worker.StartChildWorkflowInput |
| 176 | + ) -> temporalio.workflow.ChildWorkflowHandle: |
| 177 | + set_header_from_context(input, temporalio.workflow.payload_converter()) |
| 178 | + return await self.next.start_child_workflow(input) |
| 179 | + |
| 180 | + def start_local_activity( |
| 181 | + self, input: temporalio.worker.StartLocalActivityInput |
| 182 | + ) -> temporalio.workflow.ActivityHandle: |
| 183 | + set_header_from_context(input, temporalio.workflow.payload_converter()) |
| 184 | + return self.next.start_local_activity(input) |
0 commit comments