@@ -117,14 +117,22 @@ cleanup() ->
117117
118118% % Processor
119119
120- -spec process ({task_t (), encoded_args (), process ()}, map (), encoded_ctx ()) -> process_result ().
121- process ({CallType , BinArgs , Process }, #{ns := NS } = Options , Ctx ) ->
122- _ = set_context (Ctx ),
120+ -spec process ({task_t (), encoded_args (), process ()}, hg_woody_service_wrapper :handler_opts (), encoded_ctx ()) ->
121+ process_result ().
122+ process ({CallType , BinArgs , Process }, #{ns := NS } = Options , BinCtx ) ->
123+ {WoodyContext0 , OtelCtx } = woody_rpc_helper :decode_rpc_context (marshal (term , BinCtx )),
124+ ok = woody_rpc_helper :attach_otel_context (OtelCtx ),
123125 #{last_event_id := LastEventID } = Process ,
124126 Machine = marshal (process , Process #{ns => NS }),
125127 Func = marshal (function , CallType ),
126128 Args = marshal (args , {CallType , BinArgs , Machine }),
127- handle_result (hg_machine :handle_function (Func , {Args }, Options ), LastEventID ).
129+ WoodyContext = hg_woody_service_wrapper :ensure_woody_deadline_set (WoodyContext0 , Options ),
130+ ok = hg_context :save (hg_woody_service_wrapper :create_context (WoodyContext , Options )),
131+ try
132+ handle_result (hg_machine :handle_function (Func , {Args }, Options ), LastEventID )
133+ after
134+ hg_context :cleanup ()
135+ end .
128136
129137% % Internal functions
130138
@@ -188,18 +196,17 @@ handle_exception({exception, Class, Reason}) ->
188196 erlang :raise (Class , Reason , []).
189197
190198get_context () ->
191- try hg_context :load () of
192- Ctx ->
193- unmarshal (term , Ctx )
194- catch
195- _ :_ ->
196- unmarshal (term , <<>>)
197- end .
198-
199- set_context (<<>>) ->
200- hg_context :save (hg_context :create (#{party_client => #{}}));
201- set_context (BinContext ) ->
202- hg_context :save (marshal (term , BinContext )).
199+ WoodyContext =
200+ try hg_context :load () of
201+ Ctx ->
202+ hg_context :get_woody_context (Ctx )
203+ catch
204+ Class :Reason ->
205+ _ = logger :warning (" Failed to load context with error class '~s ' and reason: ~p " , [Class , Reason ]),
206+ _ = logger :info (" Creating empty fallback context" ),
207+ woody_context :new ()
208+ end ,
209+ unmarshal (term , woody_rpc_helper :encode_rpc_context (WoodyContext , otel_ctx :get_current ())).
203210
204211% % Marshalling
205212
0 commit comments