Skip to main content

Tasks API

This set of endpoints allows us to interact with tasks that have been created in the Nexus Repository. You will need appropriate privileges to view, create, update, or delete tasks.

Endpoints

The following endpoints are available for the Nexus Repository Tasks API.

GET /service/rest/v1/tasks

This endpoint allows us to iterate through a listing of all the existing tasks.

Let's get a listing of the current tasks:

curl -u admin:admin123 -H 'accept: application/json' -X GET 'http://localhost:8081/service/rest/v1/tasks'

Example Response

{
  "items" : [ {
    "id" : "3f0173a8-c379-44c3-8ba3-2f0d0290bbfa",
    "name" : "Rebuild all browse trees",
    "type" : "create.browse.nodes",
    "message" : null,
    "currentState" : "WAITING",
    "lastRunResult" : null,
    "nextRun" : null,
    "lastRun" : null
  }, {
    "id" : "dbb1f322-907a-4424-ad82-8d0a89deabe1",
    "name" : "Rebuild all search indices",
    "type" : "repository.rebuild-index",
    "message" : null,
    "currentState" : "WAITING",
    "lastRunResult" : null,
    "nextRun" : null,
    "lastRun" : null
  }, {
    "id" : "07fa0c5d-8217-45da-91d3-fdfe9452e21f",
    "name" : "Publish all maven indices",
    "type" : "repository.maven.publish-dotindex",
    "message" : null,
    "currentState" : "WAITING",
    "lastRunResult" : null,
    "nextRun" : null,
    "lastRun" : null
  }, {
    "id" : "2c8fd1f8-2395-4576-a172-a68089bb2ef7",
    "name" : "Compact default blob store",
    "type" : "blobstore.compact",
    "message" : null,
    "currentState" : "WAITING",
    "lastRunResult" : null,
    "nextRun" : null,
    "lastRun" : null
  }, {
    "id" : "0261aed9-9f29-447b-8794-f21693b1f9ac",
    "name" : "Hello World",
    "type" : "script",
    "message" : null,
    "currentState" : "WAITING",
    "lastRunResult" : null,
    "nextRun" : null,
    "lastRun" : null
  } ],
  "continuationToken" : null
}

This endpoint uses a pagination strategy that can be used to iterate through all the tasks if desired.

In this case, we only have one page of results as signified by the null continuationToken.

Note that you can also include the type of tasks to list in the call if desired. The example below would return a list of only cleanup tasks:

curl -X 'GET' \
  'http://localhost:8081/service/rest/v1/tasks?type=assetBlob.cleanup' \
  -H 'accept: application/json' \
  -H 'NX-ANTI-CSRF-TOKEN: 1.1.1.1' \
  -H 'X-Nexus-UI: true'
GET /service/rest/v1/tasks/{id}

This endpoint allows us to get the details about in individual task.

Let's take a look at the details for the "Hello World" task (i.e. id=0261aed9-9f29-447b-8794-f21693b1f9ac):

curl -u admin:admin123 -X GET 'http://localhost:8081/service/rest/v1/tasks/0261aed9-9f29-447b-8794-f21693b1f9ac'

Example Response

{
  "id" : "0261aed9-9f29-447b-8794-f21693b1f9ac",
  "name" : "Hello World",
  "type" : "script",
  "message" : null,
  "currentState" : "WAITING",
  "lastRunResult" : null,
  "nextRun" : null,
  "lastRun" : null
}
POST /service/rest/v1/tasks/{id}/run

This endpoint allows us to run an individual task.

For example, to run the "Hello World" task from above:

curl -u admin:admin123 -X POST 'http://localhost:8081/service/rest/v1/tasks/0261aed9-9f29-447b-8794-f21693b1f9ac/run'

Example Response

HTTP/1.1 204 No Content
Date: Mon, 22 Jan 2018 22:19:47 GMT
...
POST /service/rest/v1/tasks/{id}/stop

This endpoint allows us to stop an individual task. This is the equivalent of cancelling a task in the repository manager UI. Note that not all tasks will respond to a cancellation request.

For example to stop the "Hello World" task:

