From dee68ffa1ff1e976e55280026ca519ace2ea0a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Wed, 26 Nov 2025 15:00:30 +0800 Subject: [PATCH] 1 --- api/controller_admin/task_status.js | 130 ++++++++++++++++------------ 1 file changed, 76 insertions(+), 54 deletions(-) diff --git a/api/controller_admin/task_status.js b/api/controller_admin/task_status.js index 281640b..a566fd4 100644 --- a/api/controller_admin/task_status.js +++ b/api/controller_admin/task_status.js @@ -88,20 +88,21 @@ module.exports = { const { limit, offset } = ctx.getPageSize(); -const where = {}; -if (taskType) where.taskType = taskType; -if (status) where.status = status; + const where = {}; + if (taskType) where.taskType = taskType; + if (status) where.status = status; -// 支持多种搜索字段 -if (taskName) { - where.taskName = { [op.like]: `%${taskName}%` }; -} -if (taskId) { - where.taskId = { [op.like]: `%${taskId}%` }; -} -if (sn_code) { - where.sn_code = { [op.like]: `%${sn_code}%` }; -} + // 支持多种搜索字段 + if (taskName) { + where.taskName = { [op.like]: `%${taskName}%` }; + } + if (taskId) { + // 使用 id 字段搜索(数据库主键) + where.id = { [op.like]: `%${taskId}%` }; + } + if (sn_code) { + where.sn_code = { [op.like]: `%${sn_code}%` }; + } const result = await task_status.findAndCountAll({ where, @@ -213,15 +214,14 @@ return ctx.success({ return ctx.fail('任务ID不能为空'); } - -const task = await task_status.findOne({ where: { taskId } }); + // 使用 id 字段查询(数据库主键) + const task = await task_status.findOne({ where: { id: taskId } }); -if (!task) { - return ctx.fail('任务不存在'); -} + if (!task) { + return ctx.fail('任务不存在'); + } -return ctx.success(task); - + return ctx.success(task); }, /** @@ -260,27 +260,24 @@ return ctx.success(task); return ctx.fail('任务ID不能为空'); } - -const updateData = { + const updateData = {}; -}; + if (status) { + updateData.status = status; + if (status === 'completed') { + updateData.endTime = new Date(); + updateData.progress = 100; + } else if (status === 'failed') { + updateData.endTime = new Date(); + } + } + if (progress !== undefined) updateData.progress = progress; + if (errorMessage) updateData.errorMessage = errorMessage; -if (status) { - updateData.status = status; - if (status === 'completed') { - updateData.endTime = new Date(); - updateData.progress = 100; - } else if (status === 'failed') { - updateData.endTime = new Date(); - } -} -if (progress !== undefined) updateData.progress = progress; -if (errorMessage) updateData.errorMessage = errorMessage; + // 使用 id 字段更新(数据库主键) + await task_status.update(updateData, { where: { id: taskId } }); -await task_status.update(updateData, { where: { taskId } }); - -return ctx.success({ message: '任务状态更新成功' }); - + return ctx.success({ message: '任务状态更新成功' }); }, /** @@ -316,15 +313,14 @@ return ctx.success({ message: '任务状态更新成功' }); return ctx.fail('任务ID不能为空'); } - -const result = await task_status.destroy({ where: { taskId } }); + // 使用 id 字段删除(数据库主键) + const result = await task_status.destroy({ where: { id: taskId } }); -if (result === 0) { - return ctx.fail('任务不存在'); -} + if (result === 0) { + return ctx.fail('任务不存在'); + } -return ctx.success({ message: '任务删除成功' }); - + return ctx.success({ message: '任务删除成功' }); }, /** @@ -360,7 +356,8 @@ return ctx.success({ message: '任务删除成功' }); return ctx.fail('任务ID不能为空'); } - const task = await task_status.findOne({ where: { taskId } }); + // 使用 id 字段查询(数据库主键) + const task = await task_status.findOne({ where: { id: taskId } }); if (!task) { return ctx.fail('任务不存在'); @@ -370,10 +367,33 @@ return ctx.success({ message: '任务删除成功' }); return ctx.fail('只能取消待执行或执行中的任务'); } - await task_status.update({ - status: 'cancelled', - endTime: new Date(), - }, { where: { taskId } }); + // 尝试从任务队列中取消任务(如果任务在内存队列中) + try { + const scheduleManager = require('../middleware/schedule/index'); + if (scheduleManager && scheduleManager.taskQueue) { + const cancelled = await scheduleManager.taskQueue.cancelTask(taskId); + if (!cancelled) { + // 如果任务不在队列中,直接更新数据库 + await task_status.update({ + status: 'cancelled', + endTime: new Date(), + }, { where: { id: taskId } }); + } + } else { + // 如果没有任务队列,直接更新数据库 + await task_status.update({ + status: 'cancelled', + endTime: new Date(), + }, { where: { id: taskId } }); + } + } catch (error) { + console.error('[取消任务] 取消任务失败:', error); + // 即使队列操作失败,也尝试更新数据库 + await task_status.update({ + status: 'cancelled', + endTime: new Date(), + }, { where: { id: taskId } }); + } return ctx.success({ message: '任务取消成功' }); }, @@ -405,13 +425,15 @@ return ctx.success({ message: '任务删除成功' }); const models = Framework.getModels(); const { task_status } = models; const body = ctx.getBody(); - const { id } = body; + // 兼容 taskId 和 id 参数 + const task_id = body.taskId || body.id; - if (!id) { + if (!task_id) { return ctx.fail('任务ID不能为空'); } - const task = await task_status.findOne({ where: { id } }); + // 使用 id 字段查询(数据库主键) + const task = await task_status.findOne({ where: { id: task_id } }); if (!task) { return ctx.fail('任务不存在'); @@ -428,7 +450,7 @@ return ctx.success({ message: '任务删除成功' }); errorStack: '', startTime: null, endTime: null - }, { where: { id } }); + }, { where: { id: task_id } }); return ctx.success({ message: '任务重试成功' }); },