1- from typing import Any , Callable , Dict , List , Union
1+ from typing import Any , List
22
33from servc .svc import ComponentType , Middleware
4- from servc .svc .client .send import sendMessage
54from servc .svc .com .bus import BusComponent , OnConsuming
65from servc .svc .com .cache import CacheComponent
6+ from servc .svc .com .worker .hooks import evaluate_post_hooks , evaluate_pre_hooks
7+ from servc .svc .com .worker .types import EMIT_EVENT , RESOLVER_MAPPING
78from servc .svc .config import Config
8- from servc .svc .idgen .simple import simple as idGenerator
9- from servc .svc .io .input import ArgumentArtifact , InputPayload , InputType
9+ from servc .svc .io .input import InputType
1010from servc .svc .io .output import StatusCode
1111from servc .svc .io .response import getAnswerArtifact , getErrorArtifact
1212
13- EMIT_EVENT = Callable [[str , Any ], None ]
14-
15- RESOLVER = Callable [
16- [str , BusComponent , CacheComponent , Any , List [Middleware ], EMIT_EVENT ],
17- Union [StatusCode , Any , None ],
18- ]
19-
20- RESOLVER_MAPPING = Dict [str , RESOLVER ]
21-
2213
2314def HEALTHZ (
2415 _id : str , bus : BusComponent , cache : CacheComponent , _any : Any , c : List [Middleware ]
@@ -116,34 +107,6 @@ def connect(self):
116107 def emitEvent (self , bus : BusComponent , eventName : str , details : Any ):
117108 bus .emitEvent (eventName , self ._instanceId , details )
118109
119- def processPostHooks (
120- self , bus : BusComponent , message : InputPayload , artifact : ArgumentArtifact
121- ):
122- # print(artifact)
123- if "hooks" in artifact and "on_complete" in artifact ["hooks" ]:
124- for hook in artifact ["hooks" ]["on_complete" ]:
125- if hook ["type" ] == "sendmessage" :
126- try :
127- payload : InputPayload = {
128- "id" : "" ,
129- "type" : InputType .INPUT .value ,
130- "route" : hook ["route" ],
131- "force" : message ["force" ] if "force" in message else False ,
132- "argumentId" : "" ,
133- "argument" : {
134- "method" : hook ["method" ],
135- "inputs" : {
136- "id" : message ["id" ],
137- "method" : artifact ["method" ],
138- "inputs" : artifact ["inputs" ],
139- },
140- },
141- }
142- sendMessage (payload , bus , self ._cache , idGenerator )
143- except Exception as e :
144- print ("Unable to process post hook" , flush = True )
145- print (e , flush = True )
146-
147110 def inputProcessor (self , message : Any ) -> StatusCode :
148111 bus = self ._busClass (
149112 self ._config .get ("conf.bus.url" ),
@@ -214,6 +177,20 @@ def inputProcessor(self, message: Any) -> StatusCode:
214177 )
215178 return StatusCode .METHOD_NOT_FOUND
216179
180+ continueExecution = evaluate_pre_hooks (
181+ self ._route ,
182+ self ._resolvers ,
183+ bus ,
184+ cache ,
185+ message ,
186+ artifact ,
187+ self ._children ,
188+ emitEvent ,
189+ )
190+ if not continueExecution :
191+ return StatusCode .OK
192+
193+ statusCode : StatusCode = StatusCode .OK
217194 try :
218195 response = self ._resolvers [artifact ["method" ]](
219196 message ["id" ],
@@ -224,15 +201,16 @@ def inputProcessor(self, message: Any) -> StatusCode:
224201 emitEvent ,
225202 )
226203 cache .setKey (message ["id" ], getAnswerArtifact (message ["id" ], response ))
227- self .processPostHooks (bus , message , artifact )
228- return StatusCode .OK
204+ evaluate_post_hooks (bus , cache , message , artifact )
229205 except Exception as e :
230206 cache .setKey (
231207 message ["id" ],
232208 getErrorArtifact (message ["id" ], str (e ), StatusCode .SERVER_ERROR ),
233209 )
234- self .processPostHooks (bus , message , artifact )
235- return StatusCode .SERVER_ERROR
210+ statusCode = StatusCode .SERVER_ERROR
211+ finally :
212+ evaluate_post_hooks (bus , cache , message , artifact )
213+ return statusCode
236214
237215 cache .setKey (
238216 message ["id" ],
0 commit comments