# Job

Jobs are long-running tasks enqueued and processed by the instance itself. No additional worker registration is currently available.


## Pause job queue

`POST /api/v1/jobs/pause`

Authentication (security requirement alternatives):

```json
[
  {
    "OAuth2": [
      "admin"
    ]
  }
]
```

- OAuth2: oauth2. Authenticating via OAuth requires the following steps:
- Have an activated account
- [Generate] an access token for that account at `/api/v1/users/token`.
- Make requests with the *Authorization: Bearer <token\>* header
- Profit, depending on the role assigned to the account

Note that the __access token is valid for 1 day__ and is given
along with a __refresh token valid for 2 weeks__.

[Generate]: https://docs.joinpeertube.org/api/rest-getting-started
; scopes: admin

Request body: application/json

Type: object

- `processRoles` (optional)
  Type: Array<string>
  
  Roles of the PeerTube processes that must pause their job queue. Defaults to all the roles. Other processes are notified asynchronously.
  
  minItems: 1
  
  Items:
    Type: string
    
    enum: ["primary","secondary"]

Responses:

- 204: successful operation

## Examples

### cURL

```bash
curl -X POST 'https://peertube2.cpy.re/api/v1/jobs/pause' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "processRoles": [
    "primary"
  ]
}'
```

### JavaScript

```javascript
const body = `{
  "processRoles": [
    "primary"
  ]
}`;

const response = await fetch('https://peertube2.cpy.re/api/v1/jobs/pause', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN',
    'Content-Type': 'application/json',
  },
  body,
});

const data = await response.text();
```

### Python

```python
import requests

body = '''{
  "processRoles": [
    "primary"
  ]
}'''

response = requests.post(
    'https://peertube2.cpy.re/api/v1/jobs/pause',
    data=body.encode('utf-8'),
    headers={
        'Authorization': 'Bearer YOUR_TOKEN',
        'Content-Type': 'application/json',
    },
)
print(response.text)
```


## Resume job queue

`POST /api/v1/jobs/resume`

Authentication (security requirement alternatives):

```json
[
  {
    "OAuth2": [
      "admin"
    ]
  }
]
```

- OAuth2: oauth2. Authenticating via OAuth requires the following steps:
- Have an activated account
- [Generate] an access token for that account at `/api/v1/users/token`.
- Make requests with the *Authorization: Bearer <token\>* header
- Profit, depending on the role assigned to the account

Note that the __access token is valid for 1 day__ and is given
along with a __refresh token valid for 2 weeks__.

[Generate]: https://docs.joinpeertube.org/api/rest-getting-started
; scopes: admin

Request body: application/json

Type: object

- `processRoles` (optional)
  Type: Array<string>
  
  Roles of the PeerTube processes that must resume their job queue. Defaults to all the roles. Other processes are notified asynchronously.
  
  minItems: 1
  
  Items:
    Type: string
    
    enum: ["primary","secondary"]

Responses:

- 204: successful operation

## Examples

### cURL

```bash
curl -X POST 'https://peertube2.cpy.re/api/v1/jobs/resume' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "processRoles": [
    "primary"
  ]
}'
```

### JavaScript

```javascript
const body = `{
  "processRoles": [
    "primary"
  ]
}`;

const response = await fetch('https://peertube2.cpy.re/api/v1/jobs/resume', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN',
    'Content-Type': 'application/json',
  },
  body,
});

const data = await response.text();
```

### Python

```python
import requests

body = '''{
  "processRoles": [
    "primary"
  ]
}'''

response = requests.post(
    'https://peertube2.cpy.re/api/v1/jobs/resume',
    data=body.encode('utf-8'),
    headers={
        'Authorization': 'Bearer YOUR_TOKEN',
        'Content-Type': 'application/json',
    },
)
print(response.text)
```


## List instance jobs

`GET /api/v1/jobs/{state}`

Authentication (security requirement alternatives):

```json
[
  {
    "OAuth2": [
      "admin"
    ]
  }
]
```

- OAuth2: oauth2. Authenticating via OAuth requires the following steps:
- Have an activated account
- [Generate] an access token for that account at `/api/v1/users/token`.
- Make requests with the *Authorization: Bearer <token\>* header
- Profit, depending on the role assigned to the account

Note that the __access token is valid for 1 day__ and is given
along with a __refresh token valid for 2 weeks__.

[Generate]: https://docs.joinpeertube.org/api/rest-getting-started
; scopes: admin

Parameters:

- `state` (path, string, required) - The state of the job ('' for for no filter)
  Type: string
  
  enum: ["","active","completed","failed","waiting","delayed"]

