allow superadmins to create and delete whitelisted job schedules
LocationBasedServices/smartguide/pipeline/head There was a failure building this commit
LocationBasedServices/smartguide/pipeline/head There was a failure building this commit
This commit is contained in:
@@ -83,6 +83,15 @@ class JobRun(SQLModel, table=True):
|
||||
)
|
||||
|
||||
|
||||
class JobDefinitionCreateRequest(BaseModel):
|
||||
name: str
|
||||
job: BackgroundJobName
|
||||
description: str = ""
|
||||
target_index: str = ""
|
||||
schedule: Optional[str] = None
|
||||
enabled: bool = True
|
||||
|
||||
|
||||
class JobDefinitionUpdateRequest(BaseModel):
|
||||
|
||||
schedule: Optional[str] = None
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, Query, Response, status
|
||||
from sqlmodel import Session
|
||||
|
||||
from inse.auth import service as auth_service
|
||||
@@ -10,6 +10,7 @@ from . import service
|
||||
from .model import (
|
||||
BackgroundJobTask,
|
||||
DockerHealth,
|
||||
JobDefinitionCreateRequest,
|
||||
JobDefinitionUpdateRequest,
|
||||
JobRun,
|
||||
JobRunLogs,
|
||||
@@ -30,6 +31,34 @@ async def get_tasks(
|
||||
return service.get_tasks(session, access_token)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/tasks",
|
||||
operation_id="createBackgroundJobTask",
|
||||
response_model=BackgroundJobTask,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
)
|
||||
async def create_task(
|
||||
payload: JobDefinitionCreateRequest,
|
||||
session: Session = Depends(get_session),
|
||||
access_token: AccessTokenMetadata = auth_service.CurrentUserAccessTokenPayloadDep,
|
||||
) -> BackgroundJobTask:
|
||||
return service.create_task(session, access_token, payload)
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/tasks/{task_id}",
|
||||
operation_id="deleteBackgroundJobTask",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
)
|
||||
async def delete_task(
|
||||
task_id: int,
|
||||
session: Session = Depends(get_session),
|
||||
access_token: AccessTokenMetadata = auth_service.CurrentUserAccessTokenPayloadDep,
|
||||
) -> Response:
|
||||
service.delete_task(session, access_token, task_id)
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/tasks/{task_id}",
|
||||
operation_id="updateBackgroundJobTask",
|
||||
|
||||
@@ -16,6 +16,7 @@ from .model import (
|
||||
BackgroundJobTask,
|
||||
DockerHealth,
|
||||
JobDefinition,
|
||||
JobDefinitionCreateRequest,
|
||||
JobDefinitionUpdateRequest,
|
||||
JobRun,
|
||||
JobRunLogs,
|
||||
@@ -97,6 +98,70 @@ def get_tasks(
|
||||
return [_to_task(session, definition) for definition in definitions]
|
||||
|
||||
|
||||
def create_task(
|
||||
session: Session,
|
||||
access_token: AccessTokenMetadata,
|
||||
payload: JobDefinitionCreateRequest,
|
||||
) -> BackgroundJobTask:
|
||||
_require_super_admin(access_token)
|
||||
name = payload.name.strip()
|
||||
if not name:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Task name must not be empty.",
|
||||
)
|
||||
if payload.schedule is not None and not croniter.is_valid(payload.schedule):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail=f"Invalid cron expression: '{payload.schedule}'.",
|
||||
)
|
||||
if session.exec(select(JobDefinition).where(JobDefinition.name == name)).first():
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"A task named '{name}' already exists.",
|
||||
)
|
||||
|
||||
definition = JobDefinition(
|
||||
name=name,
|
||||
job=payload.job,
|
||||
description=payload.description,
|
||||
target_index=payload.target_index,
|
||||
schedule=payload.schedule,
|
||||
enabled=payload.enabled,
|
||||
)
|
||||
if definition.enabled and definition.schedule:
|
||||
definition.next_run_at = runner.compute_next_run(definition.schedule)
|
||||
session.add(definition)
|
||||
session.commit()
|
||||
session.refresh(definition)
|
||||
logger.info("Created background job task '%s' (job=%s).", name, payload.job)
|
||||
return _to_task(session, definition)
|
||||
|
||||
|
||||
def delete_task(
|
||||
session: Session, access_token: AccessTokenMetadata, definition_id: int
|
||||
) -> None:
|
||||
_require_super_admin(access_token)
|
||||
definition = _get_definition(session, definition_id)
|
||||
if runner.has_running_run(session, definition.id):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=f"'{definition.name}' is running. Stop the run first.",
|
||||
)
|
||||
runs = session.exec(
|
||||
select(JobRun).where(JobRun.definition_id == definition.id)
|
||||
).all()
|
||||
for run in runs:
|
||||
session.delete(run)
|
||||
session.delete(definition)
|
||||
session.commit()
|
||||
logger.info(
|
||||
"Deleted background job task '%s' (%d runs removed).",
|
||||
definition.name,
|
||||
len(runs),
|
||||
)
|
||||
|
||||
|
||||
def update_task(
|
||||
session: Session,
|
||||
access_token: AccessTokenMetadata,
|
||||
|
||||
@@ -144,6 +144,7 @@ model/institutionNetworkNode.ts
|
||||
model/institutionPage.ts
|
||||
model/institutionsListResponse.ts
|
||||
model/ipcClassification.ts
|
||||
model/jobDefinitionCreateRequest.ts
|
||||
model/jobDefinitionUpdateRequest.ts
|
||||
model/jobRun.ts
|
||||
model/jobRunLogs.ts
|
||||
|
||||
@@ -29,6 +29,8 @@ import { DockerHealth } from '../model/dockerHealth';
|
||||
// @ts-ignore
|
||||
import { HTTPValidationError } from '../model/hTTPValidationError';
|
||||
// @ts-ignore
|
||||
import { JobDefinitionCreateRequest } from '../model/jobDefinitionCreateRequest';
|
||||
// @ts-ignore
|
||||
import { JobDefinitionUpdateRequest } from '../model/jobDefinitionUpdateRequest';
|
||||
// @ts-ignore
|
||||
import { JobRun } from '../model/jobRun';
|
||||
@@ -52,6 +54,251 @@ export class BackgroundJobsService extends BaseService {
|
||||
super(basePath, configuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Task
|
||||
* @endpoint post /api/background-jobs/tasks
|
||||
* @param jobDefinitionCreateRequest
|
||||
* @param secureFgpAcc
|
||||
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
|
||||
* @param reportProgress flag to report request and response progress.
|
||||
*/
|
||||
public createBackgroundJobTask(
|
||||
jobDefinitionCreateRequest: JobDefinitionCreateRequest,
|
||||
secureFgpAcc?: string,
|
||||
observe?: 'body',
|
||||
reportProgress?: boolean,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<BackgroundJobTask>;
|
||||
public createBackgroundJobTask(
|
||||
jobDefinitionCreateRequest: JobDefinitionCreateRequest,
|
||||
secureFgpAcc?: string,
|
||||
observe?: 'response',
|
||||
reportProgress?: boolean,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<HttpResponse<BackgroundJobTask>>;
|
||||
public createBackgroundJobTask(
|
||||
jobDefinitionCreateRequest: JobDefinitionCreateRequest,
|
||||
secureFgpAcc?: string,
|
||||
observe?: 'events',
|
||||
reportProgress?: boolean,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<HttpEvent<BackgroundJobTask>>;
|
||||
public createBackgroundJobTask(
|
||||
jobDefinitionCreateRequest: JobDefinitionCreateRequest,
|
||||
secureFgpAcc?: string,
|
||||
observe: any = 'body',
|
||||
reportProgress: boolean = false,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<any> {
|
||||
if (
|
||||
jobDefinitionCreateRequest === null ||
|
||||
jobDefinitionCreateRequest === undefined
|
||||
) {
|
||||
throw new Error(
|
||||
'Required parameter jobDefinitionCreateRequest was null or undefined when calling createBackgroundJobTask.',
|
||||
);
|
||||
}
|
||||
|
||||
let localVarHeaders = this.defaultHeaders;
|
||||
|
||||
// authentication (HTTPBearer) required
|
||||
localVarHeaders = this.configuration.addCredentialToHeaders(
|
||||
'HTTPBearer',
|
||||
'Authorization',
|
||||
localVarHeaders,
|
||||
'Bearer ',
|
||||
);
|
||||
|
||||
const localVarHttpHeaderAcceptSelected: string | undefined =
|
||||
options?.httpHeaderAccept ??
|
||||
this.configuration.selectHeaderAccept(['application/json']);
|
||||
if (localVarHttpHeaderAcceptSelected !== undefined) {
|
||||
localVarHeaders = localVarHeaders.set(
|
||||
'Accept',
|
||||
localVarHttpHeaderAcceptSelected,
|
||||
);
|
||||
}
|
||||
|
||||
const localVarHttpContext: HttpContext =
|
||||
options?.context ?? new HttpContext();
|
||||
|
||||
const localVarTransferCache: boolean = options?.transferCache ?? true;
|
||||
|
||||
// to determine the Content-Type header
|
||||
const consumes: string[] = ['application/json'];
|
||||
const httpContentTypeSelected: string | undefined =
|
||||
this.configuration.selectHeaderContentType(consumes);
|
||||
if (httpContentTypeSelected !== undefined) {
|
||||
localVarHeaders = localVarHeaders.set(
|
||||
'Content-Type',
|
||||
httpContentTypeSelected,
|
||||
);
|
||||
}
|
||||
|
||||
let responseType_: 'text' | 'json' | 'blob' = 'json';
|
||||
if (localVarHttpHeaderAcceptSelected) {
|
||||
if (localVarHttpHeaderAcceptSelected.startsWith('text')) {
|
||||
responseType_ = 'text';
|
||||
} else if (
|
||||
this.configuration.isJsonMime(localVarHttpHeaderAcceptSelected)
|
||||
) {
|
||||
responseType_ = 'json';
|
||||
} else {
|
||||
responseType_ = 'blob';
|
||||
}
|
||||
}
|
||||
|
||||
let localVarPath = `/api/background-jobs/tasks`;
|
||||
const { basePath, withCredentials } = this.configuration;
|
||||
return this.httpClient.request<BackgroundJobTask>(
|
||||
'post',
|
||||
`${basePath}${localVarPath}`,
|
||||
{
|
||||
context: localVarHttpContext,
|
||||
body: jobDefinitionCreateRequest,
|
||||
responseType: <any>responseType_,
|
||||
...(withCredentials ? { withCredentials } : {}),
|
||||
headers: localVarHeaders,
|
||||
observe: observe,
|
||||
...(localVarTransferCache !== undefined
|
||||
? { transferCache: localVarTransferCache }
|
||||
: {}),
|
||||
reportProgress: reportProgress,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Task
|
||||
* @endpoint delete /api/background-jobs/tasks/{task_id}
|
||||
* @param taskId
|
||||
* @param secureFgpAcc
|
||||
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
|
||||
* @param reportProgress flag to report request and response progress.
|
||||
*/
|
||||
public deleteBackgroundJobTask(
|
||||
taskId: number,
|
||||
secureFgpAcc?: string,
|
||||
observe?: 'body',
|
||||
reportProgress?: boolean,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<any>;
|
||||
public deleteBackgroundJobTask(
|
||||
taskId: number,
|
||||
secureFgpAcc?: string,
|
||||
observe?: 'response',
|
||||
reportProgress?: boolean,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<HttpResponse<any>>;
|
||||
public deleteBackgroundJobTask(
|
||||
taskId: number,
|
||||
secureFgpAcc?: string,
|
||||
observe?: 'events',
|
||||
reportProgress?: boolean,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<HttpEvent<any>>;
|
||||
public deleteBackgroundJobTask(
|
||||
taskId: number,
|
||||
secureFgpAcc?: string,
|
||||
observe: any = 'body',
|
||||
reportProgress: boolean = false,
|
||||
options?: {
|
||||
httpHeaderAccept?: 'application/json';
|
||||
context?: HttpContext;
|
||||
transferCache?: boolean;
|
||||
},
|
||||
): Observable<any> {
|
||||
if (taskId === null || taskId === undefined) {
|
||||
throw new Error(
|
||||
'Required parameter taskId was null or undefined when calling deleteBackgroundJobTask.',
|
||||
);
|
||||
}
|
||||
|
||||
let localVarHeaders = this.defaultHeaders;
|
||||
|
||||
// authentication (HTTPBearer) required
|
||||
localVarHeaders = this.configuration.addCredentialToHeaders(
|
||||
'HTTPBearer',
|
||||
'Authorization',
|
||||
localVarHeaders,
|
||||
'Bearer ',
|
||||
);
|
||||
|
||||
const localVarHttpHeaderAcceptSelected: string | undefined =
|
||||
options?.httpHeaderAccept ??
|
||||
this.configuration.selectHeaderAccept(['application/json']);
|
||||
if (localVarHttpHeaderAcceptSelected !== undefined) {
|
||||
localVarHeaders = localVarHeaders.set(
|
||||
'Accept',
|
||||
localVarHttpHeaderAcceptSelected,
|
||||
);
|
||||
}
|
||||
|
||||
const localVarHttpContext: HttpContext =
|
||||
options?.context ?? new HttpContext();
|
||||
|
||||
const localVarTransferCache: boolean = options?.transferCache ?? true;
|
||||
|
||||
let responseType_: 'text' | 'json' | 'blob' = 'json';
|
||||
if (localVarHttpHeaderAcceptSelected) {
|
||||
if (localVarHttpHeaderAcceptSelected.startsWith('text')) {
|
||||
responseType_ = 'text';
|
||||
} else if (
|
||||
this.configuration.isJsonMime(localVarHttpHeaderAcceptSelected)
|
||||
) {
|
||||
responseType_ = 'json';
|
||||
} else {
|
||||
responseType_ = 'blob';
|
||||
}
|
||||
}
|
||||
|
||||
let localVarPath = `/api/background-jobs/tasks/${this.configuration.encodeParam({ name: 'taskId', value: taskId, in: 'path', style: 'simple', explode: false, dataType: 'number', dataFormat: undefined })}`;
|
||||
const { basePath, withCredentials } = this.configuration;
|
||||
return this.httpClient.request<any>(
|
||||
'delete',
|
||||
`${basePath}${localVarPath}`,
|
||||
{
|
||||
context: localVarHttpContext,
|
||||
responseType: <any>responseType_,
|
||||
...(withCredentials ? { withCredentials } : {}),
|
||||
headers: localVarHeaders,
|
||||
observe: observe,
|
||||
...(localVarTransferCache !== undefined
|
||||
? { transferCache: localVarTransferCache }
|
||||
: {}),
|
||||
reportProgress: reportProgress,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Run Logs
|
||||
* @endpoint get /api/background-jobs/runs/{run_id}/logs
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Innovation Search Engine API
|
||||
*
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
import { BackgroundJobName } from './backgroundJobName';
|
||||
|
||||
export interface JobDefinitionCreateRequest {
|
||||
name: string;
|
||||
job: BackgroundJobName;
|
||||
description?: string;
|
||||
target_index?: string;
|
||||
schedule?: string | null;
|
||||
enabled?: boolean;
|
||||
}
|
||||
@@ -115,6 +115,7 @@ export * from './institutionNetworkNode';
|
||||
export * from './institutionPage';
|
||||
export * from './institutionsListResponse';
|
||||
export * from './ipcClassification';
|
||||
export * from './jobDefinitionCreateRequest';
|
||||
export * from './jobDefinitionUpdateRequest';
|
||||
export * from './jobRun';
|
||||
export * from './jobRunLogs';
|
||||
|
||||
Reference in New Issue
Block a user