Skip to content

Commit

Permalink
remove old deployment mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
scarytom committed Jun 29, 2012
1 parent 735516e commit 1a4ad8b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected void beforeBlondinStartsUpWith(Properties properties, List<String> exp

@Test public void
retrieves_list_of_expensive_resources_via_get_request() throws Exception {
final TrivialResponse response = TrivialHttpClient.getFrom(blondinUrl() + "/status");
final TrivialResponse response = TrivialHttpClient.getFrom(blondinUrl() + "/info/status");

assertThat(response.content, containsString("/my/{catchy}/res1"));
assertThat(response.content, containsString("/my/res2"));
Expand Down
12 changes: 0 additions & 12 deletions src/acceptancetest/java/com/timgroup/blondin/StatusPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,4 @@ public final class StatusPageTest extends BlondinAcceptanceTestBase {
assertThat(response.content, startsWith("<?xml version=\"1.0\" ?>"));
assertThat(response.content, containsString("Version" + expectedVersionString));
}

@Test public void
serves_obsolete_status_page() throws Exception {
final String currentVersion = AppInfoHandler.class.getPackage().getImplementationVersion();
final String expectedVersionString = (null == currentVersion) ? "" : ": <value>" + currentVersion + "</value>";

final TrivialResponse response = TrivialHttpClient.getFrom(blondinUrl() + "/status");

assertThat(response.code, is(200));
assertThat(response.content, startsWith("<?xml version=\"1.0\" ?>"));
assertThat(response.content, containsString("Version" + expectedVersionString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ protected void beforeBlondinStartsUpWith(Properties properties, List<String> exp
@Test(timeout=10000L) public void
responds_to_a_stop_request() throws Exception {
TrivialHttpClient.getFrom(blondinUrl() + "/some/resource/or/other");
TrivialHttpClient.post(blondinUrl() + "/stop");


stopBlondin();
Sockets.waitForNoSocket("localhost", blondinPort());
assertThat(Sockets.isSocketOpen("localhost", blondinPort()), is(false));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.timgroup.blondin.testutil;

import static java.lang.String.format;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
Expand All @@ -17,8 +18,6 @@
import com.google.common.io.Files;
import com.timgroup.blondin.Blondin;

import static java.lang.String.format;

public class BlondinAcceptanceTestBase {

@Rule
Expand Down Expand Up @@ -58,9 +57,8 @@ public final void startBlondin() throws Exception {
@SuppressWarnings("unused")
protected void beforeBlondinStartsUpWith(Properties properties, List<String> expensiveResources) throws Exception { }

@After
public final void stopBlondin() throws Exception {
TrivialHttpClient.post(format("http://localhost:%s/stop", blondinPort));
Blondin.stopAllInstances();
}

public final int blondinPort() {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/timgroup/blondin/Blondin.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.timgroup.blondin;

import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.LinkedBlockingQueue;

import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
import com.timgroup.blondin.config.BlondinConfiguration;
import com.timgroup.blondin.config.BlondinParametersParser;
import com.timgroup.blondin.diagnostics.ExternalRecorder;
Expand All @@ -11,6 +15,8 @@

public final class Blondin {

private static final LinkedBlockingQueue<BlondinServer> instances = Queues.newLinkedBlockingQueue();

private static final Supplier<BlondinConfiguration> USAGE_SUPPLIER = new Supplier<BlondinConfiguration>() {
@Override public BlondinConfiguration get() {
throw new IllegalArgumentException("Usage: Blondin [port] configfile.properties");
Expand All @@ -26,6 +32,7 @@ public static void main(String[] args) {
System.out.printf("Starting blondin on port %s targetting %s:%s\n", config.blondinPort(), config.targetHost(), config.targetPort());
blondinServer = new BlondinServer(monitor, config.blondinPort(), config.targetHost(), config.targetPort(),
config.expensiveResourcesUrl(), config.throttleSize());
instances.add(blondinServer);
}
catch (IOException e) {
monitor.logError(Blondin.class, "Failed to start Blondin server", e);
Expand All @@ -39,4 +46,12 @@ public static void main(String[] args) {
}
}));
}

public static void stopAllInstances() {
final Collection<BlondinServer> runningInstances = Lists.newArrayList();
instances.drainTo(runningInstances);
for (BlondinServer instance : runningInstances) {
instance.stop();
}
}
}
51 changes: 1 addition & 50 deletions src/main/java/com/timgroup/blondin/server/BlondinServer.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.timgroup.blondin.server;

import static com.timgroup.blondin.server.RequestDispatcher.GET;
import static com.timgroup.blondin.server.RequestDispatcher.POST;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URL;

import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.core.ContainerServer;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;

import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.timgroup.blondin.config.ExpensiveResourceListLoader;
import com.timgroup.blondin.diagnostics.Monitor;
import com.timgroup.blondin.proxy.ProxyingHandler;
Expand All @@ -34,39 +30,22 @@ public final class BlondinServer {

private final Monitor monitor;
private final Connection connection;
private volatile BlondinServerStatus status = BlondinServerStatus.STOPPED;

private final Supplier<BlondinServerStatus> statusSupplier = new Supplier<BlondinServerStatus>() {
@Override public BlondinServerStatus get() {
return status;
}
};

public BlondinServer(Monitor monitor, int blondinPort, String targetHost, int targetPort,
URL expensiveResourcesUrl, int throttleSize) throws IOException
{
this.monitor = monitor;

final RequestDispatcher dispatcher = new RequestDispatcher(monitor);
dispatcher.register(POST.forPath("/stop"), new StopHandler());
dispatcher.register(POST.forPath("/suspend"), new SuspendHandler());

final ExpensiveResourceListLoader throttleListSupplier = new ExpensiveResourceListLoader(monitor, expensiveResourcesUrl);
final AppInfoHandler appInfoHandler = new AppInfoHandler(monitor, statusSupplier, throttleListSupplier);
dispatcher.register(GET.forPath(startingWith("/info")), appInfoHandler);
dispatcher.register(GET.forPath(startingWith("/status")), appInfoHandler);
dispatcher.register(GET.forPath(startingWith("/info")), new AppInfoHandler(monitor, throttleListSupplier));

final Container proxy = ProxyingHandler.create(monitor, targetHost, targetPort);
dispatcher.register(GET.forPath(throttleListSupplier), new MetricRecordingHandler(monitor, "requests.expensive", new ThrottlingHandler(proxy, throttleSize)));
dispatcher.register(GET, new MetricRecordingHandler(monitor, "requests.normal", proxy));

connection = new SocketConnection(new ContainerServer(new LoggingHandler(monitor, dispatcher), THREAD_COUNT));
connection.connect(new InetSocketAddress(blondinPort));
status = BlondinServerStatus.RUNNING;
}

public BlondinServerStatus status() {
return status;
}

public void stop() {
Expand All @@ -77,34 +56,6 @@ public void stop() {
monitor.logWarning(BlondinServer.class, "Failed to stop Blondin server", e);
}
monitor.stop();
status = BlondinServerStatus.STOPPED;
}

public void suspend() {
status = BlondinServerStatus.SUSPENDED;
}

private final class SuspendHandler implements Container {
@Override public void handle(Request request, Response response) {
closeSafely(response);
suspend();
}
}

private final class StopHandler implements Container {
@Override public void handle(Request request, Response response) {
closeSafely(response);
stop();
}
}

private void closeSafely(Response response) {
try {
response.close();
}
catch (Exception e) {
monitor.logWarning(BlondinServer.class, "Failed to close response", e);
}
}

private static final Predicate<String> startingWith(final String pathPrefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,22 @@

import com.google.common.base.Supplier;
import com.timgroup.blondin.diagnostics.Monitor;
import com.timgroup.blondin.server.BlondinServerStatus;
import com.timgroup.blondin.server.status.BlondinStatus;
import com.timgroup.tucker.info.ApplicationInformationHandler;
import com.timgroup.tucker.info.servlet.WebResponse;

import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;

public final class AppInfoHandler implements Container {

private static final String INFO_PATH = "/info";

private final Monitor monitor;
private final BlondinStatus status;
private final Supplier<BlondinServerStatus> serverStatusSupplier;
private final ApplicationInformationHandler handler;

public AppInfoHandler(Monitor monitor,
Supplier<BlondinServerStatus> serverStatusSupplier,
Supplier<Iterable<String>> expensiveResourcesListSupplier)
{
this.monitor = monitor;
this.serverStatusSupplier = serverStatusSupplier;
this.status = new BlondinStatus(expensiveResourcesListSupplier);
this.handler = new ApplicationInformationHandler(this.status.generator());
}
Expand All @@ -41,11 +35,7 @@ public AppInfoHandler(Monitor monitor,
public void handle(Request request, Response response) {
try {
final String path = request.getPath().getPath();
if (path.startsWith(INFO_PATH)) {
handler.handle(path.substring(INFO_PATH.length()), new ResponseWrapper(INFO_PATH, response));
return;
}
handleObsoleteStatusRequests(response, path);
handler.handle(path.substring(INFO_PATH.length()), new ResponseWrapper(INFO_PATH, response));
} catch (IOException e) {
monitor.logError(AppInfoHandler.class, "Failed to respond to status page request", e);
} finally {
Expand All @@ -57,20 +47,7 @@ public void handle(Request request, Response response) {
}
}

private void handleObsoleteStatusRequests(Response response, final String path) throws IOException {
if (path.equals("/status")) {
if (BlondinServerStatus.SUSPENDED.equals(serverStatusSupplier.get())) {
response.setCode(HTTP_UNAVAILABLE);
response.setText("Service Unavailable");
}
handler.handle("/status", new ResponseWrapper(INFO_PATH, response));
} else if (path.startsWith("/status")) {
handler.handle(path, new ResponseWrapper("", response));
}
}

private static final class ResponseWrapper implements WebResponse {

private final String path;
private final Response response;

Expand Down
Loading

0 comments on commit 1a4ad8b

Please sign in to comment.