diff --git a/cws-core/src/main/java/jpl/cws/core/db/SchedulerDbService.java b/cws-core/src/main/java/jpl/cws/core/db/SchedulerDbService.java index 55094dee..ceaf0946 100644 --- a/cws-core/src/main/java/jpl/cws/core/db/SchedulerDbService.java +++ b/cws-core/src/main/java/jpl/cws/core/db/SchedulerDbService.java @@ -49,7 +49,7 @@ public class SchedulerDbService extends DbService implements InitializingBean { public static final int PROCESSES_PAGE_SIZE = 100; public static final String FIND_CLAIMABLE_ROWS_SQL = - "SELECT uuid FROM cws_sched_worker_proc_inst " + + "SELECT uuid, priority FROM cws_sched_worker_proc_inst " + "WHERE " + " status='"+PENDING+"' AND " + " proc_def_key=? " + @@ -256,10 +256,10 @@ public int updateProcInstIdAndStartedByWorker( public Map> claimHighestPriorityStartReq(String workerId, Map workerProcsList, Map limitsPerProcs, int limit) { List claimUuids = new ArrayList(); List rowUuids = new ArrayList(); - List rowUuidsPerProcDefKey = new ArrayList(); + List> rowUuidsPerProcDefKey = new ArrayList>(); LinkedHashMap uuidAndProcDefKeyPair = new LinkedHashMap(); List clearOutUnclaimedInst = new ArrayList(); - List unfilteredRowUuids = new ArrayList(); + List> unfilteredProcesses = new ArrayList>(); List claimedRowUuids = new ArrayList(); long t0 = System.currentTimeMillis(); int numClaimed = 0; @@ -273,16 +273,21 @@ public Map> claimHighestPriorityStartReq(String workerId, Ma // Find claimable rows // for (Map.Entry procs : limitsPerProcs.entrySet()) { - rowUuidsPerProcDefKey = jdbcTemplate.queryForList(FIND_CLAIMABLE_ROWS_SQL, String.class, - new Object[] {procs.getKey(), procs.getValue()*2}); + rowUuidsPerProcDefKey = jdbcTemplate.queryForList(FIND_CLAIMABLE_ROWS_SQL, new Object[] {procs.getKey(), procs.getValue()*2}); // get list of uuids using array of procdefkeys IN (keys) - unfilteredRowUuids.addAll(rowUuidsPerProcDefKey); + unfilteredProcesses.addAll(rowUuidsPerProcDefKey); } + + unfilteredProcesses.sort(new Comparator>() { + public int compare(Map one, Map two) { + return ((Integer) one.get("priority")).compareTo((Integer) two.get("priority")); + } + }); - Collections.sort(unfilteredRowUuids); - for (String id : unfilteredRowUuids) { - String procDefKeyString = getProcDefKeyFromUuid(id); - uuidAndProcDefKeyPair.put(id, procDefKeyString); + for (Map proc : unfilteredProcesses) { + String uuid = (String) proc.get("uuid"); + String procDefKeyString = getProcDefKeyFromUuid(uuid); + uuidAndProcDefKeyPair.put(uuid, procDefKeyString); } for (Map.Entry procLimit : limitsPerProcs.entrySet()) { @@ -1537,4 +1542,4 @@ public int retryFailedToStart(List uuids) { return jdbcTemplate.update(query); } -} +} \ No newline at end of file