@@ -323,16 +323,19 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
323323 String restoringCondition = "download.status = org.icatproject.topcat.domain.DownloadStatus.RESTORING" ;
324324 String pausedCondition = "download.status = org.icatproject.topcat.domain.DownloadStatus.PAUSED" ;
325325
326+ int availableDownloads = maxActiveDownloads ;
326327 if (maxActiveDownloads > 0 ) {
328+ // Work out how many "available" spaces there are by accounting for the active Downloads
327329 String activeQueryString = selectString + " and " + restoringCondition ;
328330 TypedQuery <Download > activeDownloadsQuery = em .createQuery (activeQueryString , Download .class );
329331 List <Download > activeDownloads = activeDownloadsQuery .getResultList ();
330- maxActiveDownloads - = activeDownloads .size ();
331- if (maxActiveDownloads <= 0 ) {
332+ int activeDownloadsSize = activeDownloads .size ();
333+ if (activeDownloadsSize >= maxActiveDownloads ) {
332334 String format = "More downloads currently RESTORING {} than maxActiveDownloads {}, cannot prepare queued jobs" ;
333- logger .info (format , activeDownloads . size () , maxActiveDownloads );
335+ logger .info (format , activeDownloadsSize , maxActiveDownloads );
334336 return ;
335337 }
338+ availableDownloads -= activeDownloadsSize ;
336339 }
337340
338341 String queuedQueryString = selectString + " and " + pausedCondition + " and download.preparedId = null" ;
@@ -342,14 +345,14 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
342345
343346 Map <String , String > sessionIds = new HashMap <>();
344347 if (maxActiveDownloads <= 0 ) {
345- logger .info ("Preparing {} queued downloads" , queuedDownloads .size ());
346348 // No limits on how many to submit
349+ logger .info ("Preparing {} queued downloads" , queuedDownloads .size ());
347350 for (Download queuedDownload : queuedDownloads ) {
348351 queuedDownload .setStatus (DownloadStatus .PREPARING );
349352 prepareDownload (queuedDownload , null , getQueueSessionId (sessionIds , queuedDownload .getFacilityName ()));
350353 }
351354 } else {
352- logger .info ("Preparing up to {} queued downloads" , maxActiveDownloads );
355+ logger .info ("Preparing up to {} queued downloads" , availableDownloads );
353356 HashMap <Integer , List <Download >> mapping = new HashMap <>();
354357 for (Download queuedDownload : queuedDownloads ) {
355358 String sessionId = getQueueSessionId (sessionIds , queuedDownload .getFacilityName ());
@@ -360,8 +363,8 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
360363 // Highest priority, prepare now
361364 queuedDownload .setStatus (DownloadStatus .PREPARING );
362365 prepareDownload (queuedDownload , null , sessionId );
363- maxActiveDownloads -= 1 ;
364- if (maxActiveDownloads <= 0 ) {
366+ availableDownloads -= 1 ;
367+ if (availableDownloads <= 0 ) {
365368 return ;
366369 }
367370 } else {
@@ -381,8 +384,8 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
381384 for (Download download : downloadList ) {
382385 download .setStatus (DownloadStatus .PREPARING );
383386 prepareDownload (download , null , getQueueSessionId (sessionIds , download .getFacilityName ()));
384- maxActiveDownloads -= 1 ;
385- if (maxActiveDownloads <= 0 ) {
387+ availableDownloads -= 1 ;
388+ if (availableDownloads <= 0 ) {
386389 return ;
387390 }
388391 }
0 commit comments