Project

General

Profile

« Previous | Next » 

Revision 44375

avoid the massive launch of scheduled wfs after a server stop/pause

View differences:

modules/dnet-msro-service/trunk/src/test/java/eu/dnetlib/msro/cron/ScheduledWorkflowLauncherTest.java
5 5

  
6 6
import java.util.Date;
7 7

  
8
import org.junit.Ignore;
9 8
import org.junit.Test;
10 9

  
11 10
import eu.dnetlib.miscutils.datetime.DateUtils;
......
17 16
	private static final String WF_ID = "wf-1234567890";
18 17
	private static final String CRON_EXPR = "0 15 9 ? * SAT";
19 18
	private static final int ALMOST_TWO_WEEKS = 20000;
19

  
20 20
	private static final DateUtils dateUtils = new DateUtils();
21 21
	private static final Date date = dateUtils.parse("2016-10-29T09:16:00+02:00"); // Saturday
22 22
	private static final Date oneDayBefore = dateUtils.parse("2016-10-28T09:16:00+02:00");
23 23
	private static final Date oneWeekBefore = dateUtils.parse("2016-10-22T09:16:00+02:00");
24 24
	private static final Date twoWeekBefore = dateUtils.parse("2016-10-15T09:16:00+02:00");
25
	private static final Date tenMinutesAfter = dateUtils.parse("2016-10-29T09:26:00+02:00");
26
	private static final Date oneHourAfter = dateUtils.parse("2016-10-29T10:16:00+02:00");
25 27

  
26 28
	@Test
27
	@Ignore
28 29
	public void testVerifyScheduled() {
29 30
		assertTrue(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, null, date));
30 31
		assertTrue(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, twoWeekBefore, date));
32
		assertTrue(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, null, tenMinutesAfter));
33
		assertTrue(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, twoWeekBefore, tenMinutesAfter));
31 34

  
32 35
		assertFalse(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, null, oneDayBefore));
33 36
		assertFalse(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, oneDayBefore, date));
34 37
		assertFalse(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, oneWeekBefore, date));
38

  
39
		assertFalse(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, null, oneHourAfter));
40
		assertFalse(launcher.isReady(WF_ID, CRON_EXPR, ALMOST_TWO_WEEKS, twoWeekBefore, oneHourAfter));
35 41
	}
36 42

  
37 43
}
modules/dnet-msro-service/trunk/src/main/java/eu/dnetlib/msro/cron/ScheduledWorkflowLauncher.java
30 30

  
31 31
	private GraphProcessRegistry graphProcessRegistry;
32 32

  
33
	private int windowSize = 7200000; // 2 hours
33
	private int windowSize = 1800000; // 30 minutes
34 34

  
35 35
	@Resource
36 36
	private UniqueServiceLocator serviceLocator;
......
48 48
				final String[] arr = s.split("@@@");
49 49
				final String id = arr[0].trim();
50 50
				final String cron = arr[1].trim();
51
				final int minInterval = NumberUtils.toInt(arr[2].trim(), 0);
51
				final int minInterval = Math.max(NumberUtils.toInt(arr[2].trim(), 0), windowSize);
52 52
				final Date lastExecutionDate = calculateLastExecutionDate(id);
53 53

  
54 54
				if (isReady(id, cron, minInterval, lastExecutionDate, new Date()) && !isAlreadyRunning(id)) {
......
73 73
			final boolean res;
74 74
			if (lastExecutionDate != null) {
75 75
				final int elapsed = Math.round(now.getTime() - lastExecutionDate.getTime());
76
				res = (elapsed > minIntervalMillis) && verifyCron(cron, lastExecutionDate, now);
76
				res = (elapsed > minIntervalMillis) && verifyCron(cron, now);
77 77
			} else {
78
				res = verifyCron(cron, null, now);
78
				res = verifyCron(cron, now);
79 79
			}
80 80

  
81 81
			if (log.isDebugEnabled()) {
......
96 96
		return false;
97 97
	}
98 98

  
99
	private boolean verifyCron(final String cronExpression, final Date prevDate, final Date now) {
99
	private boolean verifyCron(final String cronExpression, final Date now) {
100 100
		try {
101 101
			final CronExpression cron = new CronExpression(cronExpression);
102 102

  
103
			final Date startWindowDate = new Date(now.getTime() - windowSize);
103
			final Date date = new Date(now.getTime() - windowSize);
104 104

  
105
			final Date date = prevDate != null ? prevDate : startWindowDate;
106 105
			final Date cronDate = cron.getNextValidTimeAfter(date);
107 106

  
108 107
			if (log.isDebugEnabled()) {
modules/dnet-msro-service/trunk/src/main/resources/eu/dnetlib/msro/service/applicationContext-msro-service.properties
6 6
msro.wf.max.running = 5
7 7
msro.wf.scheduler.startDelay = 60000
8 8
msro.wf.scheduler.repeatInterval = 600000
9
msro.wf.scheduler.window.size = 7200000
9
msro.wf.scheduler.window.size = 1800000
10 10

  
11 11
msro.wf.mail.thread.startDelay = 60000
12 12
msro.wf.mail.thread.repeatInterval = 60000

Also available in: Unified diff