@@ -236,18 +236,22 @@ private void sendDownloadReadyEmail(Download download) throws InternalException{
236236 }
237237
238238 private void prepareDownload (Download download , IdsClient injectedIdsClient ) throws Exception {
239+ prepareDownload (download , injectedIdsClient , download .getSessionId ());
240+ }
241+
242+ private void prepareDownload (Download download , IdsClient injectedIdsClient , String sessionId ) throws Exception {
239243
240244 try {
241245 IdsClient idsClient = injectedIdsClient ;
242246 if ( idsClient == null ) {
243247 idsClient = new IdsClient (getDownloadUrl (download .getFacilityName (),download .getTransport ()));
244248 }
245249 logger .info ("Requesting prepareData for Download " + download .getFileName ());
246- String preparedId = idsClient .prepareData (download . getSessionId () , download .getInvestigationIds (), download .getDatasetIds (), download .getDatafileIds ());
250+ String preparedId = idsClient .prepareData (sessionId , download .getInvestigationIds (), download .getDatasetIds (), download .getDatafileIds ());
247251 download .setPreparedId (preparedId );
248252
249253 try {
250- Long size = idsClient .getSize (download . getSessionId () , download .getInvestigationIds (), download .getDatasetIds (), download .getDatafileIds ());
254+ Long size = idsClient .getSize (sessionId , download .getInvestigationIds (), download .getDatasetIds (), download .getDatafileIds ());
251255 download .setSize (size );
252256 } catch (Exception e ) {
253257 logger .error ("prepareDownload: setting size to -1 as getSize threw exception: " + e .getMessage ());
@@ -274,7 +278,31 @@ private void prepareDownload(Download download, IdsClient injectedIdsClient) thr
274278 }
275279
276280 }
277-
281+
282+ /**
283+ * Gets a functional sessionId to use for submitting to the queue, logging in if needed.
284+ *
285+ * @param sessionIds Map from Facility to functional sessionId
286+ * @param facilityName Name of ICAT Facility to get the sessionId for
287+ * @return Functional ICAT sessionId
288+ * @throws InternalException If the facilityName cannot be mapped to an ICAT url
289+ * @throws BadRequestException If the login fails
290+ */
291+ private String getQueueSessionId (Map <String , String > sessionIds , String facilityName )
292+ throws InternalException , BadRequestException {
293+ String sessionId = sessionIds .get (facilityName );
294+ if (sessionId == null ) {
295+ IcatClient icatClient = new IcatClient (FacilityMap .getInstance ().getIcatUrl (facilityName ));
296+ Properties properties = Properties .getInstance ();
297+ String plugin = properties .getProperty ("queue.account." + facilityName + ".plugin" );
298+ String username = properties .getProperty ("queue.account." + facilityName + ".username" );
299+ String password = properties .getProperty ("queue.account." + facilityName + ".password" );
300+ sessionId = icatClient .login (plugin , username , password );
301+ sessionIds .put (facilityName , sessionId );
302+ }
303+ return sessionId ;
304+ }
305+
278306 /**
279307 * Prepares Downloads which are queued (PAUSED with no preparedId) up to the
280308 * maxActiveDownloads limit.
@@ -295,43 +323,48 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
295323 String restoringCondition = "download.status = org.icatproject.topcat.domain.DownloadStatus.RESTORING" ;
296324 String pausedCondition = "download.status = org.icatproject.topcat.domain.DownloadStatus.PAUSED" ;
297325
326+ int availableDownloads = maxActiveDownloads ;
298327 if (maxActiveDownloads > 0 ) {
328+ // Work out how many "available" spaces there are by accounting for the active Downloads
299329 String activeQueryString = selectString + " and " + restoringCondition ;
300330 TypedQuery <Download > activeDownloadsQuery = em .createQuery (activeQueryString , Download .class );
301331 List <Download > activeDownloads = activeDownloadsQuery .getResultList ();
302- maxActiveDownloads - = activeDownloads .size ();
303- if (maxActiveDownloads <= 0 ) {
332+ int activeDownloadsSize = activeDownloads .size ();
333+ if (activeDownloadsSize >= maxActiveDownloads ) {
304334 String format = "More downloads currently RESTORING {} than maxActiveDownloads {}, cannot prepare queued jobs" ;
305- logger .info (format , activeDownloads . size () , maxActiveDownloads );
335+ logger .info (format , activeDownloadsSize , maxActiveDownloads );
306336 return ;
307337 }
338+ availableDownloads -= activeDownloadsSize ;
308339 }
309340
310341 String queuedQueryString = selectString + " and " + pausedCondition + " and download.preparedId = null" ;
311342 queuedQueryString += " order by download.createdAt" ;
312343 TypedQuery <Download > queuedDownloadsQuery = em .createQuery (queuedQueryString , Download .class );
313344 List <Download > queuedDownloads = queuedDownloadsQuery .getResultList ();
314345
346+ Map <String , String > sessionIds = new HashMap <>();
315347 if (maxActiveDownloads <= 0 ) {
316- logger .info ("Preparing {} queued downloads" , queuedDownloads .size ());
317348 // No limits on how many to submit
349+ logger .info ("Preparing {} queued downloads" , queuedDownloads .size ());
318350 for (Download queuedDownload : queuedDownloads ) {
319351 queuedDownload .setStatus (DownloadStatus .PREPARING );
320- prepareDownload (queuedDownload , null );
352+ prepareDownload (queuedDownload , null , getQueueSessionId ( sessionIds , queuedDownload . getFacilityName ()) );
321353 }
322354 } else {
323- logger .info ("Preparing up to {} queued downloads" , maxActiveDownloads );
355+ logger .info ("Preparing up to {} queued downloads" , availableDownloads );
324356 HashMap <Integer , List <Download >> mapping = new HashMap <>();
325357 for (Download queuedDownload : queuedDownloads ) {
358+ String sessionId = getQueueSessionId (sessionIds , queuedDownload .getFacilityName ());
326359 String icatUrl = FacilityMap .getInstance ().getIcatUrl (queuedDownload .getFacilityName ());
327- IcatClient icatClient = new IcatClient (icatUrl , queuedDownload . getSessionId () );
360+ IcatClient icatClient = new IcatClient (icatUrl , sessionId );
328361 int priority = icatClient .getQueuePriority (queuedDownload .getUserName ());
329362 if (priority == 1 ) {
330363 // Highest priority, prepare now
331364 queuedDownload .setStatus (DownloadStatus .PREPARING );
332- prepareDownload (queuedDownload , null );
333- maxActiveDownloads -= 1 ;
334- if (maxActiveDownloads <= 0 ) {
365+ prepareDownload (queuedDownload , null , sessionId );
366+ availableDownloads -= 1 ;
367+ if (availableDownloads <= 0 ) {
335368 return ;
336369 }
337370 } else {
@@ -350,9 +383,9 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
350383 List <Download > downloadList = mapping .get (key );
351384 for (Download download : downloadList ) {
352385 download .setStatus (DownloadStatus .PREPARING );
353- prepareDownload (download , null );
354- maxActiveDownloads -= 1 ;
355- if (maxActiveDownloads <= 0 ) {
386+ prepareDownload (download , null , getQueueSessionId ( sessionIds , download . getFacilityName ()) );
387+ availableDownloads -= 1 ;
388+ if (availableDownloads <= 0 ) {
356389 return ;
357390 }
358391 }
0 commit comments