import { BookBackTaskListService } from '../../define/db/service/Book/bookBackTaskListService' import { BookService } from '../../define/db/service/Book/bookService' import { BookTaskDetailService } from '../../define/db/service/Book/bookTaskDetailService' import { BookTaskService } from '../../define/db/service/Book/bookTaskService' import { OtherData } from '../../define/enum/softwareEnum' import { BookBackTaskStatus } from '../../define/enum/bookEnum' class TaskManager { constructor() {} /** * 创建新任务到数据库 * 需要传递 小说ID,小说任务ID,小说任务分镜ID * 判断是不是有相同的任务在执行,如果有则不创建新任务 * 小说任务ID为null,所有相关的数据就是default * 小说任务分镜ID为null,所有相关的数据就是default * @param {*} bookId 小说ID 必传 * @param {*} taskType 任务类型 必传 * @param {*} bookTaskId 小说任务ID 可为null * @param {*} bookTaskDetailId 小说任务分镜ID 可为null * @returns */ async AddTask(bookId, taskType, bookTaskId = null, bookTaskDetailId = null) { try { // 开始创建任务 let _bookBackTaskListService = await BookBackTaskListService.getInstance() let _bookService = await BookService.getInstance() let _bookTaskService = await BookTaskService.getInstance() let _bookTaskDetailService = await BookTaskDetailService.getInstance() // 获取小说信息 let book = _bookService.GetBookDataById(bookId) if (book == null) { throw new Error('小说信息不存在,添加任务失败') } // 有传入小说批次任务ID,要检查数据是不是存在 let bookTask = null if (bookTaskId != null) { let bookTaskRes = _bookTaskService.GetBookTaskDataById(bookTaskId) if (bookTaskRes.data == null) { throw new Error('小说批次任务信息不存在,添加任务失败') } bookTask = bookTaskRes.data } let bookTaskDetail = null if (bookTaskDetailId != null) { let bookTaskDetailRes = _bookTaskDetailService.GetBookTaskDetailDataById(bookTaskDetailId) if (bookTaskDetailRes.data == null) { throw new Error('小说任务分镜信息不存在,添加任务失败') } bookTaskDetail = bookTaskDetailRes.data } // 开始往数据库中添加任务 let name = `${book.name}-${bookTask ? bookTask.name : 'default'}-${ bookTaskDetail ? bookTaskDetail.name : 'default' }-${taskType}` let addBookBackTaskListRes = _bookBackTaskListService.AddBookBackTaskList({ bookId: bookId, bookTaskId: bookTaskId ? bookTaskId : OtherData.DEFAULT, name: name, type: taskType, status: BookBackTaskStatus.WAIT }) if (addBookBackTaskListRes.code == 1) { return addBookBackTaskListRes } else { throw new Error('添加任务失败') } } catch (error) { throw error } } /** * 获取指定小说和小说批次任务中等待中的任务 * @param {*} bookId * @param {*} bookTaskId */ async GetWaitTask(bookId, bookTaskId = null) { try { if (bookId == null) { throw new Error('bookId不能为空') } let query = { bookId: bookId, status: BookBackTaskStatus.WAIT } if (bookTaskId != null) { query.bookTaskId = bookTaskId } let _bookBackTaskListService = await BookBackTaskListService.getInstance() } catch (error) { throw error } } updateTaskStatus(taskId, batchId, subBatchId, status) { return new Promise((resolve, reject) => { this.db.run( `UPDATE tasks SET status = ?, updatedAt = datetime('now') WHERE taskId = ? AND batchId = ? AND subBatchId = ?`, [status, taskId, batchId, subBatchId], function (err) { if (err) { reject(err) } else { resolve() } } ) }) } deleteTask(taskId, batchId, subBatchId) { return new Promise((resolve, reject) => { this.db.run( `DELETE FROM tasks WHERE taskId = ? AND batchId = ? AND subBatchId = ?`, [taskId, batchId, subBatchId], function (err) { if (err) { reject(err) } else { resolve() } } ) }) } getAllTasks() { return new Promise((resolve, reject) => { this.db.all(`SELECT * FROM tasks`, [], (err, rows) => { if (err) { reject(err) } else { resolve(rows) } }) }) } } class TaskExecutor { constructor(taskManager) { this.taskManager = taskManager this.isExecuting = false } async executePendingTasks() { if (this.isExecuting) { console.log('任务正在执行,跳过此次执行') return } this.isExecuting = true try { const tasks = await this.taskManager.getAllTasks() for (const task of tasks) { if (task.status === 'pending') { console.log(`Executing task: ${task.taskId}`) await this.handleTask(task) await this.taskManager.updateTaskStatus( task.taskId, task.batchId, task.subBatchId, 'completed' ) } } } catch (err) { console.error('Error executing tasks:', err) } finally { this.isExecuting = false } } async handleTask(task) { if (task.taskId === 'specificTask') { console.log(`Handling specific task: ${task.taskId}`) // 执行任务的具体逻辑 } else { console.log(`Handling general task: ${task.taskId}`) // 执行任务的具体逻辑 } } } const taskManager = new TaskManager() const taskExecutor = new TaskExecutor(taskManager) // 每分钟检查并执行一次任务 setInterval(() => { taskExecutor.executePendingTasks().catch(console.error) }, 60 * 1000) // 示例:创建任务并触发执行 taskManager .createTask('task1', 'batch1', 'subBatch1') .then((taskId) => { if (taskId) { console.log('Task created with ID:', taskId) } else { console.log('没有创建新任务') } }) .catch((err) => console.error(err))