curl -u admin:admin123 -X POST 'http://localhost:8081/service/rest/v1/tasks/0261aed9-9f29-447b-8794-f21693b1f9ac/stop'

In this example the "Hello World" task was not running when the stop request was made. We will get a 409 response code that signifies that the requested task was not currently running:

Example Response

HTTP/1.1 409 Conflict
Content-Length: 0
Date: Mon, 22 Jan 2018 22:22:33 GMT
...
POST /service/rest/v1/tasks/

The Create endpoint allows you to create a task from the API rather than from the user interface. Below is an example for creating a Cleanup Tags task.

curl -X 'POST' \
  'https://localhost:8081/service/rest/v1/tasks' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'NX-ANTI-CSRF-TOKEN: 1.1.1.1' \
  -H 'X-Nexus-UI: true' \
  -d '{
  "type": "tags.cleanup",
  "name": "Test Tags Cleanup",
  "enabled": true,
  "alertEmail": "abc@123.com",
  "notificationCondition": "FAILURE",
  "frequency": {
    "schedule": "once",
    "startDate": 0,
    "timeZoneOffset": "+3",
    "recurringDays": [
      0
    ],
    "cronExpression": "string"
  }
}'
PUT /service/rest/v1/tasks/{taskId}

The update endpoint allows you to update an existing task outside of the user interface. You must know the task's ID in order to update the task. If you do not know a task's ID, use the Get Task endpoint first to find the task and its associated ID.

Below is an example using this endpoint to update the schedule for a Tag Cleanup task.

curl -X 'PUT' \
  'https://localhost:8081/service/rest/v1/tasks/{taskID}' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'NX-ANTI-CSRF-TOKEN: 1.1.1.1' \
  -H 'X-Nexus-UI: true' \
  -d '{
  "name": "Test Tags Cleanup",
  "enabled": false,
  "alertEmail": "abc@123.com",
  "notificationCondition": "FAILURE",
  "frequency": {
    "schedule": "daily",
    "startDate": 0,
    "timeZoneOffset": "+3",
    "recurringDays": [
      0
    ],
    "cronExpression": "0 0 12 * * ?"
  }
}'
DELETE /service/rest/v1/tasks/{id}

The Delete Task endpoint allows you to delete an existing task by task ID. If you do not know a task's ID, use the Get Task endpoint first to find the task and its associated ID.

Below is an example of deleting a Tag Cleanup task using its ID.

curl -X 'DELETE' \
  'https://localhost:8081/service/rest/v1/tasks/{taskID}' \
  -H 'accept: application/json' \
  -H 'NX-ANTI-CSRF-TOKEN: 1.1.1.1' \
  -H 'X-Nexus-UI: true' 
GET /service/rest/v1/tasks/templates

This endpoint allows you to request a list of all task templates present in your Nexus Repository deployment. A template is an object that contains all task properties for that task type just as you would see in the user interface. You can then use a template as a source when constructing requests to create or update tasks.

Below is an example for retrieving all of the task templates in a given deployment:

curl -X 'GET' \
  'https://localhost:8081/service/rest/v1/tasks/templates' \
  -H 'accept: application/json' \
  -H 'NX-ANTI-CSRF-TOKEN: 1.1.1.1' \
  -H 'X-Nexus-UI: true'
GET /service/rest/v1/tasks/templates/{typeId}

This endpoint allows you to retrieve a specific task template. A template is an object that contains all task properties for that task type just as you would see in the user interface. You can then use the template as a source when constructing requests to create or update tasks.

If you are unsure what typeId to use, use the Get Task Templates endpoint first to retrieve a list of available tasks and their associated types. The type field for each available task corresponds to the typeId that you would enter in this Get Task Template by Type endpoint.

Below is an example for retrieving a Cleanup Tags task template:

curl -X 'GET' \
  'https://localhost:8081/service/rest/v1/tasks/templates/tags.cleanup' \
  -H 'accept: application/json' \
  -H 'NX-ANTI-CSRF-TOKEN: 1.1.1.1' \
  -H 'X-Nexus-UI: true'