package eu.openaire.urls_worker.components; import eu.openaire.urls_worker.controllers.FullTextsController; import eu.openaire.urls_worker.plugins.PublicationsRetrieverPlugin; import eu.openaire.urls_worker.util.AssignmentsHandler; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Set; @Component public class ScheduledTasks { private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); @Scheduled(fixedDelay = 1) // Request the next batch immediately after the last one finishes. public void handleNewAssignments() { if ( AssignmentsHandler.hadConnectionErrorOnRequest ) { try { Thread.sleep(900_000); // Sleep for 15 mins to stall the scheduler from retrying right away, thus giving time to the Controller to recover. } catch (InterruptedException ie) { logger.warn("Sleeping was interrupted!"); } finally { AssignmentsHandler.hadConnectionErrorOnRequest = false; } } AssignmentsHandler.handleAssignments(); } @Scheduled(fixedDelay = 43_200_000, initialDelay = 43_200_000) // Every 12 hours, after 12 hours from the start of this app. public static void deleteHandledAssignmentsFullTexts() { Set> entrySet = FullTextsController.assignmentsNumsHandledAndLocallyDeleted.entrySet(); if ( entrySet.isEmpty() ) return; logger.info("Going to delete the locally stored fullTexts."); for ( Map.Entry entry : entrySet ) { if ( entry.getValue().equals(true) ) // It is already deleted, move on. continue; Long curAssignments = entry.getKey(); String currentAssignmentsBasePath = PublicationsRetrieverPlugin.assignmentsBasePath + "assignments_" + curAssignments + "_fullTexts" + File.separator; logger.debug("Going to delete the files from assignments: " + currentAssignmentsBasePath); File curDir = new File(currentAssignmentsBasePath); if ( !curDir.isDirectory() ) { logger.error("This assignments-dir does not exist: " + currentAssignmentsBasePath); continue; } try { FileUtils.deleteDirectory(curDir); FullTextsController.assignmentsNumsHandledAndLocallyDeleted.put(curAssignments, true); // Set the is-handled to true. } catch (IOException e) { logger.error("The following directory could not be deleted: " + currentAssignmentsBasePath, e); } } } }