1414import com .dtstack .taier .scheduler .service .ScheduleJobService ;
1515import com .google .common .collect .Lists ;
1616import org .apache .commons .collections .CollectionUtils ;
17- import org .slf4j .Logger ;
18- import org .slf4j .LoggerFactory ;
1917import org .springframework .beans .factory .annotation .Autowired ;
2018
2119import java .util .ArrayList ;
20+ import java .util .Comparator ;
2221import java .util .List ;
2322import java .util .Map ;
2423import java .util .Set ;
@@ -44,39 +43,46 @@ public abstract class OperatorRecordJobScheduler extends AbstractJobSummitSchedu
4443 @ Autowired
4544 private ScheduleJobOperatorRecordService scheduleJobOperatorRecordService ;
4645
46+ private Long operatorRecordStartId = 0L ;
47+
4748 @ Override
4849 protected List <ScheduleJobDetails > listExecJob (Long startSort , String nodeAddress , Boolean isEq ) {
49- List <ScheduleJobOperatorRecord > records = scheduleJobOperatorRecordService .listOperatorRecord (startSort , nodeAddress , getOperatorType ().getType (), isEq );
50-
51- if (CollectionUtils .isNotEmpty (records )) {
52- Set <String > jobIds = records .stream ().map (ScheduleJobOperatorRecord ::getJobId ).collect (Collectors .toSet ());
53- List <ScheduleJob > scheduleJobList = getScheduleJob (jobIds );
54-
55- if (CollectionUtils .isNotEmpty (scheduleJobList )) {
56- List <String > jodExecIds = scheduleJobList .stream ().map (ScheduleJob ::getJobId ).collect (Collectors .toList ());
57- if (jobIds .size () != scheduleJobList .size ()) {
58- // 过滤出来已经提交运行的实例,删除操作记录
59- List <String > deleteJobIdList = jobIds .stream ().filter (jobId -> !jodExecIds .contains (jobId )).collect (Collectors .toList ());
60- removeOperatorRecord (deleteJobIdList );
61- }
50+ List <ScheduleJobOperatorRecord > records = scheduleJobOperatorRecordService .listOperatorRecord (operatorRecordStartId , nodeAddress , getOperatorType ().getType (), isEq );
51+ //empty
52+ if (CollectionUtils .isEmpty (records )) {
53+ operatorRecordStartId = 0L ;
54+ return new ArrayList <>();
55+ }
6256
63- List <String > jobKeys = scheduleJobList .stream ().map (ScheduleJob ::getJobKey ).collect (Collectors .toList ());
64- List <ScheduleJobJob > scheduleJobJobList = scheduleJobJobService .listByJobKeys (jobKeys );
65- Map <String , List <ScheduleJobJob >> jobJobMap = scheduleJobJobList .stream ().collect (Collectors .groupingBy (ScheduleJobJob ::getJobKey ));
66- List <ScheduleJobDetails > scheduleJobDetailsList = new ArrayList <>(scheduleJobList .size ());
57+ Set <String > jobIds = records .stream ().map (ScheduleJobOperatorRecord ::getJobId ).collect (Collectors .toSet ());
58+ List <ScheduleJob > scheduleJobList = getScheduleJob (jobIds );
6759
68- for (ScheduleJob scheduleJob : scheduleJobList ) {
69- ScheduleJobDetails scheduleJobDetails = new ScheduleJobDetails ();
70- scheduleJobDetails .setScheduleJob (scheduleJob );
71- scheduleJobDetails .setJobJobList (jobJobMap .get (scheduleJob .getJobKey ()));
72- scheduleJobDetailsList .add (scheduleJobDetails );
73- }
74- return scheduleJobDetailsList ;
75- } else {
76- removeOperatorRecord (Lists .newArrayList (jobIds ));
77- }
60+ if (CollectionUtils .isEmpty (scheduleJobList )) {
61+ operatorRecordStartId = 0L ;
62+ removeOperatorRecord (Lists .newArrayList (jobIds ));
7863 }
79- return Lists .newArrayList ();
64+
65+ //set max
66+ records .stream ().max (Comparator .comparing (ScheduleJobOperatorRecord ::getId ))
67+ .ifPresent (scheduleJobOperatorRecord -> operatorRecordStartId = scheduleJobOperatorRecord .getId ());
68+
69+ if (jobIds .size () != scheduleJobList .size ()) {
70+ List <String > jodExecIds = scheduleJobList .stream ().map (ScheduleJob ::getJobId ).collect (Collectors .toList ());
71+ // 过滤出来已经提交运行的实例,删除操作记录
72+ List <String > deleteJobIdList = jobIds .stream ().filter (jobId -> !jodExecIds .contains (jobId )).collect (Collectors .toList ());
73+ removeOperatorRecord (deleteJobIdList );
74+ }
75+
76+ List <String > jobKeys = scheduleJobList .stream ().map (ScheduleJob ::getJobKey ).collect (Collectors .toList ());
77+ List <ScheduleJobJob > scheduleJobJobList = scheduleJobJobService .listByJobKeys (jobKeys );
78+ Map <String , List <ScheduleJobJob >> jobJobMap = scheduleJobJobList .stream ().collect (Collectors .groupingBy (ScheduleJobJob ::getJobKey ));
79+
80+ return scheduleJobList .stream ().map (scheduleJob -> {
81+ ScheduleJobDetails scheduleJobDetails = new ScheduleJobDetails ();
82+ scheduleJobDetails .setScheduleJob (scheduleJob );
83+ scheduleJobDetails .setJobJobList (jobJobMap .get (scheduleJob .getJobKey ()));
84+ return scheduleJobDetails ;
85+ }).collect (Collectors .toList ());
8086 }
8187
8288 /**
0 commit comments