11from typing import Any , Callable , Dict , List , Union
22
33from servc .svc import ComponentType , Middleware
4+ from servc .svc .client .send import sendMessage
45from servc .svc .com .bus import BusComponent , OnConsuming
56from servc .svc .com .cache import CacheComponent
67from servc .svc .config import Config
7- from servc .svc .io .input import EventPayload , InputPayload , InputType , ArgumentArtifact
8+ from servc .svc .idgen .simple import simple as idGenerator
9+ from servc .svc .io .input import ArgumentArtifact , InputPayload , InputType
810from servc .svc .io .output import StatusCode
911from servc .svc .io .response import getAnswerArtifact , getErrorArtifact
10- from servc . svc . client . send import sendMessage
11- from servc . svc . idgen . simple import simple as idGenerator
12+
13+ EMIT_EVENT = Callable [[ str , Any ], None ]
1214
1315RESOLVER = Callable [
14- [str , BusComponent , CacheComponent , Any , List [Middleware ]],
16+ [str , BusComponent , CacheComponent , Any , List [Middleware ], EMIT_EVENT ],
1517 Union [StatusCode , Any , None ],
1618]
1719
@@ -111,18 +113,12 @@ def connect(self):
111113 bindEventExchange = self ._bindToEventExchange ,
112114 )
113115
114- def emitEvent (self , eventName : str , details : Any ):
115- eventMessage : EventPayload = {
116- "type" : InputType .EVENT .value ,
117- "event" : eventName ,
118- "details" : details ,
119- "route" : self ._route ,
120- "instanceId" : self ._instanceId ,
121- }
122-
123- self ._bus .publishMessage (self ._route , eventMessage )
116+ def emitEvent (self , bus : BusComponent , eventName : str , details : Any ):
117+ bus .emitEvent (eventName , self ._instanceId , details )
124118
125- def processPostHooks (self , bus : BusComponent , message : InputPayload , artifact : ArgumentArtifact ):
119+ def processPostHooks (
120+ self , bus : BusComponent , message : InputPayload , artifact : ArgumentArtifact
121+ ):
126122 # print(artifact)
127123 if "hooks" in artifact and "on_complete" in artifact ["hooks" ]:
128124 for hook in artifact ["hooks" ]["on_complete" ]:
@@ -139,9 +135,9 @@ def processPostHooks(self, bus: BusComponent, message: InputPayload, artifact: A
139135 "inputs" : {
140136 "id" : message ["id" ],
141137 "method" : artifact ["method" ],
142- "inputs" : artifact ["inputs" ]
138+ "inputs" : artifact ["inputs" ],
143139 },
144- }
140+ },
145141 }
146142 sendMessage (payload , bus , self ._cache , idGenerator )
147143 except Exception as e :
@@ -155,6 +151,7 @@ def inputProcessor(self, message: Any) -> StatusCode:
155151 self ._config .get ("conf.bus.prefix" ),
156152 )
157153 cache = self ._cache
154+ emitEvent : EMIT_EVENT = lambda x , y : self .emitEvent (bus , x , y )
158155
159156 if "type" not in message or "route" not in message :
160157 return StatusCode .INVALID_INPUTS
@@ -174,6 +171,7 @@ def inputProcessor(self, message: Any) -> StatusCode:
174171 cache ,
175172 {** message },
176173 self ._children ,
174+ emitEvent ,
177175 )
178176 return StatusCode .OK
179177
@@ -223,16 +221,15 @@ def inputProcessor(self, message: Any) -> StatusCode:
223221 cache ,
224222 artifact ["inputs" ],
225223 self ._children ,
224+ emitEvent ,
226225 )
227- cache .setKey (message ["id" ], getAnswerArtifact (
228- message ["id" ], response ))
226+ cache .setKey (message ["id" ], getAnswerArtifact (message ["id" ], response ))
229227 self .processPostHooks (bus , message , artifact )
230228 return StatusCode .OK
231229 except Exception as e :
232230 cache .setKey (
233231 message ["id" ],
234- getErrorArtifact (message ["id" ], str (
235- e ), StatusCode .SERVER_ERROR ),
232+ getErrorArtifact (message ["id" ], str (e ), StatusCode .SERVER_ERROR ),
236233 )
237234 self .processPostHooks (bus , message , artifact )
238235 return StatusCode .SERVER_ERROR
0 commit comments