1
This commit is contained in:
@@ -88,20 +88,21 @@ module.exports = {
|
|||||||
const { limit, offset } = ctx.getPageSize();
|
const { limit, offset } = ctx.getPageSize();
|
||||||
|
|
||||||
|
|
||||||
const where = {};
|
const where = {};
|
||||||
if (taskType) where.taskType = taskType;
|
if (taskType) where.taskType = taskType;
|
||||||
if (status) where.status = status;
|
if (status) where.status = status;
|
||||||
|
|
||||||
// 支持多种搜索字段
|
// 支持多种搜索字段
|
||||||
if (taskName) {
|
if (taskName) {
|
||||||
where.taskName = { [op.like]: `%${taskName}%` };
|
where.taskName = { [op.like]: `%${taskName}%` };
|
||||||
}
|
}
|
||||||
if (taskId) {
|
if (taskId) {
|
||||||
where.taskId = { [op.like]: `%${taskId}%` };
|
// 使用 id 字段搜索(数据库主键)
|
||||||
}
|
where.id = { [op.like]: `%${taskId}%` };
|
||||||
if (sn_code) {
|
}
|
||||||
where.sn_code = { [op.like]: `%${sn_code}%` };
|
if (sn_code) {
|
||||||
}
|
where.sn_code = { [op.like]: `%${sn_code}%` };
|
||||||
|
}
|
||||||
|
|
||||||
const result = await task_status.findAndCountAll({
|
const result = await task_status.findAndCountAll({
|
||||||
where,
|
where,
|
||||||
@@ -213,15 +214,14 @@ return ctx.success({
|
|||||||
return ctx.fail('任务ID不能为空');
|
return ctx.fail('任务ID不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 使用 id 字段查询(数据库主键)
|
||||||
const task = await task_status.findOne({ where: { taskId } });
|
const task = await task_status.findOne({ where: { id: taskId } });
|
||||||
|
|
||||||
if (!task) {
|
if (!task) {
|
||||||
return ctx.fail('任务不存在');
|
return ctx.fail('任务不存在');
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.success(task);
|
return ctx.success(task);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -260,27 +260,24 @@ return ctx.success(task);
|
|||||||
return ctx.fail('任务ID不能为空');
|
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) {
|
// 使用 id 字段更新(数据库主键)
|
||||||
updateData.status = status;
|
await task_status.update(updateData, { where: { id: taskId } });
|
||||||
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;
|
|
||||||
|
|
||||||
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不能为空');
|
return ctx.fail('任务ID不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 使用 id 字段删除(数据库主键)
|
||||||
const result = await task_status.destroy({ where: { taskId } });
|
const result = await task_status.destroy({ where: { id: taskId } });
|
||||||
|
|
||||||
if (result === 0) {
|
if (result === 0) {
|
||||||
return ctx.fail('任务不存在');
|
return ctx.fail('任务不存在');
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.success({ message: '任务删除成功' });
|
return ctx.success({ message: '任务删除成功' });
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,7 +356,8 @@ return ctx.success({ message: '任务删除成功' });
|
|||||||
return ctx.fail('任务ID不能为空');
|
return ctx.fail('任务ID不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
const task = await task_status.findOne({ where: { taskId } });
|
// 使用 id 字段查询(数据库主键)
|
||||||
|
const task = await task_status.findOne({ where: { id: taskId } });
|
||||||
|
|
||||||
if (!task) {
|
if (!task) {
|
||||||
return ctx.fail('任务不存在');
|
return ctx.fail('任务不存在');
|
||||||
@@ -370,10 +367,33 @@ return ctx.success({ message: '任务删除成功' });
|
|||||||
return ctx.fail('只能取消待执行或执行中的任务');
|
return ctx.fail('只能取消待执行或执行中的任务');
|
||||||
}
|
}
|
||||||
|
|
||||||
await task_status.update({
|
// 尝试从任务队列中取消任务(如果任务在内存队列中)
|
||||||
status: 'cancelled',
|
try {
|
||||||
endTime: new Date(),
|
const scheduleManager = require('../middleware/schedule/index');
|
||||||
}, { where: { taskId } });
|
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: '任务取消成功' });
|
return ctx.success({ message: '任务取消成功' });
|
||||||
},
|
},
|
||||||
@@ -405,13 +425,15 @@ return ctx.success({ message: '任务删除成功' });
|
|||||||
const models = Framework.getModels();
|
const models = Framework.getModels();
|
||||||
const { task_status } = models;
|
const { task_status } = models;
|
||||||
const body = ctx.getBody();
|
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不能为空');
|
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) {
|
if (!task) {
|
||||||
return ctx.fail('任务不存在');
|
return ctx.fail('任务不存在');
|
||||||
@@ -428,7 +450,7 @@ return ctx.success({ message: '任务删除成功' });
|
|||||||
errorStack: '',
|
errorStack: '',
|
||||||
startTime: null,
|
startTime: null,
|
||||||
endTime: null
|
endTime: null
|
||||||
}, { where: { id } });
|
}, { where: { id: task_id } });
|
||||||
|
|
||||||
return ctx.success({ message: '任务重试成功' });
|
return ctx.success({ message: '任务重试成功' });
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user