#AI
zhou zhou
8 小时以前 51877df13075ad10ef51107f15bcd21f1661febe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.vincent.rsf.server.ai.service.diagnosis;
 
import com.vincent.rsf.server.system.entity.AiDiagnosisPlan;
import com.vincent.rsf.server.system.service.AiDiagnosisPlanService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
@Component
public class AiDiagnosisPlanScheduler {
 
    @Resource
    private AiDiagnosisPlanService aiDiagnosisPlanService;
    @Resource
    private AiDiagnosisPlanRunnerService aiDiagnosisPlanRunnerService;
    @Resource
    private ThreadPoolTaskScheduler taskScheduler;
 
    /**
     * 每 30 秒扫描一次到期巡检计划,并异步分发执行。
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void dispatchDuePlans() {
        Date now = new Date();
        List<AiDiagnosisPlan> plans = aiDiagnosisPlanService.listDuePlans(now);
        for (AiDiagnosisPlan plan : plans) {
            Long operatorId = plan.getUpdateBy() == null ? plan.getCreateBy() : plan.getUpdateBy();
            Date nextRunTime = aiDiagnosisPlanService.calculateNextRunTime(plan.getCronExpr(), now);
            boolean acquired = aiDiagnosisPlanService.acquireForExecution(
                    plan.getId(),
                    operatorId,
                    "计划执行中",
                    nextRunTime
            );
            if (!acquired) {
                continue;
            }
            taskScheduler.execute(() -> aiDiagnosisPlanRunnerService.runPlan(plan.getId(), false));
        }
    }
}