Don't dump a stacktrace for invalid patterns when executing elasticsearch-croneval (#49744) (#50578)

Co-authored-by: bellengao <gbl_long@163.com>
This commit is contained in:
Lee Hinman 2020-01-02 16:57:51 -07:00 committed by GitHub
parent 81a9cff16f
commit 0d78aa2708
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 38 deletions

View File

@ -31,6 +31,10 @@ This command is provided in the `$ES_HOME/bin` directory.
The number of future times this expression will be triggered. The default The number of future times this expression will be triggered. The default
value is `10`. value is `10`.
`-d, --detail`::
Shows detail for invalid cron expression. It will print the stacktrace if the
expression is not valid.
`-h, --help`:: `-h, --help`::
Returns all of the command parameters. Returns all of the command parameters.

View File

@ -37,13 +37,16 @@ public class CronEvalTool extends LoggingAwareCommand {
private final OptionSpec<Integer> countOption; private final OptionSpec<Integer> countOption;
private final OptionSpec<String> arguments; private final OptionSpec<String> arguments;
private final OptionSpec<Void> detailOption;
CronEvalTool() { CronEvalTool() {
super("Validates and evaluates a cron expression"); super("Validates and evaluates a cron expression");
this.countOption = parser.acceptsAll(Arrays.asList("c", "count"), this.countOption = parser.acceptsAll(Arrays.asList("c", "count"), "The number of future times this expression will be triggered")
"The number of future times this expression will be triggered") .withRequiredArg()
.withRequiredArg().ofType(Integer.class).defaultsTo(10); .ofType(Integer.class)
.defaultsTo(10);
this.arguments = parser.nonOptions("expression"); this.arguments = parser.nonOptions("expression");
this.detailOption = parser.acceptsAll(Arrays.asList("d", "detail"), "Show detail for invalid cron expression");
parser.accepts("E", "Unused. Only for compatibility with other CLI tools.").withRequiredArg(); parser.accepts("E", "Unused. Only for compatibility with other CLI tools.").withRequiredArg();
} }
@ -55,10 +58,12 @@ public class CronEvalTool extends LoggingAwareCommand {
if (args.size() != 1) { if (args.size() != 1) {
throw new UserException(ExitCodes.USAGE, "expecting a single argument that is the cron expression to evaluate"); throw new UserException(ExitCodes.USAGE, "expecting a single argument that is the cron expression to evaluate");
} }
execute(terminal, args.get(0), count); boolean printDetail = options.has(detailOption);
execute(terminal, args.get(0), count, printDetail);
} }
private void execute(Terminal terminal, String expression, int count) throws Exception { private void execute(Terminal terminal, String expression, int count, boolean printDetail) throws Exception {
try {
Cron.validate(expression); Cron.validate(expression);
terminal.println("Valid!"); terminal.println("Valid!");
@ -67,7 +72,9 @@ public class CronEvalTool extends LoggingAwareCommand {
if (isLocalTimeUTC) { if (isLocalTimeUTC) {
terminal.println("Now is [" + UTC_FORMATTER.format(date) + "] in UTC"); terminal.println("Now is [" + UTC_FORMATTER.format(date) + "] in UTC");
} else { } else {
terminal.println("Now is [" + UTC_FORMATTER.format(date) + "] in UTC, local time is [" + LOCAL_FORMATTER.format(date) + "]"); terminal.println(
"Now is [" + UTC_FORMATTER.format(date) + "] in UTC, local time is [" + LOCAL_FORMATTER.format(date) + "]"
);
} }
terminal.println("Here are the next " + count + " times this cron expression will trigger:"); terminal.println("Here are the next " + count + " times this cron expression will trigger:");
@ -80,11 +87,16 @@ public class CronEvalTool extends LoggingAwareCommand {
time = cron.getNextValidTimeAfter(time); time = cron.getNextValidTimeAfter(time);
if (time < 0) { if (time < 0) {
if (i == 0) { if (i == 0) {
throw new UserException(ExitCodes.OK, "Could not compute future times since [" throw new UserException(
+ UTC_FORMATTER.format(Instant.ofEpochMilli(prevTime)) + "] " + "(perhaps the cron expression only points to " + ExitCodes.OK,
"times in the" + "Could not compute future times since ["
" " + + UTC_FORMATTER.format(Instant.ofEpochMilli(prevTime))
"past?)"); + "] "
+ "(perhaps the cron expression only points to "
+ "times in the"
+ " "
+ "past?)"
);
} }
break; break;
} }
@ -96,5 +108,13 @@ public class CronEvalTool extends LoggingAwareCommand {
terminal.println("\t" + LOCAL_FORMATTER.format(Instant.ofEpochMilli(time))); terminal.println("\t" + LOCAL_FORMATTER.format(Instant.ofEpochMilli(time)));
} }
} }
} catch (Exception e) {
if (printDetail) {
throw e;
} else {
throw new UserException(ExitCodes.OK, e.getMessage() + (e.getCause() == null ? "" : ": " + e.getCause().getMessage()));
}
}
} }
} }