Skip to content

Commit

Permalink
Updated to fall back to old command line args if new args are missing (
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-gopalakrishna committed Sep 15, 2023
1 parent b037e95 commit edd3c4c
Showing 1 changed file with 75 additions and 12 deletions.
87 changes: 75 additions & 12 deletions src/main/java/com/microsoft/azure/functions/worker/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());

Expand All @@ -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;
Expand Down

0 comments on commit edd3c4c

Please sign in to comment.