Improve cron exception handling (#4553)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>pull/4556/head
parent
f8d34d9882
commit
229d87263f
|
@ -68,9 +68,13 @@ public class GenericCronTriggerHandler extends BaseTriggerModuleHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
private void scheduleJob() {
|
private void scheduleJob() {
|
||||||
schedule = scheduler.schedule(this, expression);
|
try {
|
||||||
logger.debug("Scheduled cron job '{}' for trigger '{}'.", module.getConfiguration().get(CFG_CRON_EXPRESSION),
|
schedule = scheduler.schedule(this, expression);
|
||||||
module.getId());
|
logger.debug("Scheduled cron job '{}' for trigger '{}'.",
|
||||||
|
module.getConfiguration().get(CFG_CRON_EXPRESSION), module.getId());
|
||||||
|
} catch (IllegalArgumentException e) { // Catch exception from CronAdjuster
|
||||||
|
logger.warn("Failed to schedule job for trigger '{}'. {}", module.getId(), e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
*/
|
*/
|
||||||
package org.openhab.core.scheduler;
|
package org.openhab.core.scheduler;
|
||||||
|
|
||||||
|
import java.time.DateTimeException;
|
||||||
import java.time.DayOfWeek;
|
import java.time.DayOfWeek;
|
||||||
import java.time.temporal.ChronoField;
|
import java.time.temporal.ChronoField;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
@ -71,7 +72,6 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
|
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
|
||||||
|
|
||||||
private final List<Field> fields = new ArrayList<>(7);
|
private final List<Field> fields = new ArrayList<>(7);
|
||||||
private String cronExpression;
|
|
||||||
private final Map<String, String> environmentMap;
|
private final Map<String, String> environmentMap;
|
||||||
private final boolean reboot;
|
private final boolean reboot;
|
||||||
|
|
||||||
|
@ -83,7 +83,6 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||||
environmentMap = parseEnvironment(entries);
|
environmentMap = parseEnvironment(entries);
|
||||||
|
|
||||||
String cronExpression = entries[entries.length - 1].trim();
|
String cronExpression = entries[entries.length - 1].trim();
|
||||||
this.cronExpression = cronExpression;
|
|
||||||
|
|
||||||
reboot = "@reboot".equals(cronExpression);
|
reboot = "@reboot".equals(cronExpression);
|
||||||
|
|
||||||
|
@ -108,6 +107,14 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||||
parseAndAdd(cronExpression, parts[2], ChronoField.HOUR_OF_DAY);
|
parseAndAdd(cronExpression, parts[2], ChronoField.HOUR_OF_DAY);
|
||||||
parseAndAdd(cronExpression, parts[1], ChronoField.MINUTE_OF_HOUR);
|
parseAndAdd(cronExpression, parts[1], ChronoField.MINUTE_OF_HOUR);
|
||||||
parseAndAdd(cronExpression, parts[0], ChronoField.SECOND_OF_MINUTE);
|
parseAndAdd(cronExpression, parts[0], ChronoField.SECOND_OF_MINUTE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Test the cron expression in action to make sure it won't cause too many restarts
|
||||||
|
adjustInto(java.time.ZonedDateTime.now());
|
||||||
|
} catch (final DateTimeException e) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("Invalid cron expression '%s': %s", cronExpression, e.getMessage()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -529,8 +536,7 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||||
index++;
|
index++;
|
||||||
} else {
|
} else {
|
||||||
if (restarts++ > 1000) {
|
if (restarts++ > 1000) {
|
||||||
throw new IllegalArgumentException(
|
throw new DateTimeException("Conditions not satisfied.");
|
||||||
String.format("Cron expression '%s' is not valid, too many restarts", cronExpression));
|
|
||||||
}
|
}
|
||||||
ret = out;
|
ret = out;
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|
|
@ -203,9 +203,6 @@ public class CronAdjusterTest {
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = { "0 0 0 31 2 *", "* * *", "80 * * * * *" })
|
@ValueSource(strings = { "0 0 0 31 2 *", "* * *", "80 * * * * *" })
|
||||||
public void testInvalidCronExpression(String cron) {
|
public void testInvalidCronExpression(String cron) {
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
assertThrows(IllegalArgumentException.class, () -> new CronAdjuster(cron));
|
||||||
final CronAdjuster cronAdjuster = new CronAdjuster(cron);
|
|
||||||
cronAdjuster.adjustInto(java.time.ZonedDateTime.now());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue