Search code examples
xquerymarklogicspawnmarklogic-10

Marklogic - Return the HTTP response without waiting for spawn process to get completed


I have created a REST API that receives a HTTP POST request(array of json requests) from UI. This would trigger a Xquery code which would spawn the requests to execute some functionalities and it may 10-30 mins to get completed.

If the request gets into the XQuery code, we just want to send the response back with status "Upload triggered successfully" and let the task on server side continue running in background without UI waiting for the process to get completed. As the task runs in background, a report document is inserted and the completed request count is updated after each request process complete.

Please find below the code outline for the same.

declare function local:upload-record($req-xml, $chunk-size,$upload-uri){
    if (exists($req-xml))
    then (
    let $log := xdmp:log("Upload started")
    <!-- some code to execute functionalities-->

                     ,
                      xdmp:spawn-function(function(){ 
                        local:upload-record(subsequence($req-xml, $chunk-size+1), $chunk-size,$upload-uri) 
                          }, 
                        <options xmlns="xdmp:eval">
                        <result>true</result>
                        
                        <update>true</update>
                        <commit>auto</commit>
                        </options>))
                   
     return $response)
     else xdmp:log("Job completed successfully")
};

let $req := xdmp:get-request-body("json")/node()

let $config := json:config("custom")
let $req-xml := json:transform-from-json($req,$config)
let $chunk-size := 10

let $resp := local:upload-record($req-xml, $chunk-size,$upload-uri)
  
return <response>Upload triggered successfully</response>

I need help in implementing below questions.

  1. To return status response once the request reached ML code without UI waiting for the process to get completed.
  2. Determine proper chunk size based on number of requests.(min req count: 1 max:1000).Maximum number of threads configured for the app server is 32.

Please let me know your thoughts. Thanks in advance!


Solution

  • Spawn the initial call to the recursive function and set the result option to false so that it doesn't wait to execute and resolve the results.

    let $resp := xdmp:spawn-function(function() { 
        local:upload-record($req-xml, $chunk-size,$upload-uri) 
      }, 
      <options xmlns="xdmp:eval"><result>false</result></options> )