Skip to content

Commit 229d872

Browse files
authored
Improve cron exception handling (openhab#4553)
Signed-off-by: Jimmy Tanagra <[email protected]>
1 parent f8d34d9 commit 229d872

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/GenericCronTriggerHandler.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ public synchronized void setCallback(ModuleHandlerCallback callback) {
6868
}
6969

7070
private void scheduleJob() {
71-
schedule = scheduler.schedule(this, expression);
72-
logger.debug("Scheduled cron job '{}' for trigger '{}'.", module.getConfiguration().get(CFG_CRON_EXPRESSION),
73-
module.getId());
71+
try {
72+
schedule = scheduler.schedule(this, expression);
73+
logger.debug("Scheduled cron job '{}' for trigger '{}'.",
74+
module.getConfiguration().get(CFG_CRON_EXPRESSION), module.getId());
75+
} catch (IllegalArgumentException e) { // Catch exception from CronAdjuster
76+
logger.warn("Failed to schedule job for trigger '{}'. {}", module.getId(), e.getMessage());
77+
}
7478
}
7579

7680
@Override

bundles/org.openhab.core/src/main/java/org/openhab/core/scheduler/CronAdjuster.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.openhab.core.scheduler;
1414

15+
import java.time.DateTimeException;
1516
import java.time.DayOfWeek;
1617
import java.time.temporal.ChronoField;
1718
import java.time.temporal.ChronoUnit;
@@ -71,7 +72,6 @@ interface Checker {
7172
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
7273

7374
private final List<Field> fields = new ArrayList<>(7);
74-
private String cronExpression;
7575
private final Map<String, String> environmentMap;
7676
private final boolean reboot;
7777

@@ -83,7 +83,6 @@ public CronAdjuster(final String specification) {
8383
environmentMap = parseEnvironment(entries);
8484

8585
String cronExpression = entries[entries.length - 1].trim();
86-
this.cronExpression = cronExpression;
8786

8887
reboot = "@reboot".equals(cronExpression);
8988

@@ -108,6 +107,14 @@ public CronAdjuster(final String specification) {
108107
parseAndAdd(cronExpression, parts[2], ChronoField.HOUR_OF_DAY);
109108
parseAndAdd(cronExpression, parts[1], ChronoField.MINUTE_OF_HOUR);
110109
parseAndAdd(cronExpression, parts[0], ChronoField.SECOND_OF_MINUTE);
110+
111+
try {
112+
// Test the cron expression in action to make sure it won't cause too many restarts
113+
adjustInto(java.time.ZonedDateTime.now());
114+
} catch (final DateTimeException e) {
115+
throw new IllegalArgumentException(
116+
String.format("Invalid cron expression '%s': %s", cronExpression, e.getMessage()));
117+
}
111118
}
112119

113120
/**
@@ -529,8 +536,7 @@ public Temporal adjustInto(@Nullable final Temporal temporal) {
529536
index++;
530537
} else {
531538
if (restarts++ > 1000) {
532-
throw new IllegalArgumentException(
533-
String.format("Cron expression '%s' is not valid, too many restarts", cronExpression));
539+
throw new DateTimeException("Conditions not satisfied.");
534540
}
535541
ret = out;
536542
index = 0;

bundles/org.openhab.core/src/test/java/org/openhab/core/internal/scheduler/CronAdjusterTest.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,6 @@ public void testCronExpression(String in, String cron, String[] outs) {
203203
@ParameterizedTest
204204
@ValueSource(strings = { "0 0 0 31 2 *", "* * *", "80 * * * * *" })
205205
public void testInvalidCronExpression(String cron) {
206-
assertThrows(IllegalArgumentException.class, () -> {
207-
final CronAdjuster cronAdjuster = new CronAdjuster(cron);
208-
cronAdjuster.adjustInto(java.time.ZonedDateTime.now());
209-
});
206+
assertThrows(IllegalArgumentException.class, () -> new CronAdjuster(cron));
210207
}
211208
}

0 commit comments

Comments
 (0)