From edd3c4c7fbeee9ecb581222b2ed56ca9412eeaa7 Mon Sep 17 00:00:00 2001 From: Shreyas Gopalakrishna <11889130+shreyas-gopalakrishna@users.noreply.github.com> Date: Fri, 15 Sep 2023 11:06:51 -0500 Subject: [PATCH] Updated to fall back to old command line args if new args are missing (#728) --- .../azure/functions/worker/Application.java | 87 ++++++++++++++++--- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/microsoft/azure/functions/worker/Application.java b/src/main/java/com/microsoft/azure/functions/worker/Application.java index d20436af..fb1c8095 100644 --- a/src/main/java/com/microsoft/azure/functions/worker/Application.java +++ b/src/main/java/com/microsoft/azure/functions/worker/Application.java @@ -25,12 +25,40 @@ private void parseCommandLine(String[] args) { CommandLineParser parser = new DefaultParser(); try { CommandLine commands = parser.parse(this.OPTIONS, args, true); - this.uri = this.parseUri(commands.getOptionValue(FUNCTIONS_URI_OPTION)); - this.workerId = this.parseWorkerId(commands.getOptionValue(FUNCTIONS_WORKER_ID_OPTION)); - this.requestId = this.parseRequestId(commands.getOptionValue(FUNCTIONS_REQUEST_ID_OPTION)); - this.logToConsole = commands.hasOption(FUNCTIONS_CONSOLE_LOG_OPTION); + + if (commands.hasOption("fu")) { + this.uri = this.parseUri(commands.getOptionValue(FUNCTIONS_URI_OPTION)); + }else if (commands.hasOption("h") && commands.hasOption("p")) { + this.host = this.parseHost(commands.getOptionValue("h")); + this.port = this.parsePort(commands.getOptionValue("p")); + }else { + throw new ParseException("Error parsing command line options. Please include functions host and port or uri."); + } + + if (commands.hasOption("fw")) { + this.workerId = this.parseWorkerId(commands.getOptionValue(FUNCTIONS_WORKER_ID_OPTION)); + }else if (commands.hasOption("w")) { + this.workerId = this.parseWorkerId(commands.getOptionValue("w")); + }else { + throw new ParseException("Error parsing command line options. Please include worker id."); + } + + if (commands.hasOption("fq")) { + this.requestId = this.parseRequestId(commands.getOptionValue(FUNCTIONS_REQUEST_ID_OPTION)); + }else if (commands.hasOption("q")) { + this.requestId = this.parseRequestId(commands.getOptionValue("q")); + }else { + throw new ParseException("Error parsing command line options. Please include request id."); + } + + this.logToConsole = commands.hasOption(FUNCTIONS_CONSOLE_LOG_OPTION) || commands.hasOption("l"); + if (commands.hasOption(FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION)) { this.maxMessageSize = this.parseMaxMessageSize(commands.getOptionValue(FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION)); + } else if (commands.hasOption("m")) { + this.maxMessageSize = this.parseMaxMessageSize(commands.getOptionValue("m")); + }else { + throw new ParseException("Error parsing command line options. Please include message size in bytes."); } this.commandParseSucceeded = true; } catch (ParseException ex) { @@ -61,26 +89,46 @@ public static void main(String[] args) { private boolean logToConsole; private Integer maxMessageSize = null; private final Options OPTIONS = new Options() - .addOption(Option.builder("u").longOpt(FUNCTIONS_URI_OPTION) + .addOption(Option.builder("h").longOpt("host") + .hasArg().argName("HostName") + .desc("The address of the machine that the Azure Functions host is running on") + .build()) + .addOption(Option.builder("p").longOpt("port") + .hasArg().argName("PortNumber") + .desc("The port number which the Azure Functions host is listening to") + .build()) + .addOption(Option.builder("w").longOpt("workerId") + .hasArg().argName("WorkerId") + .desc("The ID of this running worker throughout communication session") + .build()) + .addOption(Option.builder("q").longOpt("requestId") + .hasArg().argName("RequestId") + .desc("The startup request ID of this communication session") + .build()) + .addOption(Option.builder("l").longOpt("consoleLog") + .desc("Whether to duplicate all host logs to console as well") + .build()) + .addOption(Option.builder("m").longOpt("grpcMaxMessageLength") + .hasArg().argName("MessageSizeInBytes") + .desc("The maximum message size could be used by GRPC protocol") + .build()) + .addOption(Option.builder("fu").longOpt(FUNCTIONS_URI_OPTION) .hasArg().argName("Uri") .desc("The uri of the machine that the Azure Functions host is running on") - .required() .build()) - .addOption(Option.builder("w").longOpt(FUNCTIONS_WORKER_ID_OPTION) + .addOption(Option.builder("fw").longOpt(FUNCTIONS_WORKER_ID_OPTION) .hasArg().argName("WorkerId") .desc("The ID of this running worker throughout communication session") - .required() .build()) - .addOption(Option.builder("q").longOpt(FUNCTIONS_REQUEST_ID_OPTION) + .addOption(Option.builder("fq").longOpt(FUNCTIONS_REQUEST_ID_OPTION) .hasArg().argName("RequestId") .desc("The startup request ID of this communication session") - .required() .build()) - .addOption(Option.builder("l").longOpt(FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION) + .addOption(Option.builder("fm").longOpt(FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION) .hasArg().argName("MessageSizeInBytes") .desc("The maximum message size could be used by GRPC protocol") .build()) - .addOption(Option.builder("m").longOpt(FUNCTIONS_CONSOLE_LOG_OPTION) + .addOption(Option.builder("fl").longOpt(FUNCTIONS_CONSOLE_LOG_OPTION) .desc("Whether to duplicate all host logs to console as well") .build()); @@ -89,6 +137,21 @@ public String getHost() { return this.host; } + private String parseHost(String input) { return input; } + + private int parsePort(String input) throws ParseException { + try { + int result = Integer.parseInt(input); + if (result < 1 || result > 65535) { + throw new IndexOutOfBoundsException("port number out of range"); + } + return result; + } catch (NumberFormatException | IndexOutOfBoundsException ex) { + throw new ParseException(String.format( + "port number \"%s\" is not qualified. It must be an integer within range [1, 65535]", input)); + } + } + @Override public int getPort() { return this.port;