Skip to content

Commit deedec7

Browse files
lipsillspinscale
authored andcommitted
Watcher: Improve error messages for CronEvalTool (#32800)
CronEvalTool prints an error only for cron expressions that result in no upcoming time events. If a cron expression results in less than the specified count (default 10) time events, now all the coming times are printed without displaying error message. Closes #32735
1 parent 3c0fe7d commit deedec7

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ void execute(Terminal terminal, String expression, int count) throws Exception {
6161

6262
Cron cron = new Cron(expression);
6363
long time = date.getMillis();
64+
6465
for (int i = 0; i < count; i++) {
6566
long prevTime = time;
6667
time = cron.getNextValidTimeAfter(time);
6768
if (time < 0) {
68-
throw new UserException(ExitCodes.OK, (i + 1) + ".\t Could not compute future times since ["
69-
+ formatter.print(prevTime) + "] " + "(perhaps the cron expression only points to times in the past?)");
69+
if (i == 0) {
70+
throw new UserException(ExitCodes.OK, "Could not compute future times since ["
71+
+ formatter.print(prevTime) + "] " + "(perhaps the cron expression only points to times in the past?)");
72+
}
73+
break;
7074
}
71-
terminal.println((i+1) + ".\t" + formatter.print(time));
75+
terminal.println((i + 1) + ".\t" + formatter.print(time));
7276
}
7377
}
7478
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolTests.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
import org.elasticsearch.cli.Command;
99
import org.elasticsearch.cli.CommandTestCase;
1010

11+
import java.util.Calendar;
12+
import java.util.Locale;
13+
import java.util.TimeZone;
14+
15+
import static org.hamcrest.Matchers.containsString;
16+
import static org.hamcrest.Matchers.not;
17+
1118
public class CronEvalToolTests extends CommandTestCase {
1219
@Override
1320
protected Command newCommand() {
@@ -18,6 +25,27 @@ public void testParse() throws Exception {
1825
String countOption = randomBoolean() ? "-c" : "--count";
1926
int count = randomIntBetween(1, 100);
2027
String output = execute(countOption, Integer.toString(count), "0 0 0 1-6 * ?");
21-
assertTrue(output, output.contains("Here are the next " + count + " times this cron expression will trigger"));
28+
assertThat(output, containsString("Here are the next " + count + " times this cron expression will trigger"));
29+
}
30+
31+
public void testGetNextValidTimes() throws Exception {
32+
final int year = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT).get(Calendar.YEAR) + 1;
33+
{
34+
String output = execute("0 3 23 8 9 ? " + year);
35+
assertThat(output, containsString("Here are the next 10 times this cron expression will trigger:"));
36+
assertThat(output, not(containsString("ERROR")));
37+
assertThat(output, not(containsString("2.\t")));
38+
}
39+
{
40+
String output = execute("0 3 23 */4 9 ? " + year);
41+
assertThat(output, containsString("Here are the next 10 times this cron expression will trigger:"));
42+
assertThat(output, not(containsString("ERROR")));
43+
}
44+
{
45+
Exception expectThrows = expectThrows(Exception.class, () -> execute("0 3 23 */4 9 ? 2017"));
46+
String message = expectThrows.getMessage();
47+
assertThat(message, containsString("Could not compute future times since"));
48+
assertThat(message, containsString("(perhaps the cron expression only points to times in the past?)"));
49+
}
2250
}
2351
}

0 commit comments

Comments
 (0)