Event onPostDispatch %CSP.Rest or similar
Hi all,
For a requirement of the customer, we have a BS Rest api with a lot of methods, we need to get the IP of the caller, the method and what is the time that the API has taken to process.
I've found the event onPreDispatch where I can take the IP, ClassMethod, etc.. I'm using a global variable to save this information.
ClassMethod OnPreDispatch(pUrl As%String, pMethod As%String, ByRef pContinue As%Boolean) As%Status
{
set^CallApi($CLASSNAME(),$JOB,"url") = pUrl
set^CallApi($CLASSNAME(),$JOB,"method") = pMethod
set^CallApi($CLASSNAME(),$JOB,"Start") = $HOROLOGset^CallApi($CLASSNAME(),$JOB,"remote_addr") = %request.CgiEnvs("REMOTE_ADDR")
set pContinue = 1quit$$$OK
}Now, I need to get the end time, and save all the information using a BO that save the information out of IRIS (Async)
Is there any method/event/whatever that is fired when has finished the call? Then I can kill the global variable and get the end time, call to the out process, etc..
Thanks in advance
Comments
Hi,
As you are in Aync mode, it's a bit tricky to get the end time. In sync mode, you just have to wait for the answer to be back.
In this case I would use the session id from the message that is going out of the business service to the BO.
So in the BS, try something like this:
Method OnProcessInput(
pDocIn As %RegisteredObject,
Output pDocOut As %RegisteredObject) As %Status
{
// here you need to pass the jobid from the %CSP.REST that is invoking the BS
set tJobId = pDocIn.JobID
// your code here
set ^CallApi($CLASSNAME(),tJobId,"sessionid") = ..%SessionId
set pDocOut = pDocIn
quit $$$OK
}
Then in the BO, you can use the session id to get the information from the global.
Method MyAsyncMethod(
pRequest As %RegisteredObject,
Output pResponse As %RegisteredObject) As %Status
{
set tSessionId = ..%SessionId
// lookup for the good session id
for {
set tClassname = $ORDER(^CallApi(tClassname))
quit:tClassname=""
for {
set tJobId = $ORDER(^CallApi(tClassname,tJobId))
quit:tJobId=""
if ^CallApi(tClassname,tJobId,"sessionid") = tSessionId {
set ^CallApi(tClassname,tJobId,"End") = $HOROLOG
quit
}
}
}
// your code here
quit $$$OK
}
I hope this helps.
Thanks for your help.
But I am trying to use a common class, all the BS are inherited from this BS.ApiBase.cls, this way I don't need to modify the rest of the productions and we can implement this feature for all of them.
Anyway, you have solved me how to get the SessionId, using the OnProcessInput
I don't think there is a callback method available.
One option could be to subclass %CSP.REST and use your subclass instead of %CSP.REST.
You subclass can be something like:
Class MyCustomCSP.REST Extends%CSP.REST
{
ClassMethod DispatchRequest(url As%String, method As%String, forwarded As%Boolean = 0, args...) As%Status
{
Set sc=##super(url,method,forwarded,.args)
; your "onPostDispatch" code here Quit sc
}
}
Also look at the events OnEndRequest()/OnStartRequest()
ding, ding, ding... we have a winner!!!
Yep... the event OnEndRequest is the correct answer... I can get the end of the process.
Thanks for your help