refactor(scheduler): 优化巡检任务调度逻辑
- 移除任务状态自动规范化逻辑 - 不再修改任务状态字段 - 仅根据任务状态 0=启用、1=停用 控制触发 - 移除运行状态缓存机制- 注释掉任务状态更新相关代码 - 调整任务记录失败状态值为2
This commit is contained in:
@@ -24,9 +24,10 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 内部巡检任务调度器(不依赖若依Quartz)
|
* 内部巡检任务调度器(不依赖若依Quartz)
|
||||||
* - 周期性轮询 v_inspection_task 中启用(status='0')的任务
|
* - 周期性轮询 v_inspection_task 中任务,按状态 0=启用、1=停用 控制是否触发
|
||||||
* - 使用 Spring CronExpression 计算下一次执行
|
* - 使用 Spring CronExpression 计算下一次执行
|
||||||
* - 到点调用 InspectionTaskService.executeInspectionTask(taskId)
|
* - 到点调用 InspectionTaskService.executeInspectionTask(taskId)
|
||||||
|
* - 不修改任务的状态字段(状态仅由外部手动启停控制)
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@@ -119,21 +120,6 @@ public class InspectionCronScheduler {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.debug("更新nextExecuteTime失败: taskId={}, err={}", taskId, e.getMessage());
|
log.debug("更新nextExecuteTime失败: taskId={}, err={}", taskId, e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 若发现状态为“2”(服务执行完成后置状态),将其规范化回“0”以维持启用
|
|
||||||
if ("2".equals(status)) {
|
|
||||||
try {
|
|
||||||
InspectionTask patch = new InspectionTask();
|
|
||||||
patch.setTaskId(taskId);
|
|
||||||
patch.setStatus("0");
|
|
||||||
patch.setUpdateTime(DateUtils.getNowDate());
|
|
||||||
inspectionTaskMapper.updateInspectionTask(patch);
|
|
||||||
task.setStatus("0");
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.debug("规范化任务状态失败: taskId={}, err={}", taskId, e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 到点触发判定:如果 next <= now + 10s 窗口内,就触发一次
|
// 到点触发判定:如果 next <= now + 10s 窗口内,就触发一次
|
||||||
long nowMs = System.currentTimeMillis();
|
long nowMs = System.currentTimeMillis();
|
||||||
long nextMs = nextDate.getTime();
|
long nextMs = nextDate.getTime();
|
||||||
@@ -164,19 +150,6 @@ public class InspectionCronScheduler {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("执行巡检任务触发失败: taskId={}, err={}", taskId, e.getMessage(), e);
|
log.error("执行巡检任务触发失败: taskId={}, err={}", taskId, e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 延迟规范化状态为启用(服务执行期间可能将其改为1或2)
|
|
||||||
try {
|
|
||||||
poller.schedule(() -> {
|
|
||||||
try {
|
|
||||||
InspectionTask patch2 = new InspectionTask();
|
|
||||||
patch2.setTaskId(taskId);
|
|
||||||
patch2.setStatus("0");
|
|
||||||
patch2.setUpdateTime(DateUtils.getNowDate());
|
|
||||||
inspectionTaskMapper.updateInspectionTask(patch2);
|
|
||||||
} catch (Exception ignored) {}
|
|
||||||
}, 3, TimeUnit.SECONDS);
|
|
||||||
} catch (Exception ignore) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class InspectionTaskServiceImpl implements InspectionTaskService {
|
|||||||
private IVMinioObjectService vMinioObjectService;
|
private IVMinioObjectService vMinioObjectService;
|
||||||
|
|
||||||
// 运行状态缓存(避免重复执行)
|
// 运行状态缓存(避免重复执行)
|
||||||
private final Map<Long, Boolean> runningTasks = new ConcurrentHashMap<>();
|
// private final Map<Long, Boolean> runningTasks = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private ModelManager modelManager;
|
private ModelManager modelManager;
|
||||||
// 延迟初始化,避免启动时的依赖问题
|
// 延迟初始化,避免启动时的依赖问题
|
||||||
@@ -123,7 +123,7 @@ public class InspectionTaskServiceImpl implements InspectionTaskService {
|
|||||||
task.setUpdateTime(DateUtils.getNowDate());
|
task.setUpdateTime(DateUtils.getNowDate());
|
||||||
inspectionTaskMapper.updateInspectionTask(task);
|
inspectionTaskMapper.updateInspectionTask(task);
|
||||||
|
|
||||||
runningTasks.put(taskId, true);
|
// runningTasks.put(taskId, true);
|
||||||
|
|
||||||
// 这里应该集成到Quartz定时任务中
|
// 这里应该集成到Quartz定时任务中
|
||||||
log.info("启动巡检任务: {}", taskId);
|
log.info("启动巡检任务: {}", taskId);
|
||||||
@@ -142,7 +142,7 @@ public class InspectionTaskServiceImpl implements InspectionTaskService {
|
|||||||
task.setUpdateTime(DateUtils.getNowDate());
|
task.setUpdateTime(DateUtils.getNowDate());
|
||||||
inspectionTaskMapper.updateInspectionTask(task);
|
inspectionTaskMapper.updateInspectionTask(task);
|
||||||
|
|
||||||
runningTasks.remove(taskId);
|
// runningTasks.remove(taskId);
|
||||||
|
|
||||||
log.info("停止巡检任务: {}", taskId);
|
log.info("停止巡检任务: {}", taskId);
|
||||||
return true;
|
return true;
|
||||||
@@ -170,8 +170,8 @@ public class InspectionTaskServiceImpl implements InspectionTaskService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 更新任务状态为执行中
|
// 更新任务状态为执行中
|
||||||
task.setStatus("1");
|
// task.setStatus("1");
|
||||||
inspectionTaskMapper.updateInspectionTask(task);
|
// inspectionTaskMapper.updateInspectionTask(task);
|
||||||
|
|
||||||
// 获取设备信息
|
// 获取设备信息
|
||||||
Device device = deviceService.selectDeviceByDeviceId(task.getDeviceId());
|
Device device = deviceService.selectDeviceByDeviceId(task.getDeviceId());
|
||||||
@@ -191,16 +191,16 @@ public class InspectionTaskServiceImpl implements InspectionTaskService {
|
|||||||
inspectionTaskRecordMapper.updateInspectionTaskRecord(record);
|
inspectionTaskRecordMapper.updateInspectionTaskRecord(record);
|
||||||
|
|
||||||
// 更新任务状态为已完成
|
// 更新任务状态为已完成
|
||||||
task.setStatus("2");
|
// task.setStatus("0");
|
||||||
inspectionTaskMapper.updateInspectionTask(task);
|
// inspectionTaskMapper.updateInspectionTask(task);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("巡检任务执行失败: taskId={}", taskId, e);
|
log.error("巡检任务执行失败: taskId={}", taskId, e);
|
||||||
updateRecordFailed(record, e.getMessage());
|
updateRecordFailed(record, e.getMessage());
|
||||||
|
|
||||||
// 更新任务状态为已完成(虽然失败)
|
// 更新任务状态为已完成(虽然失败)
|
||||||
task.setStatus("2");
|
// task.setStatus("0");
|
||||||
inspectionTaskMapper.updateInspectionTask(task);
|
// inspectionTaskMapper.updateInspectionTask(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +208,7 @@ public class InspectionTaskServiceImpl implements InspectionTaskService {
|
|||||||
* 更新记录为失败状态
|
* 更新记录为失败状态
|
||||||
*/
|
*/
|
||||||
private void updateRecordFailed(InspectionTaskRecord record, String errorMsg) {
|
private void updateRecordFailed(InspectionTaskRecord record, String errorMsg) {
|
||||||
record.setStatus(1); // 失败
|
record.setStatus(2); // 失败
|
||||||
record.setResult("执行失败: " + errorMsg);
|
record.setResult("执行失败: " + errorMsg);
|
||||||
inspectionTaskRecordMapper.updateInspectionTaskRecord(record);
|
inspectionTaskRecordMapper.updateInspectionTaskRecord(record);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user