2021-03-16 17:38:53 +01:00
package eu.openaire.urls_worker.components ;
2021-12-23 23:12:34 +01:00
import eu.openaire.urls_worker.UrlsWorkerApplication ;
2021-11-26 16:04:31 +01:00
import eu.openaire.urls_worker.controllers.FullTextsController ;
2021-07-29 08:01:53 +02:00
import eu.openaire.urls_worker.plugins.PublicationsRetrieverPlugin ;
2021-11-27 01:37:33 +01:00
import eu.openaire.urls_worker.util.AssignmentsHandler ;
2021-12-17 07:24:09 +01:00
import org.apache.commons.io.FileUtils ;
2021-03-16 17:38:53 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.scheduling.annotation.Scheduled ;
import org.springframework.stereotype.Component ;
2021-11-26 16:04:31 +01:00
import java.io.File ;
import java.io.IOException ;
2021-03-16 17:38:53 +01:00
import java.text.SimpleDateFormat ;
import java.util.Date ;
2021-11-26 16:04:31 +01:00
import java.util.Map ;
import java.util.Set ;
2021-03-16 17:38:53 +01:00
@Component
public class ScheduledTasks {
private static final Logger logger = LoggerFactory . getLogger ( ScheduledTasks . class ) ;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat ( " HH:mm:ss " ) ;
2021-09-21 15:21:39 +02:00
//@Scheduled(fixedRate = 600_000) // Every 10 mins: 600_000
2021-03-16 17:38:53 +01:00
public void reportCurrentTime ( ) {
logger . info ( " Server is live! Time is now {} " , dateFormat . format ( new Date ( ) ) ) ;
}
2021-05-20 02:28:48 +02:00
2021-10-14 02:03:47 +02:00
@Scheduled ( fixedRate = 900_000 ) // Every 15 mins: 900_000
public void handleNewAssignments ( ) {
2021-11-27 01:37:33 +01:00
if ( AssignmentsHandler . isAvailableForWork )
2021-12-23 23:12:34 +01:00
{
if ( ( UrlsWorkerApplication . maxAssignmentsBatchesToHandleBeforeRestart = = 0 ) // Infinite batches.
| | ( AssignmentsHandler . numHandledAssignmentsBatches < UrlsWorkerApplication . maxAssignmentsBatchesToHandleBeforeRestart ) )
AssignmentsHandler . handleAssignments ( ) ;
else {
logger . info ( " The maximum assignments-batches ( " + UrlsWorkerApplication . maxAssignmentsBatchesToHandleBeforeRestart + " ) to be handled was reached! Shut down, in order for the external Linux-service to restart on its own.. " ) ;
UrlsWorkerApplication . gentleShutdown ( ) ;
}
} else {
2021-11-26 16:04:31 +01:00
//logger.debug("The worker is not available for work at the moment.."); // JUST FOR DEBUG!
}
2021-05-20 02:28:48 +02:00
}
2021-06-22 04:58:07 +02:00
2021-11-26 16:04:31 +01:00
@Scheduled ( fixedRate = 7_200_000 ) // Every 2 hours.
public void deleteHandledAssignmentsFullTexts ( )
{
Set < Map . Entry < Long , Boolean > > entrySet = FullTextsController . assignmentsNumsHandledAndLocallyDeleted . entrySet ( ) ;
if ( entrySet . isEmpty ( ) )
return ;
logger . info ( " Going to delete the locally stored fullTexts. " ) ;
for ( Map . Entry < Long , Boolean > 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 ) ;
}
}
}
2021-06-22 04:58:07 +02:00
//@Scheduled(fixedRate = 20_000) // Every 20 secs.
public void testUrlConnection ( ) {
String urlToCheck = " https://zenodo.org/record/1145726 " ;
PublicationsRetrieverPlugin . connectWithUrlTest ( urlToCheck ) ;
}
2021-11-26 16:04:31 +01:00
2021-06-22 04:58:07 +02:00
}