Implement task scheduling system (#1427)
* Implement task scheduling system * Remove tests
This commit is contained in:
@@ -7,15 +7,23 @@ import {MetadataRefreshRequest} from '../../metadata/model/request/metadata-refr
|
||||
export enum TaskType {
|
||||
CLEAR_CBX_CACHE = 'CLEAR_CBX_CACHE',
|
||||
CLEAR_PDF_CACHE = 'CLEAR_PDF_CACHE',
|
||||
RE_SCAN_LIBRARY = 'RE_SCAN_LIBRARY',
|
||||
REFRESH_METADATA = 'REFRESH_METADATA',
|
||||
REFRESH_LIBRARY_METADATA = 'REFRESH_LIBRARY_METADATA',
|
||||
UPDATE_BOOK_RECOMMENDATIONS = 'UPDATE_BOOK_RECOMMENDATIONS',
|
||||
CLEANUP_DELETED_BOOKS = 'CLEANUP_DELETED_BOOKS',
|
||||
SYNC_LIBRARY_FILES = 'SYNC_LIBRARY_FILES',
|
||||
CLEANUP_TEMP_METADATA = 'CLEANUP_TEMP_METADATA',
|
||||
REFRESH_METADATA_MANUAL = 'REFRESH_METADATA_MANUAL'
|
||||
}
|
||||
|
||||
export const TASK_TYPE_CONFIG: Record<TaskType, { parallel: boolean; async: boolean }> = {
|
||||
[TaskType.CLEAR_CBX_CACHE]: {parallel: false, async: false},
|
||||
[TaskType.CLEAR_PDF_CACHE]: {parallel: false, async: false},
|
||||
[TaskType.RE_SCAN_LIBRARY]: {parallel: false, async: true},
|
||||
[TaskType.REFRESH_METADATA]: {parallel: false, async: true}
|
||||
export const TASK_TYPE_CONFIG: Record<TaskType, { parallel: boolean; async: boolean; displayOrder: number }> = {
|
||||
[TaskType.REFRESH_LIBRARY_METADATA]: {parallel: false, async: true, displayOrder: 1},
|
||||
[TaskType.SYNC_LIBRARY_FILES]: {parallel: false, async: false, displayOrder: 2},
|
||||
[TaskType.UPDATE_BOOK_RECOMMENDATIONS]: {parallel: false, async: true, displayOrder: 3},
|
||||
[TaskType.CLEANUP_DELETED_BOOKS]: {parallel: false, async: false, displayOrder: 4},
|
||||
[TaskType.CLEANUP_TEMP_METADATA]: {parallel: false, async: false, displayOrder: 5},
|
||||
[TaskType.REFRESH_METADATA_MANUAL]: {parallel: false, async: false, displayOrder: 6},
|
||||
[TaskType.CLEAR_CBX_CACHE]: {parallel: false, async: false, displayOrder: 7},
|
||||
[TaskType.CLEAR_PDF_CACHE]: {parallel: false, async: false, displayOrder: 8},
|
||||
};
|
||||
|
||||
export enum MetadataReplaceMode {
|
||||
@@ -41,7 +49,7 @@ export interface TaskCreateResponse {
|
||||
}
|
||||
|
||||
export interface TaskStatusResponse {
|
||||
tasks: Task[];
|
||||
taskHistories: TaskHistory[];
|
||||
}
|
||||
|
||||
export enum TaskStatus {
|
||||
@@ -53,23 +61,36 @@ export enum TaskStatus {
|
||||
PENDING = 'PENDING'
|
||||
}
|
||||
|
||||
export interface TaskMetadata {
|
||||
currentCacheSizeBytes?: number;
|
||||
currentCacheSize?: string;
|
||||
|
||||
[key: string]: any;
|
||||
export interface CronConfig {
|
||||
id: number | null;
|
||||
taskType: string;
|
||||
cronExpression: string | null;
|
||||
enabled: boolean;
|
||||
options: Record<string, any> | null;
|
||||
createdAt: string | null;
|
||||
updatedAt: string | null;
|
||||
}
|
||||
|
||||
export interface Task {
|
||||
export interface TaskInfo {
|
||||
taskType: string;
|
||||
name: string;
|
||||
description: string;
|
||||
parallel: boolean;
|
||||
async: boolean;
|
||||
cronSupported: boolean;
|
||||
cronConfig: CronConfig | null;
|
||||
metadata?: string | null;
|
||||
}
|
||||
|
||||
export interface TaskHistory {
|
||||
id: string | null;
|
||||
type: string;
|
||||
status: string | null;
|
||||
status: TaskStatus | null;
|
||||
progressPercentage: number | null;
|
||||
message: string | null;
|
||||
createdAt: string | null;
|
||||
updatedAt: string | null;
|
||||
completedAt: string | null;
|
||||
metadata: TaskMetadata;
|
||||
}
|
||||
|
||||
export interface TaskCancelResponse {
|
||||
@@ -78,6 +99,11 @@ export interface TaskCancelResponse {
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface TaskCronConfigRequest {
|
||||
cronExpression?: string | null;
|
||||
enabled?: boolean | null;
|
||||
}
|
||||
|
||||
export interface TaskProgressPayload {
|
||||
taskId: string;
|
||||
taskType: string;
|
||||
@@ -91,21 +117,29 @@ export interface TaskProgressPayload {
|
||||
})
|
||||
export class TaskService {
|
||||
private http = inject(HttpClient);
|
||||
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v2/tasks`;
|
||||
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/tasks`;
|
||||
|
||||
private taskProgressSubject = new BehaviorSubject<TaskProgressPayload | null>(null);
|
||||
public taskProgress$ = this.taskProgressSubject.asObservable();
|
||||
|
||||
getAvailableTasks(): Observable<TaskInfo[]> {
|
||||
return this.http.get<TaskInfo[]>(`${this.baseUrl}`);
|
||||
}
|
||||
|
||||
startTask(request: TaskCreateRequest): Observable<TaskCreateResponse> {
|
||||
return this.http.post<TaskCreateResponse>(this.baseUrl, request);
|
||||
return this.http.post<TaskCreateResponse>(`${this.baseUrl}/start`, request);
|
||||
}
|
||||
|
||||
getLatestTasksForEachType(): Observable<TaskStatusResponse> {
|
||||
return this.http.get<TaskStatusResponse>(`${this.baseUrl}/latest`);
|
||||
return this.http.get<TaskStatusResponse>(`${this.baseUrl}/last`);
|
||||
}
|
||||
|
||||
cancelTask(taskId: string): Observable<TaskCancelResponse> {
|
||||
return this.http.delete<TaskCancelResponse>(`${this.baseUrl}/${taskId}`);
|
||||
return this.http.delete<TaskCancelResponse>(`${this.baseUrl}/${taskId}/cancel`);
|
||||
}
|
||||
|
||||
updateCronConfig(taskType: string, request: TaskCronConfigRequest): Observable<CronConfig> {
|
||||
return this.http.patch<CronConfig>(`${this.baseUrl}/${taskType}/cron`, request);
|
||||
}
|
||||
|
||||
handleTaskProgress(progress: TaskProgressPayload): void {
|
||||
|
||||
Reference in New Issue
Block a user