How to Run Background Tasks Using APIs
To run a Background Task using the API endpoints, a user interacts with the refinery through HTTP requests. The refinery provides specific endpoints to create and run Background Tasks. Below are the steps to run a Background Task using the APIs.
In order to create a Background Task, a user must have the TASK_EDITOR permission. For more information, see the Background Tasks page or the Permissions and Roles page.
Table of contents
Uploading the Background Task Image
To upload a Background Task image to the Elastic Container Registry (ECR) repository, follow these steps to send HTTP requests to the refinery.
-
If required, retrieve and set the API
authorization tokenby following the steps in Appendix A: Retrieve and Set JWT_TOKEN. - To fetch the
ECRrepository URL, send aHTTP GETrequest to/tasks/repo/urlendpoint using the followingcurlcommand.curl -X GET "https://example.kingland-data-refinery.com/api/tasks/repo/url" \ -H "Authorization: Bearer $JWT_TOKEN"This returns the
ECRrepository URL string as shown in the example below.example-account.dkr.ecr.example-region.amazonaws.com/bgtask_exampleTry fetching the
ECRrepository URL using the DR Designer API Reference. -
Next, fetch the
ECRrepository password by sending aHTTP GETrequest to/tasks/repo/tokenusing the followingcurlcommand.curl -X GET "https://example.kingland-data-refinery.com/api/tasks/repo/token" \ -H "Authorization: Bearer $JWT_TOKEN"This returns the
tokenrequired to configure the Amazon Web Services, Command Line Interface (AWS CLI) and the role to assume. See below.{"password": "example-ecr-repo-token", "expiresAt": "yyyy-mm-ddThh:mm:ss.SSSZ"}Try fetching the
ECRrepository password using the DR Designer API Reference. -
Dockershould be installed on the user’s machine to log in to theECRrepository by using the repo URL and password retrieved in Step 2 and Step 3, respectively.docker login example-account.dkr.ecr.example-region.amazonaws.com --username AWS --password your-passwordReplace “your-password” with the password retrieved from Step 3. If successful, the “Login Succeeded” response will appear.
-
Next, build the
Dockerimage. Then, after the build is complete, tag your image to push to the repository.If an example Docker image is needed for this demo, refer to Appendix B: Using a Sample Docker Image.
docker build -t bgtask_example . docker tag bgtask_example:latest your-ecr-repo-url:latest-exampleReplace “your-ecr-repo-url” with the real repo URL retrieved in Step 2.
- Push this image to the
ECRBackground Task repository.docker push your-ecr-repo-url:latest-exampleReplace “your-ecr-repo-url” with the real repo URL retrieved in Step 2.
Creating a New Task
To create a new Background Task using curl commands, follow these steps to send an HTTP POST request to the refinery.
-
Prepare the task input in a
JSONfile based on theAWSBackgroundTaskmodel. TheJSONfile should include the necessary fields for task definition includingname,cronSchedule,imageUrl,envVars,cpu,memory,command, and reference the launch template found using Selecting a Launch Template.{ "name": "example-task-name", "cronSchedule": "* * * * *", "imageUrl": "account.dck.ecr.region.amazonaws.com/bgtask_dev:latest-example", "cpu": 256, "memory": 512, "command": [""], "envVars": { "VAR1": "value1", "VAR2": "value2" }, "launchTemplateModel": { "ID": 1, "templateName": "example-template-name", "cluster": "example-cluster-name" } }Note. Background Tasks are scheduled with Coordinated Universal Time (UTC).
Note. The example image from Appendix B: Using a Sample Docker Image, needs the following for
command:["/bin/sh", "-c", "echo 'hello from bgtask'"]. -
If required, retrieve and set the API
authorization tokenby following the steps in Appendix A: Retrieve and Set JWT_TOKEN. -
Use the
curlcommand to send anHTTP POSTrequest to the/tasksendpoint. Use theJSONfile created in Step 1 as the task input. Include the necessary headers, such as the content type and authorization with thebearer tokenretrieved in Step 2.curl -X POST "https://example.kingland-data-refinery.com/api/tasks" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $JWT_TOKEN" \ -d "@PATH_TO_TASK_JSON_FILE"Below is an example of an expected task response.
{ "ID": 98, "name": "example-task-name", "cronSchedule": "* * * * *", "imageUrl": "1234567890.dkr.ecr.us-east-2.amazonaws.com/bgtask_dev:latest-example", "envVars": { "VAR1": "", "VAR2": "" }, "taskDefinitionArn": "arn:aws:ecs:us-east-2:1234567890:task-definition/drf-bg:2", "launchType": 0, "cpu": 256, "memory": 512, "tags": null, "launchTemplate": 110, "launchTemplateModel": { "ID": 110, "templateName": "example-template-name", "cluster": "example-cluster-name" } }Try creating a new task using the DR Designer API Reference.
Note. For security, Data Refinery Designer API redacts the values of a task’s environment variables’ values. Only the variable keys are returned. When updating the tasks, if
EnvVarsonly contain empty values, then theEnvVarsinput is ignored and not updated.
Running the Task Manually
To run a Background Task manually, follow these steps to send a HTTP POST request to the refinery. Both scheduled and unscheduled tasks can be run manually.
- If required, retrieve and set the API authorization token by following the steps in Appendix A: Retrieve and Set JWT_TOKEN
- To run a Background Task, send a
HTTP POSTrequest to/tasks/:ID/executionendpoint using the followingcurlcommand.curl -X POST "https://example.kingland-data-refinery.com/api/tasks/example-task-id/execution" \ -H "Authorization: Bearer $JWT_TOKEN"The run task response contains the
taskIdentifierwhich can be used to check the task run results.{"ID":15,"definitionID":9,"definitionType":1,"taskIdentifier":"arn:aws:ecs:us-east-2:1234567890:task/example-cluster-name/example-task-arn"}Try running the task using the DR Designer API Reference.
View Task Run Results
To view the task run status, use the AWS CLI describe-tasks command to verify the desired status from the desired task. To ensure a successful run, check the exit code of the task container using the command below.
aws ecs describe-tasks --region your-aws-region\
--cluster $STRING_CLUSTER_NAME \
--tasks your-task-identifier \
--query "tasks[0].containers[?name=='aws-bg-task'].exitCode" \
--output text
Replace “your-aws-region” with AWS region and “your-task-identifier’ with the taskIdentifier value retrieved from Step 2 in Running the Task.
Troubleshoot Failed Scheduled Tasks
Scheduled tasks are started and tracked by the API containers. A new or updated task may take up to 30 seconds before the initial execution. The application’s CloudWatch logs contain events for when a task is scheduled, updated, or removed from the internal scheduler. If a task’s cron is removed, it becomes a single-execution task and is removed from the scheduler.
The logs’ format is [INFO] - {Registered|Removed} task from scheduler taskID={ID}.
Note. An update event is represented by a “Removed” event, then immediately followed by a “Registered” event.
Successfully started tasks are also logged in CloudWatch. The log’s format is: [INFO] - Background task started taskID={ID}.
Scheduled tasks may fail to start due to improper configuration, such as a bad cron schedule or resource allocation. If a scheduled task fails to start, it will not be queryable via the aws ecs describe-tasks command. Instead, an application log will be sent to CloudWatch detailing the failed task and more information on the failure. A CloudWatch alarm can be set up to trigger based on this error message if desired.
The log’s format is: [WARN] - Background task execution failed for taskID={ID} err={error message}.
Appendices
Appendix A: Retrieve and Set JWT_TOKEN
-
To retrieve the
bearer token, a user must log in using their username and password. Next, follow the steps in How to Authenticate. -
Replace
YOUR_JWT_TOKEN_VALUEwith the bearertokenretrieved from login to setJWT_TOKEN.export JWT_TOKEN=YOUR_JWT_TOKEN_VALUE
Appendix B: Using a Sample Docker Image
To build a simple Docker image for a demo purpose, follow the steps below to pull and tag an alpine image.
docker pull alpine:latest
docker tag alpine:latest your-ecr-repo-url:latest-example
Replace “your-ecr-repo-url” fetch from Step 2 under Uploading the Background Task Image.
Appendix C: AWSBackgroundTask Model
{
"name": "string",
"cronSchedule": "string",
"imageUrl": "string",
"envVars": {
"key1": "value1",
"key2": "value2",
},
"command": ["command1", "command2"],
"taskDefinitionArn": "string",
"launchType": 0,
"cpu": 0,
"memory": 0,
"tags": [
{
"Key": "string",
"Value": "string"
},
{
"Key": "string",
"Value": "string"
},
],
"launchTemplate": 0,
"launchTemplateModel": {
"templateName": "string",
"cluster": "string"
}
}