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 202 indicates that the job is pending. The status shows job is still running.
HTTP/2 202 
date: Wed, 12 Mar 2025 23:09:20 GMT
content-type: application/json
content-length: 93
strict-transport-security: max-age=31536000; includeSubDomains
x-content-type-options: nosniff
cache-control: no-cache, no-store, must-revalidate
pragma: no-cache
x-frame-options: SAMEORIGIN
x-xss-protection: 1;mode=block
content-security-policy: default-src 'none'; connect-src 'self'; font-src 'self' static.oracle.com; img-src 'self' data: static.oracle.com; media-src 'none'; object-src 'none'; script-src 'self' static.oracle.com 'unsafe-inline' 'unsafe-eval'; style-src 'self' static.oracle.com 'unsafe-inline'; frame-ancestors 'none'
 
{"createdDate":" Mar 12,2025 22:55","status":"job is still running","elapsedTime":"00:13:32"}

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}]}
To delete a running job, pass the job id to cURL using the DELETE method.

Note:

Attempting to delete a job id again will result in the message: "Job <job id> status is FAILURE and not applicable to delete".
curl -i -k -X DELETE --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"service":"LOW"}' "https://<server>/oml/api/r-scripts/v1/jobs/<job id>"
The HTTP response status 200 indicates that the request was executed successfully.
HTTP/1.1 200 OKServer: nginx/1.20.1
Date: Tue, 18 Feb 2025 17:03:50 GMT GMT
Content-Type: application/json
Content-Length: 139
Connection: keep-alive
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Content-Security-Policy: default-src 'none'; connect-src 'self'; font-src 'self' static.oracle.com; img-src 'self' data: static.oracle.com; media-src 'none'; object-src 'none'; script-src 'self' static.oracle.com 'unsafe-inline' 'unsafe-eval'; style-src 'self' static.oracle.com 'unsafe-inline'; frame-ancestors 'none' 
{"Message":"Job <job id> sucessfully deleted container ending :  LOW-1732561821973"}

Adjust the timeout Parameter for Longer Runs in Asynchronous Mode

The default mode for REST API for Embedded R Execution function endpoint calls is synchronous and scripts will automatically time out after 60 seconds. For longer-running scripts, use asynchronous mode. In asynchronous mode, the default timeout is 1800 seconds. To adjust the timeout, set the timeout parameter (in seconds) and enable async_flag=true. The following example uses the asynchronous table-apply invocation and sets the timeout parameter to 7200 seconds.


curl -i -X POST --header "Authorization: Bearer <token>" \   --header 'Content-Type:
      application/json' \   --header 'Accept: application/json' \   -d '{"input":"tablename",
      "asyncFlag":true, "timeout":7200}' \        "<OML
      URL>/api/r-scripts/v1/table-apply/scriptname"