shikeying
2022-09-26 fd03e31f173ad9c52b15a30a9127e2b6a468538d
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.iplatform.recvideo.scheduler;
 
import com.iplatform.core.BeanContextAware;
import com.iplatform.gather.LocalAddress;
import com.iplatform.recvideo.VideoScheduler;
import com.walker.store.AbstractStore;
import com.walker.store.task.GatherTask;
 
/**
 * 视频信息存储、搜索相似度处理的调度任务。<p></p>
 * 该任务会持续运行:
 * <pre>
 *     1)
 * </pre>
 * @author 时克英
 * @date 2022-09-26
 */
public class VideoSearchScheduler extends VideoScheduler {
 
    private VideoSearchTask videoSearchTask = null;
    private VideoSearchMeta currentVideoSearchMeta = null;
 
    //
    private int failedCount = 0;
 
    public VideoSearchScheduler(int id, String name){
        super(id, name);
    }
 
    @Override
    protected GatherTask providerTask(AbstractStore store) {
        LocalAddress localAddress = BeanContextAware.getBeanByType(LocalAddress.class);
        VideoSearchTask task = new VideoSearchTask("视频相似度检索任务", localAddress);
        return task;
    }
 
//    @Override
//    protected Object[] getRunParameters(Object previousInvokeResult) {}
 
    @Override
    protected Object onProcess(Object[] inputParams) throws Exception {
        if(this.videoSearchTask == null){
            logger.debug("调度任务初始化,需要先查询任务状态表");
            this.videoSearchTask = (VideoSearchTask)this.providerTask(this.getStore());
            Object[] params = new Object[]{this.videoSearchTask.getSql(), new Object[]{}};
            this.currentVideoSearchMeta = (VideoSearchMeta)this.videoSearchTask.run(null, null, null, params);
            if(this.currentVideoSearchMeta == null){
                return null;
            }
            logger.debug("开始启动批次查询:" + this.currentVideoSearchMeta.getBatchId());
        }
 
        //
        this.videoSearchTask.checkExecutor(this.currentVideoSearchMeta);
        //
        int result = this.videoSearchTask.executeOneSearch();
 
        if(result == 0){
            return SUCCESS;
        }
 
        if(result == -1){
            this.failedCount ++;
            if(this.failedCount > 3){
                // 如果报错超过多次,返回空,让线程休眠一下
                this.failedCount = 0;
                return null;
            } else {
                return SUCCESS;
            }
        }
 
        if(result == 1){
            // 完成,准备查找下一个批次记录
            logger.info("完成批次,准备查找下一个批次记录。batchId = " + this.currentVideoSearchMeta.getBatchId());
            this.clearStatus();
            return SUCCESS;
        }
 
        return null;
//        if(this.gatherTask == null){
//            this.gatherTask = this.providerTask(this.store);
//            if(this.gatherTask == null){
//                throw new IllegalArgumentException("未提供任务对象,调度无法执行:" + this.getName());
//            }
//        }
//        // srcName, createTableSQL, parameter, params[]
//        return this.gatherTask.run((String)inputParams[0], (String)inputParams[1], inputParams[2], (Object[])inputParams[3]);
    }
 
    private void clearStatus(){
        if(this.videoSearchTask != null){
            this.videoSearchTask = null;
        }
        this.currentVideoSearchMeta = null;
    }
}