Skip to content

Commit

Permalink
Add execution info query to job api (#3792)
Browse files Browse the repository at this point in the history
This PR makes the following changes:

# teraslice
## New Features
- Adds `ex` query parameter to the `v1/jobs/:job_id` api endpoint
- It's now possible to to get a job with specific information about the
current execution in one request
## Documentation
- Adds docs on how to use new `ex` query parameter in v1 jobs api
endpoint

---------

Co-authored-by: Austin Godber <[email protected]>
  • Loading branch information
sotojn and godber authored Oct 17, 2024
1 parent 09febc5 commit a0135ca
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
34 changes: 34 additions & 0 deletions docs/management-apis/endpoints-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ $ curl 'localhost:5678/v1/jobs'

Returns the job that matches given job id.

**Query Options:**

- `ex: string = [execution controller field options]`

You can pass in a query using `ex` which takes field options for what you want returned in the `ex` object. This gives information on the current execution accociated with the specified job. If no execution is present, it will return the `ex` field with an empty object. If no fields are passed in, it will return all fields. Fields MUST be separated with commas. Example: `localhost:5678/v1/jobs/<job_id>?ex=_status,assets`

Look at the returned object in [GET v1/ex](#get-v1ex) for valid field names.


**Usage:**

```sh
Expand All @@ -309,6 +318,30 @@ $ curl 'localhost:5678/v1/jobs/5a50580c-4a50-48d9-80f8-ac70a00f3dbd'
"_context": "job"
}
```
When getting a job with current execution info:
```sh
$ curl 'localhost:5678/v1/jobs/5a50580c-4a50-48d9-80f8-ac70a00f3dbd?ex=assets,_status'
{
"name": "Example",
"lifecycle": "persistent",
"workers": 1,
"operations": [
{
"_op": "noop"
}
]
"job_id": "5a50580c-4a50-48d9-80f8-ac70a00f3dbd",
"_created": "2018-09-21T17:49:05.029Z",
"_updated": "2018-11-01T13:15:22.743Z",
"_context": "job",
"ex": {
"assets": [
"74dcba12408fc02868d8c88b15be8a386092091b"
],
"_status": "running"
}
}
```

**NOTE:** Jobs without the `active` property are treated the same as jobs with
the `active` property set to `true`.
Expand Down Expand Up @@ -952,3 +985,4 @@ $ curl 'localhost:5678/v1/ex/1cb20d4c-520a-44fe-a802-313f41dd5b05/controller'
}
]
```

5 changes: 4 additions & 1 deletion packages/teraslice/src/lib/cluster/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,13 @@ export class ApiService {
});

v1routes.get('/jobs/:jobId', (req, res) => {
const { ex } = req.query;
const { jobId } = req.params;
// @ts-expect-error
const requestHandler = handleTerasliceRequest(req as TerasliceRequest, res, 'Could not retrieve job');
requestHandler(async () => this.jobsStorage.get(jobId));
typeof ex === 'string'
? requestHandler(async () => this.jobsService.getJobWithExInfo(jobId, ex.split(',')))
: requestHandler(async () => this.jobsStorage.get(jobId));
});

v1routes.put('/jobs/:jobId', (req, res) => {
Expand Down
31 changes: 31 additions & 0 deletions packages/teraslice/src/lib/cluster/services/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,37 @@ export class JobsService {
return ex[0];
}

/**
* Get the job with the latest ex status
*
* @param {string} jobId
* @param {string} [fields]
* @returns {Promise<JobConfig>}
*/
async getJobWithExInfo(
jobId: string,
fields?: string[],
): Promise<JobConfig> {
if (!jobId || !isString(jobId)) {
throw new TSError(`Invalid job id, got ${getTypeOf(jobId)}`);
}

const job = await this.jobsStorage.get(jobId);

const ex = await this.executionStorage.search(
`job_id: "${jobId}"`, undefined, 1, '_created:desc', fields || undefined
) as ExecutionConfig[];

if (!ex.length || ex[0]._deleted === true) {
job.ex = {};
return job;
}

job.ex = ex[0];

return job;
}

/**
* Get the active execution
*
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/teraslice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface JobConfig extends ValidatedJobConfig {
_updated: string | Date;
_deleted?: boolean;
_deleted_on?: string | Date;
ex?: any;
}

export enum RecoveryCleanupType {
Expand Down

0 comments on commit a0135ca

Please sign in to comment.