fix:修复设备及台账定时生成工单任务问题
parent
f9af1c74a8
commit
98544d42c9
@ -0,0 +1,99 @@
|
|||||||
|
package cn.iocoder.yudao.module.mes.service.taskmanagement;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.scheduler.TaskSchedulerManager;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||||
|
import cn.iocoder.yudao.module.mes.dal.dataobject.moldtaskmanagement.MoldTaskManagementDO;
|
||||||
|
import cn.iocoder.yudao.module.mes.dal.dataobject.taskmanagement.TaskManagementDO;
|
||||||
|
import cn.iocoder.yudao.module.mes.dal.mysql.moldtaskmanagement.MoldTaskManagementMapper;
|
||||||
|
import cn.iocoder.yudao.module.mes.dal.mysql.taskmanagement.TaskManagementMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class WorkOrderScheduleInitializer {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TaskManagementMapper taskManagementMapper;
|
||||||
|
@Resource
|
||||||
|
private MoldTaskManagementMapper moldTaskManagementMapper;
|
||||||
|
@Resource
|
||||||
|
private TaskSchedulerManager taskSchedulerManager;
|
||||||
|
@Resource
|
||||||
|
private TenantFrameworkService tenantFrameworkService;
|
||||||
|
|
||||||
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
public void restoreEnabledWorkOrderSchedules() {
|
||||||
|
List<Long> tenantIds = getTenantIdsSafely();
|
||||||
|
for (Long tenantId : tenantIds) {
|
||||||
|
try {
|
||||||
|
TenantUtils.execute(tenantId, () -> {
|
||||||
|
restoreDeviceWorkOrderSchedules();
|
||||||
|
restoreMoldWorkOrderSchedules();
|
||||||
|
});
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("Tenant {} work order schedule restore exception", tenantId, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Long> getTenantIdsSafely() {
|
||||||
|
try {
|
||||||
|
return tenantFrameworkService.getTenantIds();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("Load tenant ids for work order schedule restore failed", ex);
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restoreDeviceWorkOrderSchedules() {
|
||||||
|
List<TaskManagementDO> taskList = taskManagementMapper.selectList(
|
||||||
|
Wrappers.<TaskManagementDO>lambdaQuery().eq(TaskManagementDO::getEnabled, true));
|
||||||
|
for (TaskManagementDO task : taskList) {
|
||||||
|
if (!StringUtils.hasText(task.getCronExpression())) {
|
||||||
|
log.warn("Device work order task {} is enabled but cron is blank, skip schedule restore", task.getId());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
boolean success = taskSchedulerManager.startWorkOrderTask(task.getId(), task.getCronExpression());
|
||||||
|
if (success) {
|
||||||
|
log.info("Device work order task {} schedule restored successfully", task.getId());
|
||||||
|
} else {
|
||||||
|
log.warn("Device work order task {} schedule restore failed", task.getId());
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("Device work order task {} schedule restore exception", task.getId(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restoreMoldWorkOrderSchedules() {
|
||||||
|
List<MoldTaskManagementDO> taskList = moldTaskManagementMapper.selectList(
|
||||||
|
Wrappers.<MoldTaskManagementDO>lambdaQuery().eq(MoldTaskManagementDO::getEnabled, true));
|
||||||
|
for (MoldTaskManagementDO task : taskList) {
|
||||||
|
if (!StringUtils.hasText(task.getCronExpression())) {
|
||||||
|
log.warn("Mold work order task {} is enabled but cron is blank, skip schedule restore", task.getId());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
boolean success = taskSchedulerManager.startMoldWorkOrderTask(task.getId(), task.getCronExpression());
|
||||||
|
if (success) {
|
||||||
|
log.info("Mold work order task {} schedule restored successfully", task.getId());
|
||||||
|
} else {
|
||||||
|
log.warn("Mold work order task {} schedule restore failed", task.getId());
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.error("Mold work order task {} schedule restore exception", task.getId(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue