|
| 1 | +package org.commonwl.view; |
| 2 | + |
| 3 | + |
| 4 | +import org.commonwl.view.workflow.QueuedWorkflowRepository; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.scheduling.annotation.Scheduled; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | + |
| 12 | +import java.util.Calendar; |
| 13 | +import java.util.Date; |
| 14 | + |
| 15 | +/** |
| 16 | + * Scheduler class for recurrent processes. |
| 17 | + */ |
| 18 | +@Component |
| 19 | +public class Scheduler { |
| 20 | + |
| 21 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 22 | + private final QueuedWorkflowRepository queuedWorkflowRepository; |
| 23 | + |
| 24 | + @Value("${queuedWorkflowAgeLimitHours}") |
| 25 | + private Integer QUEUED_WORKFLOW_AGE_LIMIT_HOURS; |
| 26 | + |
| 27 | + @Autowired |
| 28 | + public Scheduler(QueuedWorkflowRepository queuedWorkflowRepository) { |
| 29 | + this.queuedWorkflowRepository = queuedWorkflowRepository; |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + /** |
| 34 | + * A Scheduled function to delete old queued workflow entries |
| 35 | + * from the queue. Age is determined by QUEUED_WORKFLOW_AGE_LIMIT_HOURS |
| 36 | + */ |
| 37 | + @Scheduled(cron = "${cron.deleteOldQueuedWorkflows}") |
| 38 | + public void removeOldQueuedWorkflowEntries() { |
| 39 | + Calendar calendar = Calendar.getInstance(); |
| 40 | + Date now = new Date(); |
| 41 | + calendar.setTime(now); |
| 42 | + |
| 43 | + if (QUEUED_WORKFLOW_AGE_LIMIT_HOURS == null) { |
| 44 | + QUEUED_WORKFLOW_AGE_LIMIT_HOURS = 24; |
| 45 | + } |
| 46 | + |
| 47 | + // calculate time QUEUED_WORKFLOW_AGE_LIMIT_HOURS before now |
| 48 | + calendar.add(Calendar.HOUR, -QUEUED_WORKFLOW_AGE_LIMIT_HOURS); |
| 49 | + Date removeTime = calendar.getTime(); |
| 50 | + |
| 51 | + logger.info("The time is " + now); |
| 52 | + logger.info("Delete time interval is : OLDER THAN " + QUEUED_WORKFLOW_AGE_LIMIT_HOURS + " HOURS"); |
| 53 | + logger.info("Deleting queued workflows older than or equal to " + removeTime); |
| 54 | + |
| 55 | + logger.info(queuedWorkflowRepository.deleteByTempRepresentation_RetrievedOnLessThanEqual(removeTime) |
| 56 | + + " Old queued workflows removed"); |
| 57 | + } |
| 58 | +} |
0 commit comments