Skip to content

Commit e43aa56

Browse files
committed
Immediately shutdown standalone/hub/node if the port is busy with a human readable message
1 parent 02d2c4a commit e43aa56

File tree

6 files changed

+51
-18
lines changed

6 files changed

+51
-18
lines changed

java/server/src/org/openqa/grid/internal/utils/SelfRegisteringRemote.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ public void setRemoteServer(GridNodeServer server) {
116116
this.server = server;
117117
}
118118

119-
public void startRemoteServer() throws Exception {
119+
public boolean startRemoteServer() throws Exception {
120120
if (server == null) {
121121
throw new GridConfigurationException("no server set to register to the hub");
122122
}
123123
server.setExtraServlets(nodeServlets);
124-
server.boot();
124+
return server.boot();
125125
}
126126

127127
public void stopRemoteServer() {

java/server/src/org/openqa/grid/selenium/GridLauncherV3.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,13 @@ public void setConfiguration(String[] args) {
229229
}
230230

231231
public void launch() throws Exception {
232-
log.info("Launching a standalone Selenium Server");
232+
log.info(String.format(
233+
"Launching a standalone Selenium Server on port %s", configuration.port));
233234
SeleniumServer server = new SeleniumServer(configuration);
234235
Map<String, Class<? extends Servlet >> servlets = new HashMap<>();
235236
servlets.put("/*", DisplayHelpServlet.class);
236237
server.setExtraServlets(servlets);
237238
server.boot();
238-
log.info("Selenium Server is up and running");
239239
}
240240
})
241241
.put(GridRole.HUB.toString(), () -> new GridItemLauncher() {
@@ -256,11 +256,10 @@ public void setConfiguration(String[] args) {
256256
}
257257

258258
public void launch() throws Exception {
259-
log.info("Launching Selenium Grid hub");
259+
log.info(String.format(
260+
"Launching Selenium Grid hub on port %s", configuration.port));
260261
Hub h = new Hub(configuration);
261262
h.start();
262-
log.info("Nodes should register to " + h.getRegistrationURL());
263-
log.info("Selenium Grid hub is up and running");
264263
}
265264
})
266265
.put(GridRole.NODE.toString(), () -> new GridItemLauncher() {
@@ -284,12 +283,14 @@ public void setConfiguration(String[] args) {
284283
}
285284

286285
public void launch() throws Exception {
287-
log.info("Launching a Selenium Grid node");
286+
log.info(String.format(
287+
"Launching a Selenium Grid node on port %s", configuration.port));
288288
SelfRegisteringRemote remote = new SelfRegisteringRemote(configuration);
289289
remote.setRemoteServer(new SeleniumServer(remote.getConfiguration()));
290-
remote.startRemoteServer();
291-
log.info("Selenium Grid node is up and ready to register to the hub");
292-
remote.startRegistrationProcess();
290+
if (remote.startRemoteServer()) {
291+
log.info("Selenium Grid node is up and ready to register to the hub");
292+
remote.startRegistrationProcess();
293+
}
293294
}
294295
});
295296

java/server/src/org/openqa/grid/shared/GridNodeServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import javax.servlet.Servlet;
2525

2626
public interface GridNodeServer {
27-
void boot() throws Exception;
27+
boolean boot() throws Exception;
2828
void stop();
2929
int getRealPort();
3030
void setExtraServlets(Map<String, Class<? extends Servlet>> extraServlets);

java/server/src/org/openqa/grid/web/Hub.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.seleniumhq.jetty9.servlet.ServletHolder;
4848
import org.seleniumhq.jetty9.util.thread.QueuedThreadPool;
4949

50+
import java.net.BindException;
5051
import java.net.MalformedURLException;
5152
import java.net.URL;
5253
import java.util.Map;
@@ -174,8 +175,6 @@ private void initServer() {
174175
httpConfig.setSecureScheme("https");
175176
httpConfig.setSecurePort(config.port);
176177

177-
log.info("Will listen on " + config.port);
178-
179178
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
180179
http.setPort(config.port);
181180

@@ -211,7 +210,26 @@ public Map<?,?> getConfigurationForJMX() {
211210

212211
public void start() throws Exception {
213212
initServer();
214-
server.start();
213+
214+
try {
215+
server.start();
216+
} catch (Exception e) {
217+
try {
218+
stop();
219+
} catch (Exception ignore) {
220+
}
221+
if (e instanceof BindException) {
222+
log.severe(String.format(
223+
"Port %s is busy, please choose a free port for the hub and specify it using -port option", config.port));
224+
return;
225+
} else {
226+
throw new RuntimeException(e);
227+
}
228+
}
229+
230+
log.info("Selenium Grid hub is up and running");
231+
log.info(String.format("Nodes should register to %s", getRegistrationURL()));
232+
log.info(String.format("Clients should connect to %s", getWebDriverHubRequestURL()));
215233
}
216234

217235
public void stop() throws Exception {

java/server/src/org/openqa/selenium/remote/server/SeleniumServer.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.seleniumhq.jetty9.util.security.Constraint;
4646
import org.seleniumhq.jetty9.util.thread.QueuedThreadPool;
4747

48+
import java.net.BindException;
4849
import java.util.Map;
4950
import java.util.concurrent.TimeUnit;
5051
import java.util.logging.Logger;
@@ -121,7 +122,7 @@ public void setExtraServlets(Map<String, Class<? extends Servlet>> extraServlets
121122
this.extraServlets = extraServlets;
122123
}
123124

124-
public void boot() {
125+
public boolean boot() {
125126
if (configuration.jettyMaxThreads != null && configuration.jettyMaxThreads > 0) {
126127
server = new Server(new QueuedThreadPool(configuration.jettyMaxThreads));
127128
} else {
@@ -195,8 +196,21 @@ public void boot() {
195196
try {
196197
server.start();
197198
} catch (Exception e) {
198-
throw new RuntimeException(e);
199+
try {
200+
server.stop();
201+
} catch (Exception ignore) {
202+
}
203+
if (e instanceof BindException) {
204+
LOG.severe(String.format(
205+
"Port %s is busy, please choose a free port and specify it using -port option", configuration.port));
206+
return false;
207+
} else {
208+
throw new RuntimeException(e);
209+
}
199210
}
211+
212+
LOG.info(String.format("Selenium Server is up and running on port %s", configuration.port));
213+
return true;
200214
}
201215

202216
private NewSessionPipeline createPipeline(StandaloneConfiguration configuration) {

java/server/test/org/openqa/grid/internal/utils/SelfRegisteringRemoteTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private final class DummyGridNodeServer implements GridNodeServer {
3939
public Map<String, Class<? extends Servlet>> extraServlets;
4040

4141
@Override
42-
public void boot() throws Exception { }
42+
public boolean boot() throws Exception { return true; }
4343

4444
@Override
4545
public void stop() { }

0 commit comments

Comments
 (0)