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 {
// 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.
private static volatile long shuttingTime = 0;
private static volatile long turningOnTime = 0;
private static String shutdownToken = null;
......@@ -218,7 +219,7 @@ public class BuildServer {
// DRAINING: We have reached > 2/3 of max permitted jobs
// We return bad health (but accept jobs) until
// 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
// the load to become < 1/3 max load
......@@ -237,6 +238,9 @@ public class BuildServer {
} else if (shut == ShutdownState.DRAINING) {
LOG.info("Healthcheck: DRAINING");
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 {
LOG.info("Healthcheck: SHUTTING");
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 {
if (shuttingTime != 0) {
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("uptime-in-ms", runtimeBean.getUptime() + "");
variables.put("vm-name", runtimeBean.getVmName());
......@@ -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
* App Inventor source zip that's generated by the Download Source command. The differences are
......@@ -760,6 +801,11 @@ public class BuildServer {
}
private ShutdownState getShutdownState() {
if (turningOnTime != 0 && System.currentTimeMillis() > turningOnTime && turningOnTime > shuttingTime) {
turningOnTime = 0;
shuttingTime = 0;
}
if (shuttingTime == 0) {
int max = buildExecutor.getMaxActiveTasks();
if (max < 10) { // Only do this scheme if we are not unlimited
......@@ -781,10 +827,12 @@ public class BuildServer {
} else {
return ShutdownState.UP;
}
} else if (System.currentTimeMillis() > shuttingTime) {
return ShutdownState.DOWN;
} else {
} else if (turningOnTime >= System.currentTimeMillis()) {
return ShutdownState.TURNING;
} else if (shuttingTime >= System.currentTimeMillis()) {
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