Search in sources :

Example 21 with CronExpression

use of org.quartz.CronExpression in project openremote by openremote.

the class TimerProtocol method doLinkProtocolConfiguration.

@Override
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
    AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
    // Verify that this is a valid Timer Configuration
    if (!isValidTimerConfiguration(protocolConfiguration)) {
        LOG.warning("Timer Configuration is not valid so it will be ignored: " + protocolRef);
        updateStatus(protocolRef, ConnectionStatus.ERROR);
        return;
    }
    // Validate the cron expression
    CronExpressionParser expressionParser = getCronExpression(protocolConfiguration).map(CronExpressionParser::new).orElse(null);
    if (expressionParser == null || !expressionParser.isValid()) {
        LOG.warning("Timer cron expression is missing or invalid");
        updateStatus(protocolRef, ConnectionStatus.ERROR);
        return;
    }
    cronExpressionMap.put(protocolRef, expressionParser);
    if (protocolConfiguration.isEnabled()) {
        updateStatus(protocolRef, ConnectionStatus.CONNECTED);
        CronExpression cronExpression = createCronExpression(expressionParser.buildCronExpression());
        if (cronExpression != null) {
            getCronScheduler().addOrReplaceJob(getTimerId(protocolRef), cronExpression, () -> doTriggerAction(protocolConfiguration));
        } else {
            updateStatus(protocolRef, ConnectionStatus.DISABLED);
        }
    }
}
Also used : CronExpression(org.quartz.CronExpression)

Example 22 with CronExpression

use of org.quartz.CronExpression in project OpenOLAT by OpenOLAT.

the class ReminderModuleTest method testCronJob_everyHeightHours.

@Test
public void testCronJob_everyHeightHours() throws ParseException {
    reminderModule.setScheduler("8", "6:30");
    sleep(1000);
    String cron = reminderModule.getCronExpression();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 5);
    CronExpression cronExpression = new CronExpression(cron);
    Date d1 = cronExpression.getNextValidTimeAfter(cal.getTime());
    Calendar triggerCal = Calendar.getInstance();
    triggerCal.setTime(d1);
    Assert.assertEquals(6, triggerCal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, triggerCal.get(Calendar.MINUTE));
    Date d2 = cronExpression.getNextValidTimeAfter(d1);
    triggerCal.setTime(d2);
    Assert.assertEquals(14, triggerCal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, triggerCal.get(Calendar.MINUTE));
    Date d3 = cronExpression.getNextValidTimeAfter(d2);
    triggerCal.setTime(d3);
    Assert.assertEquals(22, triggerCal.get(Calendar.HOUR_OF_DAY));
    Assert.assertEquals(30, triggerCal.get(Calendar.MINUTE));
}
Also used : Calendar(java.util.Calendar) CronExpression(org.quartz.CronExpression) Date(java.util.Date) Test(org.junit.Test)

Example 23 with CronExpression

use of org.quartz.CronExpression in project kylo by Teradata.

the class CronExpressionTest method testTimerToCron.

@Test
public void testTimerToCron() {
    String timer = "30 hours";
    try {
        CronExpression expression = CronExpressionUtil.timerToCronExpression(timer);
        String cron = expression.getCronExpression();
        Assert.assertNotNull(cron);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Also used : CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 24 with CronExpression

use of org.quartz.CronExpression in project kylo by Teradata.

the class FeedOnTimeArrivalMetricAssessorTest method testNoDataButStillBeforeLateTime.

@Test
public /**
 * test use case where the date window is still valid, but still no data has been found for the Feed.
 * This will return no Assessment result as it still could process data
 */
void testNoDataButStillBeforeLateTime() throws ParseException {
    PowerMockito.mockStatic(DateTime.class);
    PowerMockito.mockStatic(CronExpressionUtil.class);
    DateTime now = new DateTime().minusHours(2);
    // Some Cron Expression .. Mockito will overwrite
    CronExpression cron = new CronExpression("0 0 0/2 1/1 * ? *");
    // set the current time to a known time
    BDDMockito.given(DateTime.now()).willReturn(now);
    // set the previous fire date to a known time in the past,but within the window
    DateTime previousFireTime = new DateTime(now).minusHours(3);
    BDDMockito.given(CronExpressionUtil.getPreviousFireTime(cron)).willReturn(previousFireTime.toDate());
    // window is = (now - 3)  - (now -3) + lateTime)
    // Some Feed End Time to a time not within this window
    DateTime lastFeedTime = new DateTime().minusWeeks(2);
    when(this.feedProvider.getLastActiveTimeStamp("feed")).thenReturn(lastFeedTime);
    this.metric = new FeedOnTimeArrivalMetric("feed", cron, Period.hours(lateTimeGracePeriod));
    this.assessor.assess(metric, this.builder);
    // assert values
    DateTime lateTime = previousFireTime.plusHours(lateTimeGracePeriod);
    Assert.assertTrue(now.isBefore(lateTime) && !(lastFeedTime.isAfter(previousFireTime) && lastFeedTime.isBefore(lateTime)));
    verify(this.builder);
}
Also used : CronExpression(org.quartz.CronExpression) FeedOnTimeArrivalMetric(com.thinkbiganalytics.metadata.sla.api.core.FeedOnTimeArrivalMetric) DateTime(org.joda.time.DateTime) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with CronExpression

use of org.quartz.CronExpression in project kylo by Teradata.

the class FeedOnTimeArrivalMetricAssessorTest method setUp.

@Before
public void setUp() throws Exception {
    initMocks(this);
    when(this.builder.message(any(String.class))).thenReturn(this.builder);
    when(this.builder.metric(any(Metric.class))).thenReturn(this.builder);
    when(this.builder.result(any(AssessmentResult.class))).thenReturn(this.builder);
    this.assessor.setMetadataAccess(this.metadataAccess);
    // Noon every day
    CronExpression cron = new CronExpression("0 0 12 1/1 * ? *");
    this.lateTime = new DateTime(CronExpressionUtil.getPreviousFireTime(cron)).plusHours(4);
    this.metric = new FeedOnTimeArrivalMetric("feed", cron, Period.hours(lateTimeGracePeriod));
}
Also used : Metric(com.thinkbiganalytics.metadata.sla.api.Metric) FeedOnTimeArrivalMetric(com.thinkbiganalytics.metadata.sla.api.core.FeedOnTimeArrivalMetric) CronExpression(org.quartz.CronExpression) FeedOnTimeArrivalMetric(com.thinkbiganalytics.metadata.sla.api.core.FeedOnTimeArrivalMetric) AssessmentResult(com.thinkbiganalytics.metadata.sla.api.AssessmentResult) DateTime(org.joda.time.DateTime) Before(org.junit.Before)

Aggregations

CronExpression (org.quartz.CronExpression)30 Date (java.util.Date)13 ParseException (java.text.ParseException)12 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)6 Calendar (java.util.Calendar)5 DateTime (org.joda.time.DateTime)5 FeedOnTimeArrivalMetric (com.thinkbiganalytics.metadata.sla.api.core.FeedOnTimeArrivalMetric)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 RFC5545Schedule (com.hubspot.singularity.helpers.RFC5545Schedule)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Matcher (java.util.regex.Matcher)2 ProcessException (org.apache.nifi.processor.exception.ProcessException)2 InvalidRecurrenceRuleException (org.dmfs.rfc5545.recur.InvalidRecurrenceRuleException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SingularityDeployStatistics (com.hubspot.singularity.SingularityDeployStatistics)1 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)1 AbstractJob (com.jeesuite.scheduler.AbstractJob)1