Skip to content

Commit b037e95

Browse files
Adding support for updated command line args (#726)
1 parent ff172d5 commit b037e95

File tree

2 files changed

+105
-74
lines changed

2 files changed

+105
-74
lines changed
Lines changed: 99 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.microsoft.azure.functions.worker;
22

3+
import java.net.MalformedURLException;
4+
import java.net.URISyntaxException;
5+
import java.net.URL;
36
import java.util.logging.*;
47
import javax.annotation.*;
58

69
import org.apache.commons.cli.*;
710
import org.apache.commons.lang3.exception.ExceptionUtils;
811

12+
import static com.microsoft.azure.functions.worker.Constants.*;
13+
914
/**
1015
* The entry point of the Java Language Worker. Every component could get the command line options from this singleton
1116
* Application instance, and typically that instance will be passed to your components as constructor arguments.
@@ -15,36 +20,17 @@ private Application(String[] args) {
1520
this.parseCommandLine(args);
1621
}
1722

18-
@Override
19-
public String getHost() { return this.host; }
20-
@Override
21-
public int getPort() { return this.port; }
22-
@Override
23-
public boolean logToConsole() { return this.logToConsole; }
24-
@Override
25-
public Integer getMaxMessageSize() { return this.maxMessageSize; }
26-
private String getWorkerId() { return this.workerId; }
27-
private String getRequestId() { return this.requestId; }
28-
29-
private void printUsage() {
30-
HelpFormatter formatter = new HelpFormatter();
31-
formatter.printHelp("Application", this.OPTIONS, true);
32-
}
33-
34-
private boolean isCommandlineValid() { return this.commandParseSucceeded; }
35-
3623
@PostConstruct
3724
private void parseCommandLine(String[] args) {
3825
CommandLineParser parser = new DefaultParser();
3926
try {
40-
CommandLine commands = parser.parse(this.OPTIONS, args);
41-
this.host = this.parseHost(commands.getOptionValue("h"));
42-
this.port = this.parsePort(commands.getOptionValue("p"));
43-
this.workerId = this.parseWorkerId(commands.getOptionValue("w"));
44-
this.requestId = this.parseRequestId(commands.getOptionValue("q"));
45-
this.logToConsole = commands.hasOption("l");
46-
if (commands.hasOption("m")) {
47-
this.maxMessageSize = this.parseMaxMessageSize(commands.getOptionValue("m"));
27+
CommandLine commands = parser.parse(this.OPTIONS, args, true);
28+
this.uri = this.parseUri(commands.getOptionValue(FUNCTIONS_URI_OPTION));
29+
this.workerId = this.parseWorkerId(commands.getOptionValue(FUNCTIONS_WORKER_ID_OPTION));
30+
this.requestId = this.parseRequestId(commands.getOptionValue(FUNCTIONS_REQUEST_ID_OPTION));
31+
this.logToConsole = commands.hasOption(FUNCTIONS_CONSOLE_LOG_OPTION);
32+
if (commands.hasOption(FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION)) {
33+
this.maxMessageSize = this.parseMaxMessageSize(commands.getOptionValue(FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION));
4834
}
4935
this.commandParseSucceeded = true;
5036
} catch (ParseException ex) {
@@ -53,84 +39,123 @@ private void parseCommandLine(String[] args) {
5339
}
5440
}
5541

56-
private String parseHost(String input) { return input; }
57-
58-
private int parsePort(String input) throws ParseException {
59-
try {
60-
int result = Integer.parseInt(input);
61-
if (result < 1 || result > 65535) {
62-
throw new IndexOutOfBoundsException("port number out of range");
42+
public static void main(String[] args) {
43+
WorkerLogManager.getSystemLogger().log(Level.INFO, "Azure Functions Java Worker version [ " + version() + "]");
44+
Application app = new Application(args);
45+
if (!app.isCommandlineValid()) {
46+
app.printUsage();
47+
System.exit(1);
48+
} else {
49+
try (JavaWorkerClient client = new JavaWorkerClient(app)) {
50+
client.listen(app.getWorkerId(), app.getRequestId()).get();
51+
} catch (Exception ex) {
52+
WorkerLogManager.getSystemLogger().log(Level.SEVERE, ExceptionUtils.getRootCauseMessage(ex), ex);
53+
System.exit(-1);
6354
}
64-
return result;
65-
} catch (NumberFormatException | IndexOutOfBoundsException ex) {
66-
throw new ParseException(String.format(
67-
"port number \"%s\" is not qualified. It must be an integer within range [1, 65535]", input));
6855
}
6956
}
7057

71-
private String parseRequestId(String input) { return input; }
72-
73-
private String parseWorkerId(String input) { return input; }
74-
75-
private Integer parseMaxMessageSize(String input) {
76-
return Integer.parseInt(input);
77-
}
78-
7958
private boolean commandParseSucceeded = false;
80-
private String host;
59+
private String uri, host, workerId, requestId;
8160
private int port;
82-
private String workerId, requestId;
8361
private boolean logToConsole;
8462
private Integer maxMessageSize = null;
85-
8663
private final Options OPTIONS = new Options()
87-
.addOption(Option.builder("h").longOpt("host")
88-
.hasArg().argName("HostName")
89-
.desc("The address of the machine that the Azure Functions host is running on")
90-
.required()
91-
.build())
92-
.addOption(Option.builder("p").longOpt("port")
93-
.hasArg().argName("PortNumber")
94-
.desc("The port number which the Azure Functions host is listening to")
64+
.addOption(Option.builder("u").longOpt(FUNCTIONS_URI_OPTION)
65+
.hasArg().argName("Uri")
66+
.desc("The uri of the machine that the Azure Functions host is running on")
9567
.required()
9668
.build())
97-
.addOption(Option.builder("w").longOpt("workerId")
69+
.addOption(Option.builder("w").longOpt(FUNCTIONS_WORKER_ID_OPTION)
9870
.hasArg().argName("WorkerId")
9971
.desc("The ID of this running worker throughout communication session")
10072
.required()
10173
.build())
102-
.addOption(Option.builder("q").longOpt("requestId")
74+
.addOption(Option.builder("q").longOpt(FUNCTIONS_REQUEST_ID_OPTION)
10375
.hasArg().argName("RequestId")
10476
.desc("The startup request ID of this communication session")
10577
.required()
10678
.build())
107-
.addOption(Option.builder("l").longOpt("consoleLog")
108-
.desc("Whether to duplicate all host logs to console as well")
109-
.build())
110-
.addOption(Option.builder("m").longOpt("grpcMaxMessageLength")
79+
.addOption(Option.builder("l").longOpt(FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION)
11180
.hasArg().argName("MessageSizeInBytes")
11281
.desc("The maximum message size could be used by GRPC protocol")
82+
.build())
83+
.addOption(Option.builder("m").longOpt(FUNCTIONS_CONSOLE_LOG_OPTION)
84+
.desc("Whether to duplicate all host logs to console as well")
11385
.build());
11486

87+
@Override
88+
public String getHost() {
89+
return this.host;
90+
}
11591

116-
public static void main(String[] args) {
117-
WorkerLogManager.getSystemLogger().log(Level.INFO, "Azure Functions Java Worker version [ " + version() + "]");
118-
Application app = new Application(args);
119-
if (!app.isCommandlineValid()) {
120-
app.printUsage();
121-
System.exit(1);
122-
} else {
123-
try (JavaWorkerClient client = new JavaWorkerClient(app)) {
124-
client.listen(app.getWorkerId(), app.getRequestId()).get();
125-
} catch (Exception ex) {
126-
WorkerLogManager.getSystemLogger().log(Level.SEVERE, ExceptionUtils.getRootCauseMessage(ex), ex);
127-
System.exit(-1);
92+
@Override
93+
public int getPort() {
94+
return this.port;
95+
}
96+
97+
public String getUri() {
98+
return this.uri;
99+
}
100+
101+
@Override
102+
public boolean logToConsole() {
103+
return this.logToConsole;
104+
}
105+
106+
@Override
107+
public Integer getMaxMessageSize() {
108+
return this.maxMessageSize;
109+
}
110+
111+
private String getWorkerId() {
112+
return this.workerId;
113+
}
114+
115+
private String getRequestId() {
116+
return this.requestId;
117+
}
118+
119+
private boolean isCommandlineValid() {
120+
return this.commandParseSucceeded;
121+
}
122+
123+
private String parseUri(String uri) throws ParseException {
124+
try {
125+
URL url = new URL(uri);
126+
url.toURI();
127+
this.host = url.getHost();
128+
this.port = url.getPort();
129+
if (port < 1 || port > 65535) {
130+
throw new IndexOutOfBoundsException("port number out of range");
128131
}
132+
return uri;
133+
} catch (MalformedURLException | URISyntaxException | IndexOutOfBoundsException e) {
134+
throw new ParseException(String.format(
135+
"Error parsing URI \"%s\". Please provide a valid URI", uri));
129136
}
130137
}
131138

139+
private String parseRequestId(String input) {
140+
return input;
141+
}
142+
143+
private String parseWorkerId(String input) {
144+
return input;
145+
}
146+
147+
private Integer parseMaxMessageSize(String input) {
148+
return Integer.parseInt(input);
149+
}
150+
132151
public static String version() {
133152
String jarVersion = Application.class.getPackage().getImplementationVersion();
134153
return jarVersion != null && !jarVersion.isEmpty() ? jarVersion : "Unknown";
135154
}
155+
156+
private void printUsage() {
157+
HelpFormatter formatter = new HelpFormatter();
158+
formatter.setWidth(100);
159+
formatter.printHelp("Application", this.OPTIONS, true);
160+
}
136161
}

src/main/java/com/microsoft/azure/functions/worker/Constants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
*/
66
public final class Constants {
77
private Constants(){}
8+
9+
public final static String FUNCTIONS_URI_OPTION = "functions-uri";
10+
public final static String FUNCTIONS_WORKER_ID_OPTION = "functions-worker-id";
11+
public final static String FUNCTIONS_REQUEST_ID_OPTION = "functions-request-id";
12+
public final static String FUNCTIONS_GRPC_MAX_MESSAGE_LENGTH_OPTION = "functions-grpc-max-message-length";
13+
public final static String FUNCTIONS_CONSOLE_LOG_OPTION = "functions-console-log";
814
public final static String TRIGGER_METADATA_DOLLAR_REQUEST_KEY = "$request";
915
public final static String JAVA_LIBRARY_DIRECTORY = "/annotationLib";
1016
public final static String JAVA_LIBRARY_ARTIFACT_ID = "azure-functions-java-library";

0 commit comments

Comments
 (0)