1414import jakarta .ejb .EJB ;
1515import jakarta .ejb .Schedule ;
1616import jakarta .ejb .Singleton ;
17+ import jakarta .json .JsonObject ;
1718import jakarta .persistence .EntityManager ;
1819import jakarta .persistence .PersistenceContext ;
1920import jakarta .persistence .TypedQuery ;
@@ -114,35 +115,35 @@ public void updateStatuses(int pollDelay, int pollIntervalWait, IdsClient inject
114115 String queryString = selectString + " and " + notExpiredCondition + " and (" + isActiveCondition + ")" ;
115116
116117 TypedQuery <Download > query = em .createQuery (queryString , Download .class );
117- List <Download > downloads = query .getResultList ();
118+ List <Download > downloads = query .getResultList ();
118119
119120 for (Download download : downloads ) {
120- Date lastCheck = lastChecks .get (download .getId ());
121- Date now = new Date ();
122- long createdSecondsAgo = (now .getTime () - download .getCreatedAt ().getTime ()) / 1000 ;
121+ Date lastCheck = lastChecks .get (download .getId ());
122+ Date now = new Date ();
123+ long createdSecondsAgo = (now .getTime () - download .getCreatedAt ().getTime ()) / 1000 ;
123124 if (download .getStatus () == DownloadStatus .PREPARING ) {
124125 // If prepareDownload was called previously but caught an exception (other than
125126 // TopcatException), we should not call it again immediately, but should impose
126127 // a delay. See issue #462.
127128 if (lastCheck == null ) {
128- prepareDownload (download , injectedIdsClient );
129- } else {
130- long lastCheckSecondsAgo = (now .getTime () - lastCheck .getTime ()) / 1000 ;
129+ prepareDownload (download , injectedIdsClient );
130+ } else {
131+ long lastCheckSecondsAgo = (now .getTime () - lastCheck .getTime ()) / 1000 ;
131132 if (lastCheckSecondsAgo >= pollIntervalWait ) {
132- prepareDownload (download , injectedIdsClient );
133- }
134- }
135- } else if (createdSecondsAgo >= pollDelay ) {
133+ prepareDownload (download , injectedIdsClient );
134+ }
135+ }
136+ } else if (download . getPreparedId () != null && createdSecondsAgo >= pollDelay ) {
136137 if (lastCheck == null ) {
137- performCheck (download , injectedIdsClient );
138- } else {
139- long lastCheckSecondsAgo = (now .getTime () - lastCheck .getTime ()) / 1000 ;
138+ performCheck (download , injectedIdsClient );
139+ } else {
140+ long lastCheckSecondsAgo = (now .getTime () - lastCheck .getTime ()) / 1000 ;
140141 if (lastCheckSecondsAgo >= pollIntervalWait ) {
141- performCheck (download , injectedIdsClient );
142- }
142+ performCheck (download , injectedIdsClient );
143143 }
144144 }
145- }
145+ }
146+ }
146147 }
147148
148149 private void performCheck (Download download , IdsClient injectedIdsClient ) {
@@ -250,12 +251,14 @@ private void prepareDownload(Download download, IdsClient injectedIdsClient, Str
250251 String preparedId = idsClient .prepareData (sessionId , download .getInvestigationIds (), download .getDatasetIds (), download .getDatafileIds ());
251252 download .setPreparedId (preparedId );
252253
253- try {
254- Long size = idsClient .getSize (sessionId , download .getInvestigationIds (), download .getDatasetIds (), download .getDatafileIds ());
255- download .setSize (size );
256- } catch (Exception e ) {
257- logger .error ("prepareDownload: setting size to -1 as getSize threw exception: " + e .getMessage ());
258- download .setSize (-1 );
254+ if (download .getSize () <= 0 ) {
255+ try {
256+ Long size = idsClient .getSize (sessionId , download .getInvestigationIds (), download .getDatasetIds (), download .getDatafileIds ());
257+ download .setSize (size );
258+ } catch (Exception e ) {
259+ logger .error ("prepareDownload: setting size to -1 as getSize threw exception: " + e .getMessage ());
260+ download .setSize (-1 );
261+ }
259262 }
260263
261264 if (download .getIsTwoLevel () || !download .getTransport ().matches ("https|http" )) {
@@ -285,19 +288,20 @@ private void prepareDownload(Download download, IdsClient injectedIdsClient, Str
285288 * @param sessionIds Map from Facility to functional sessionId
286289 * @param facilityName Name of ICAT Facility to get the sessionId for
287290 * @return Functional ICAT sessionId
288- * @throws InternalException If the facilityName cannot be mapped to an ICAT url
289- * @throws BadRequestException If the login fails
291+ * @throws Exception If the login fails
290292 */
291293 private String getQueueSessionId (Map <String , String > sessionIds , String facilityName )
292- throws InternalException , BadRequestException {
294+ throws Exception {
293295 String sessionId = sessionIds .get (facilityName );
294296 if (sessionId == null ) {
295297 IcatClient icatClient = new IcatClient (FacilityMap .getInstance ().getIcatUrl (facilityName ));
296298 Properties properties = Properties .getInstance ();
297299 String plugin = properties .getProperty ("queue.account." + facilityName + ".plugin" );
298300 String username = properties .getProperty ("queue.account." + facilityName + ".username" );
299301 String password = properties .getProperty ("queue.account." + facilityName + ".password" );
300- sessionId = icatClient .login (plugin , username , password );
302+ String jsonString = icatClient .login (plugin , username , password );
303+ JsonObject jsonObject = Utils .parseJsonObject (jsonString );
304+ sessionId = jsonObject .getString ("sessionId" );
301305 sessionIds .put (facilityName , sessionId );
302306 }
303307 return sessionId ;
@@ -315,7 +319,7 @@ private String getQueueSessionId(Map<String, String> sessionIds, String facility
315319 */
316320 public void startQueuedDownloads (int maxActiveDownloads ) throws Exception {
317321 if (maxActiveDownloads == 0 ) {
318- logger .debug ("Preparing of queued jobs disabled by config, skipping" );
322+ logger .trace ("Preparing of queued jobs disabled by config, skipping" );
319323 return ;
320324 }
321325
@@ -332,7 +336,7 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
332336 int activeDownloadsSize = activeDownloads .size ();
333337 if (activeDownloadsSize >= maxActiveDownloads ) {
334338 String format = "More downloads currently RESTORING {} than maxActiveDownloads {}, cannot prepare queued jobs" ;
335- logger .info (format , activeDownloadsSize , maxActiveDownloads );
339+ logger .trace (format , activeDownloadsSize , maxActiveDownloads );
336340 return ;
337341 }
338342 availableDownloads -= activeDownloadsSize ;
@@ -346,13 +350,13 @@ public void startQueuedDownloads(int maxActiveDownloads) throws Exception {
346350 Map <String , String > sessionIds = new HashMap <>();
347351 if (maxActiveDownloads <= 0 ) {
348352 // No limits on how many to submit
349- logger .info ("Preparing {} queued downloads" , queuedDownloads .size ());
353+ logger .trace ("Preparing {} queued downloads" , queuedDownloads .size ());
350354 for (Download queuedDownload : queuedDownloads ) {
351355 queuedDownload .setStatus (DownloadStatus .PREPARING );
352356 prepareDownload (queuedDownload , null , getQueueSessionId (sessionIds , queuedDownload .getFacilityName ()));
353357 }
354358 } else {
355- logger .info ("Preparing up to {} queued downloads" , availableDownloads );
359+ logger .trace ("Preparing up to {} queued downloads" , availableDownloads );
356360 HashMap <Integer , List <Download >> mapping = new HashMap <>();
357361 for (Download queuedDownload : queuedDownloads ) {
358362 String sessionId = getQueueSessionId (sessionIds , queuedDownload .getFacilityName ());
0 commit comments