· Apr 6, 2017

How to store json data in session which returns from background method?

Based on user request in zenpage, i am running the method ConvertMDXtoJSON in background and loading the status in progressbar.

D ..%RunBackgroundMethod("ConvertMDXtoJSON",MDX)

From this background method ConvertMDXtoJSON() i want to return the value(sjson) and store it into session.

How can i do this?

Below is my code. 

ClassMethod ConvertMDXtoJSON(pMDX) As %String
      Do ..%SetBackgroundMethodStatus("Started",0)
      set Params = ""
      set pStatus = $$$OK
      set RS = ##class(MS.Deepsee.MDX2JSON.ResultSet).%New()    
      set pStatus = RS.%PrepareMDX(pMDX)
      return:$$$ISERR(pStatus) RS
      set pStatus = RS.%ExecuteAsynch(.QueryKey,.Params,0)
      s pCubeName="StockDetail"

      // Gets progress Percentage
      set prog= ##class(%DeepSee.ResultSet).%GetQueryStatus(pCubeName, QueryKey,.tComplete,.tSC,.tPending)
      while (prog<100 )
            set prog= ##class(%DeepSee.ResultSet).%GetQueryStatus(pCubeName, QueryKey,.tComplete,.tSC,.tPending)
            Do ..%SetBackgroundMethodStatus("Percentage completed",tComplete)
  Catch {}

   set obj = RS.ToProxyObject(.st)
   return:$$$ISERR(st) st
   Set tInitIO = $IO

    // we MUST use %ISCJSONStream variable used by mnemonic space ^%ZEN.Auxiliary.jsonProvider.1 as output redirection container
    set %ISCJSONStream=##class(%Stream.TmpCharacter).%New()
    use tInitIO::("^%ZEN.Auxiliary.jsonProvider.1") // this routine – here as mnemonic space – is created during %ZEN.Auxiliary.jsonProvider class compilation
    do ##class(%Library.Device).ReDirectIO(1)
    If ##class(%Library.Device).ReDirectIO(0) Use tInitIO
    do %ISCJSONStream.Rewind() 
    set sjson = %ISCJSONStream.Read() 
    Do ..%EndBackgroundMethod()
    Q sjson

ClassMethod %OnMonitorBackgroundTask(pTaskID As %String, pStatus As %String, pPercentComplete As %Float)

Set pMsg = pStatus _ ": " _ +pPercentComplete _ "%"    
if pPercentComplete>0

ClassMethod %OnFinishBackgroundTask(pTaskID As %String)
#; Clear HTML component    

After finishing the backgroundtask, loading a iframe. there i need to get the status(sjon).

Give me some suggestion.

Discussion (4)0
Log in or sign up to continue

Converting MDX2Json that doesn't matter here.

In background task, i am quiting json. I dont know how to store the json into a session.


In general if i write like this,

S json=..ConvertMDX2JSON(MDXQuery)

I will get json data for the MDX Query. But, if i go for complex MDX, it will take more time to convert and will get timeout error.

So i am doing this in background task and want to store the result json in session.

Hi Ashok,

It looks like the %session variable isn't accessible in a zen background method. I'd suggest passing the %session.SessionId as an argument into the background method. You can store the JSON data in a global or a persistent class keyed by the session id, and access it from there. Then you can override the OnEndSession() method of the application's event class to delete the data when the session is over: