78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
|
|
import { BookBackTaskStatus, BookBackTaskType, TaskExecuteType } from "../../../define/enum/bookEnum";
|
|||
|
|
import { BookBackTaskListService } from "../../../define/db/service/Book/bookBackTaskListService";
|
|||
|
|
import { Book } from "../../../model/book";
|
|||
|
|
|
|||
|
|
export default class BookBackTaskServiceBasic {
|
|||
|
|
bookBackTaskListService: BookBackTaskListService
|
|||
|
|
constructor() { }
|
|||
|
|
|
|||
|
|
private async InitService() {
|
|||
|
|
if (!this.bookBackTaskListService) {
|
|||
|
|
this.bookBackTaskListService = await BookBackTaskListService.getInstance()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 添加一个后台任务
|
|||
|
|
* @param bookId 小说ID
|
|||
|
|
* @param taskType 后台任务类型
|
|||
|
|
* @param executeType 执行的类型,是不是自动执行
|
|||
|
|
* @param bookTaskId 小说批次任务ID
|
|||
|
|
* @param bookTaskDetailId 小说批次任务分镜ID
|
|||
|
|
* @param responseMessageName 消息名称
|
|||
|
|
*/
|
|||
|
|
async AddBookBackTask(bookId: string,
|
|||
|
|
taskType: BookBackTaskType,
|
|||
|
|
executeType = TaskExecuteType.AUTO,
|
|||
|
|
bookTaskId = null,
|
|||
|
|
bookTaskDetailId = null,
|
|||
|
|
responseMessageName: string = null
|
|||
|
|
): Promise<TaskModal.Task> {
|
|||
|
|
await this.InitService();
|
|||
|
|
let res = this.bookBackTaskListService.AddBookBackTask(bookId, taskType, executeType, bookTaskId, bookTaskDetailId, responseMessageName)
|
|||
|
|
if (res.code == 0) {
|
|||
|
|
throw new Error(res.message)
|
|||
|
|
}
|
|||
|
|
return res.data as TaskModal.Task
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取指定状态的任务数量
|
|||
|
|
* @param status
|
|||
|
|
* @returns
|
|||
|
|
*/
|
|||
|
|
async GetAllStatusTaskCount(status: BookBackTaskStatus[]): Promise<number> {
|
|||
|
|
await this.InitService();
|
|||
|
|
let count = this.bookBackTaskListService.GetAllStatusTaskCount(status)
|
|||
|
|
return count;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 修改后台队列的状态
|
|||
|
|
* @param bookBackTask 要修改的数据
|
|||
|
|
*/
|
|||
|
|
async UpdateTaskStatus(bookBackTask: Book.UpdateBookTaskListStatus) {
|
|||
|
|
await this.InitService();
|
|||
|
|
this.bookBackTaskListService.UpdateTaskStatus(bookBackTask)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 丢弃等待中和重新连接的任务
|
|||
|
|
*/
|
|||
|
|
async GiveUpNotStartBackTask() {
|
|||
|
|
await this.InitService();
|
|||
|
|
let task = this.bookBackTaskListService.realm.objects('BookBackTaskList').filtered('status == $0 || status == $1', BookBackTaskStatus.WAIT, BookBackTaskStatus.RECONNECT);
|
|||
|
|
let ids = task.map((item) => {
|
|||
|
|
return item.id
|
|||
|
|
})
|
|||
|
|
// 讲所有的人物状态改为放弃,然后errmessage改为 此任务已丢弃
|
|||
|
|
this.bookBackTaskListService.transaction(() => {
|
|||
|
|
for (let i = 0; i < ids.length; i++) {
|
|||
|
|
const element = this.bookBackTaskListService.realm.objectForPrimaryKey('BookBackTaskList', ids[i]);
|
|||
|
|
element.status = BookBackTaskStatus.FAIL
|
|||
|
|
element.errorMessage = '任务被丢弃'
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
}
|