Skip to content

Commit b98a2f9

Browse files
authored
Merge pull request #326 from obasekiosa/purge-old-queuedworkflows
Schedule recurrent CWL Viewer maintained cron-job for purging of old queued workflows from database
2 parents 67ec4b7 + fe8ea8f commit b98a2f9

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

src/main/java/org/commonwl/view/CwlViewerApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import org.springframework.boot.autoconfigure.SpringBootApplication;
2424
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
2525
import org.springframework.scheduling.annotation.EnableAsync;
26+
import org.springframework.scheduling.annotation.EnableScheduling;
2627

2728
@SpringBootApplication
2829
@EnableMongoRepositories
2930
@EnableAsync
31+
@EnableScheduling
3032
public class CwlViewerApplication {
3133

3234
public static void main(String[] args) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

src/main/java/org/commonwl/view/workflow/QueuedWorkflowRepository.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.springframework.data.mongodb.repository.Query;
55
import org.springframework.data.repository.PagingAndSortingRepository;
66

7+
import java.util.Date;
8+
import java.util.List;
9+
710
/**
811
* Stores workflows in the queue waiting for cwltool
912
*/
@@ -24,4 +27,20 @@ public interface QueuedWorkflowRepository extends PagingAndSortingRepository<Que
2427
@Query("{tempRepresentation.retrievedFrom: ?0}")
2528
void deleteByRetrievedFrom(GitDetails retrievedFrom);
2629

30+
/**
31+
* Deletes all queued workflows with date retrieved on older or equal to the Date argument passed.
32+
* @param retrievedOn Date of when the queued workflow was retrieved
33+
* @return The number of queued workflows deleted
34+
*/
35+
Long deleteByTempRepresentation_RetrievedOnLessThanEqual(Date retrievedOn);
36+
37+
38+
/**
39+
* Finds and returns all queued workflows with date retrieved on older or equal to the Date argument passed.
40+
* @param retrievedOn Details of where the queued workflow is from
41+
* @return A list of queued workflows
42+
*/
43+
List<QueuedWorkflow> findByTempRepresentation_RetrievedOnLessThanEqual(Date retrievedOn);
44+
45+
2746
}

src/main/resources/application.properties

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ spring.data.mongodb.port = 27017
4545
#=======================
4646
# SPARQL endpoint
4747
#=======================
48-
sparql.endpoint = http://localhost:3030/cwlviewer/
48+
sparql.endpoint = http://localhost:3030/cwlviewer/
49+
50+
51+
#=======================
52+
# Scheduler settings
53+
#=======================
54+
55+
# Cron expression that specifies the interval for running deletes of old queued workflow
56+
# For more info see https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions#usage
57+
# The expression below implies every hour at the 0th second and 0th minute i.e (01:00:00, 02:00::00,... etc)
58+
cron.deleteOldQueuedWorkflows = 0 0 * * * ?
59+
60+
# Age limit for queued workflows in hours.
61+
queuedWorkflowAgeLimitHours = 24

0 commit comments

Comments
 (0)