- `jobType` (query, JobType, optional) - job type
  Type: JobType
  
  Schema: [JobType](https://docs.joinpeertube.org/api-rest-reference/api/models/jobtype.md)
  
  enum: ["activitypub-cleaner","activitypub-follow","activitypub-http-broadcast","activitypub-http-broadcast-parallel","activitypub-http-fetcher","activitypub-http-unicast","activitypub-refresher","actor-keys","after-video-channel-import","build-automatic-tags","build-object-automatic-tags","create-user-export","email","federate-video","generate-video-storyboard","import-user-archive","manage-video-torrent","move-to-file-system","move-to-object-storage","notify","transcoding-job-builder","video-channel-import","video-file-import","video-files-lifecycle","video-import","video-live-ending","video-redundancy","video-studio-edition","video-transcoding","video-transcription","videos-stats"]

- `start` (query, integer, optional) - Offset used to paginate results
  Type: integer
  
  minimum: 0

- `count` (query, integer, optional) - Number of items to return
  Type: integer
  
  default: 15
  
  minimum: 1
  
  maximum: 100

- `sort` (query, string, optional) - Sort column
  Type: string
  
  Example:
  
  ```json
  "-createdAt"
  ```

Responses:

- 200: successful operation

Content type: application/json

Type: object

- `total` (optional)
  Type: integer
  
  Example:
  
  ```json
  1
  ```

- `data` (optional)
  Type: Array<Job>
  
  maxItems: 100
  
  Items:
    Reference: [Job](https://docs.joinpeertube.org/api-rest-reference/api/models/job.md); Type: Job

## Examples

### cURL

```bash
curl -X GET 'https://peertube2.cpy.re/api/v1/jobs/?count=15' \
  -H 'Authorization: Bearer YOUR_TOKEN'
```

### JavaScript

```javascript
const response = await fetch('https://peertube2.cpy.re/api/v1/jobs/?count=15', {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN',
  },
});

const data = await response.text();
```

### Python

```python
import requests

response = requests.get(
    'https://peertube2.cpy.re/api/v1/jobs/?count=15',
    headers={
        'Authorization': 'Bearer YOUR_TOKEN',
    },
)
print(response.text)
```


## Cancel a job

`POST /api/v1/jobs/{jobType}/{jobId}/cancel`

Abort a running job and its underlying ffmpeg/child process. Only active jobs that support cancellation can be cancelled.

Authentication (security requirement alternatives):

```json
[
  {
    "OAuth2": [
      "admin"
    ]
  }
]
```

- OAuth2: oauth2. Authenticating via OAuth requires the following steps:
- Have an activated account
- [Generate] an access token for that account at `/api/v1/users/token`.
- Make requests with the *Authorization: Bearer <token\>* header
- Profit, depending on the role assigned to the account

Note that the __access token is valid for 1 day__ and is given
along with a __refresh token valid for 2 weeks__.

[Generate]: https://docs.joinpeertube.org/api/rest-getting-started
; scopes: admin

Parameters:

- `jobType` (path, JobType, required) - The type of the job to cancel
  Type: JobType
  
  Schema: [JobType](https://docs.joinpeertube.org/api-rest-reference/api/models/jobtype.md)
  
  enum: ["activitypub-cleaner","activitypub-follow","activitypub-http-broadcast","activitypub-http-broadcast-parallel","activitypub-http-fetcher","activitypub-http-unicast","activitypub-refresher","actor-keys","after-video-channel-import","build-automatic-tags","build-object-automatic-tags","create-user-export","email","federate-video","generate-video-storyboard","import-user-archive","manage-video-torrent","move-to-file-system","move-to-object-storage","notify","transcoding-job-builder","video-channel-import","video-file-import","video-files-lifecycle","video-import","video-live-ending","video-redundancy","video-studio-edition","video-transcoding","video-transcription","videos-stats"]

- `jobId` (path, string, required) - The id of the job to cancel
  Type: string

Responses:

- 204: successful operation

- 404: job not found

- 409: job cannot be cancelled (not active or not cancellable)

## Examples

### cURL

```bash
curl -X POST 'https://peertube2.cpy.re/api/v1/jobs/activitypub-cleaner/string/cancel' \
  -H 'Authorization: Bearer YOUR_TOKEN'
```

### JavaScript

```javascript
const response = await fetch('https://peertube2.cpy.re/api/v1/jobs/activitypub-cleaner/string/cancel', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN',
  },
});

const data = await response.text();
```

### Python

```python
import requests

response = requests.post(
    'https://peertube2.cpy.re/api/v1/jobs/activitypub-cleaner/string/cancel',
    headers={
        'Authorization': 'Bearer YOUR_TOKEN',
    },
)
print(response.text)
```
