Asynchronous Script Execution

The default mode for REST API for Embedded R Execution function endpoint calls is synchronous, so the REST API call must return before you can execute any additional calls. Running a call in asynchronous mode allows you to invoke function endpoints without needing to wait for a previous call to return.

Follow these steps to use the asynchronous mode of script execution.

  1. Call a script execution function with the asyncFlag set to true. The return is a location that includes a job id that you can use to poll the status of the job.
  2. Get the status of the job. In the return, a 202 status code indicates that the job is pending. A 302 code indicates that the job has finished. When the job has finished, the return includes a Content-Location header.
  3. Fetch the results of the script.

The following is an example of making an asynchronous group-apply call and fetching the results. In the example, the parallelFlag argument is set to true to make use of database parallelism.

curl -i -X POST --header "Authorization: Bearer ${token}" \
--header 'Content-Type: application/json' --header 'Accept: application/json' \
-d '{"asyncFlag":true, "input":"IRIS", "groupBy":"Species", "parallelFlag":true}' \
"<oml-cloud-service-location-url>/oml/api/r-scripts/v1/group-apply/group.count"

The output includes the HTTP 201 status code indicating that the job was created and a job id that you use to get the job status and the results.

In the following result, the job has been created.

HTTP/1.1 201 Created
Date: Thu, 28 Jul 2022 21:04:18 GMT
Content-Type: application/json
Content-Length: 1820
Connection: keep-alive
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Security-Policy: frame-ancestors 'none'
Location: https://<oml-cloud-service-location-url>/oml/api/r-scripts/v1/jobs/<job id>

To poll the job status, pass the job id to cURL using the GET method.

curl -i -X GET --header "Authorization: Bearer ${token}" \
--header 'Accept: application/json' \
"<oml-cloud-service-location-url>/oml/api/r-scripts/v1/jobs/<job id>"

The HTTP response status 302 indicates that the job has finished. The Content-Location has the location of the result.

HTTP/1.1 302 Found
Date: Thu, 28 Jul 2022 21:04:18 GMT
Content-Type: application/json
Content-Length: 1820
Connection: keep-alive
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Security-Policy: frame-ancestors 'none'
Content-Location: <oml-cloud-service-location-url>/oml/api/r-scripts/v1/jobs/<job id>/result

Get the result.

curl -i -X GET --header "Authorization: Bearer ${token}" \
--header 'Accept: application/json' \
"<oml-cloud-service-location-url>/oml/api/r-scripts/v1/jobs/<job id>/result"
HTTP/1.1 200 OK
Date: Thu, 27 Aug 2020 15:16:32 GMT
Content-Type: application/json
Content-Length: 5045
Connection: keep-alive
Cache-Control: no-cache, no-store, private
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff

The following is a portion of the result.

{"result":[{"Petal.Width":50,"Sepal.Length":50,"Sepal.Width":50,"Species":"setosa","Petal.Length":50,"Species.1":50},{"Petal.Width":50,"Sepal.Length":50,"Sepal.Width":50,"Species":"versicolor","Petal.Length":50,"Species.1":50},{"Petal.Width":50,"Sepal.Length":50,"Sepal.Width":50,"Species":"virginica","Petal.Length":50,"Species.1":50}]}