Commit bc27297d authored by Diego Barreiro's avatar Diego Barreiro Committed by Jeffrey Schiller

Added TurnOn Function to BuildServer (#1871)

Provide a way to re-enable a previously shutdown buildserver. This provides more granular control for buildservers behind a load balancer.
parent 9024435b
...@@ -208,6 +208,7 @@ public class BuildServer { ...@@ -208,6 +208,7 @@ public class BuildServer {
// otherwise still accepting jobs. This avoids having people get an error if the load // otherwise still accepting jobs. This avoids having people get an error if the load
// balancer sends a job our way because it hasn't decided we are down. // balancer sends a job our way because it hasn't decided we are down.
private static volatile long shuttingTime = 0; private static volatile long shuttingTime = 0;
private static volatile long turningOnTime = 0;
private static String shutdownToken = null; private static String shutdownToken = null;
...@@ -218,7 +219,7 @@ public class BuildServer { ...@@ -218,7 +219,7 @@ public class BuildServer {
// DRAINING: We have reached > 2/3 of max permitted jobs // DRAINING: We have reached > 2/3 of max permitted jobs
// We return bad health (but accept jobs) until // We return bad health (but accept jobs) until
// the number of active jobs is < 1/3 of max // the number of active jobs is < 1/3 of max
private enum ShutdownState { UP, SHUTTING, DOWN, DRAINING }; private enum ShutdownState { UP, SHUTTING, TURNING, DOWN, DRAINING };
private static volatile boolean draining = false; // We have exceeded 2/3 max load, waiting for private static volatile boolean draining = false; // We have exceeded 2/3 max load, waiting for
// the load to become < 1/3 max load // the load to become < 1/3 max load
...@@ -237,6 +238,9 @@ public class BuildServer { ...@@ -237,6 +238,9 @@ public class BuildServer {
} else if (shut == ShutdownState.DRAINING) { } else if (shut == ShutdownState.DRAINING) {
LOG.info("Healthcheck: DRAINING"); LOG.info("Healthcheck: DRAINING");
return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("Build Server is draining").build(); return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("Build Server is draining").build();
} else if (shut == ShutdownState.TURNING) {
LOG.info("Healthcheck: TURNING");
return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("Build Server is turning on").build();
} else { } else {
LOG.info("Healthcheck: SHUTTING"); LOG.info("Healthcheck: SHUTTING");
return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("Build Server is shutting down").build(); return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("Build Server is shutting down").build();
...@@ -256,6 +260,9 @@ public class BuildServer { ...@@ -256,6 +260,9 @@ public class BuildServer {
if (shuttingTime != 0) { if (shuttingTime != 0) {
variables.put("shutdown-time", dateTimeFormat.format(new Date(shuttingTime))); variables.put("shutdown-time", dateTimeFormat.format(new Date(shuttingTime)));
} }
if (turningOnTime != 0) {
variables.put("turnon-time", dateTimeFormat.format(new Date(turningOnTime)));
}
variables.put("start-time", dateTimeFormat.format(new Date(runtimeBean.getStartTime()))); variables.put("start-time", dateTimeFormat.format(new Date(runtimeBean.getStartTime())));
variables.put("uptime-in-ms", runtimeBean.getUptime() + ""); variables.put("uptime-in-ms", runtimeBean.getUptime() + "");
variables.put("vm-name", runtimeBean.getVmName()); variables.put("vm-name", runtimeBean.getVmName());
...@@ -345,6 +352,40 @@ public class BuildServer { ...@@ -345,6 +352,40 @@ public class BuildServer {
} }
} }
/**
* Indicate that the server is turning on.
*
* @param token -- secret token used like a password to authenticate the shutdown command
* @param delay -- the delay in seconds before jobs are no longer accepted
*/
@GET
@Path("turnon")
@Produces(MediaType.TEXT_PLAIN)
public Response turnon(@QueryParam("token") String token, @QueryParam("delay") String delay) throws IOException {
if (commandLineOptions.shutdownToken == null || token == null) {
return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("No Shutdown Token").build();
} else if (!token.equals(commandLineOptions.shutdownToken)) {
return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("Invalid Shutdown Token").build();
} else {
if (shuttingTime == 0) {
return Response.status(Response.Status.FORBIDDEN).type(MediaType.TEXT_PLAIN_TYPE).entity("Buildserver is not expected to be shutted down").build();
}
long turnonTime = System.currentTimeMillis();
if (delay != null) {
try {
turnonTime += Integer.parseInt(delay) *1000;
} catch (NumberFormatException e) {
// XXX Ignore
}
}
turningOnTime = turnonTime;
DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
return Response.ok("ok: Will turnon at " + dateTimeFormat.format(new Date(turningOnTime)),
MediaType.TEXT_PLAIN_TYPE).build();
}
}
/** /**
* Build an APK file from the input zip file. The zip file needs to be a variant of the same * Build an APK file from the input zip file. The zip file needs to be a variant of the same
* App Inventor source zip that's generated by the Download Source command. The differences are * App Inventor source zip that's generated by the Download Source command. The differences are
...@@ -760,6 +801,11 @@ public class BuildServer { ...@@ -760,6 +801,11 @@ public class BuildServer {
} }
private ShutdownState getShutdownState() { private ShutdownState getShutdownState() {
if (turningOnTime != 0 && System.currentTimeMillis() > turningOnTime && turningOnTime > shuttingTime) {
turningOnTime = 0;
shuttingTime = 0;
}
if (shuttingTime == 0) { if (shuttingTime == 0) {
int max = buildExecutor.getMaxActiveTasks(); int max = buildExecutor.getMaxActiveTasks();
if (max < 10) { // Only do this scheme if we are not unlimited if (max < 10) { // Only do this scheme if we are not unlimited
...@@ -781,10 +827,12 @@ public class BuildServer { ...@@ -781,10 +827,12 @@ public class BuildServer {
} else { } else {
return ShutdownState.UP; return ShutdownState.UP;
} }
} else if (System.currentTimeMillis() > shuttingTime) { } else if (turningOnTime >= System.currentTimeMillis()) {
return ShutdownState.DOWN; return ShutdownState.TURNING;
} else { } else if (shuttingTime >= System.currentTimeMillis()) {
return ShutdownState.SHUTTING; return ShutdownState.SHUTTING;
} else {
return ShutdownState.DOWN;
} }
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment