Search in sources :

Example 1 with ShinyProxyException

use of eu.openanalytics.shinyproxy.ShinyProxyException in project shinyproxy by openanalytics.

the class ProxyService method waitForLaunchingProxy.

private void waitForLaunchingProxy(String userName, String appName) {
    int totalWaitMs = Integer.parseInt(environment.getProperty("shiny.proxy.container-wait-time", "20000"));
    int waitMs = Math.min(2000, totalWaitMs);
    int maxTries = totalWaitMs / waitMs;
    boolean mayProceed = Utils.retry(i -> {
        return getProxies(p -> p.getStatus() == ContainerProxyStatus.Starting && isUserProxy(p, userName, appName)).isEmpty();
    }, maxTries, waitMs);
    if (!mayProceed)
        throw new ShinyProxyException("Cannot proceed: waiting for proxy to launch");
}
Also used : URISyntaxException(java.net.URISyntaxException) ContainerProxyRequest(eu.openanalytics.shinyproxy.container.ContainerProxyRequest) HttpServerExchange(io.undertow.server.HttpServerExchange) ShinyApp(eu.openanalytics.shinyproxy.services.AppService.ShinyApp) HashMap(java.util.HashMap) ContainerProxyStatus(eu.openanalytics.shinyproxy.container.ContainerProxyStatus) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) Logger(org.apache.log4j.Logger) HttpServletRequest(javax.servlet.http.HttpServletRequest) Service(org.springframework.stereotype.Service) Map(java.util.Map) IContainerProxy(eu.openanalytics.shinyproxy.container.IContainerProxy) BiConsumer(java.util.function.BiConsumer) Utils(eu.openanalytics.shinyproxy.util.Utils) URI(java.net.URI) IContainerBackend(eu.openanalytics.shinyproxy.container.IContainerBackend) ExecutorService(java.util.concurrent.ExecutorService) WebUtils(org.springframework.web.util.WebUtils) Predicate(java.util.function.Predicate) Set(java.util.Set) Cookie(io.undertow.server.handlers.Cookie) File(java.io.File) Executors(java.util.concurrent.Executors) List(java.util.List) EventType(eu.openanalytics.shinyproxy.services.EventService.EventType) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) Environment(org.springframework.core.env.Environment) PostConstruct(javax.annotation.PostConstruct) Collections(java.util.Collections) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException)

Example 2 with ShinyProxyException

use of eu.openanalytics.shinyproxy.ShinyProxyException in project shinyproxy by openanalytics.

the class AbstractContainerBackend method startProxy.

@Override
public void startProxy(IContainerProxy p) throws ShinyProxyException {
    T proxy = getProxyCast(p);
    proxy.setStatus(ContainerProxyStatus.Starting);
    try {
        doStartProxy(proxy);
        calculateTargetURL(proxy);
    } catch (Throwable t) {
        disposeProxy(proxy);
        throw new ShinyProxyException("Failed to start container", t);
    }
    if (!testProxy(proxy)) {
        stopProxy(proxy);
        throw new ShinyProxyException("Container did not respond in time");
    }
    proxy.setStartupTimestamp(System.currentTimeMillis());
    proxy.setStatus(ContainerProxyStatus.Up);
}
Also used : ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException)

Example 3 with ShinyProxyException

use of eu.openanalytics.shinyproxy.ShinyProxyException in project shinyproxy by openanalytics.

the class AbstractContainerBackend method stopProxy.

@Override
public void stopProxy(IContainerProxy p) throws ShinyProxyException {
    T proxy = getProxyCast(p);
    try {
        proxy.setStatus(ContainerProxyStatus.Stopping);
        disposeProxy(proxy);
        doStopProxy(proxy);
        proxy.setStatus(ContainerProxyStatus.Stopped);
    } catch (Exception e) {
        throw new ShinyProxyException("Failed to stop container", e);
    }
}
Also used : ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) IOException(java.io.IOException) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException)

Example 4 with ShinyProxyException

use of eu.openanalytics.shinyproxy.ShinyProxyException in project shinyproxy by openanalytics.

the class AbstractContainerBackend method createProxy.

@Override
public IContainerProxy createProxy(ContainerProxyRequest request) throws ShinyProxyException {
    T proxy = instantiateProxy();
    proxy.setStatus(ContainerProxyStatus.New);
    proxy.setUserId(request.userId);
    proxy.setApp(request.app);
    if (!useInternalNetwork)
        proxy.setPort(allocatePort());
    try {
        prepareProxy(proxy, request);
    } catch (Exception e) {
        disposeProxy(proxy);
        if (e instanceof ShinyProxyException)
            throw (ShinyProxyException) e;
        throw new ShinyProxyException("Failed to create container", e);
    }
    return proxy;
}
Also used : ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) IOException(java.io.IOException) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException)

Example 5 with ShinyProxyException

use of eu.openanalytics.shinyproxy.ShinyProxyException in project shinyproxy by openanalytics.

the class ProxyService method startProxy.

private IContainerProxy startProxy(String userName, String appName) {
    ShinyApp app = appService.getApp(appName);
    if (app == null) {
        throw new ShinyProxyException("Cannot start container: unknown application: " + appName);
    }
    if (findProxy(userName, appName) != null) {
        throw new ShinyProxyException("Cannot start container: user " + userName + " already has an active proxy for " + appName);
    }
    ContainerProxyRequest request = new ContainerProxyRequest();
    request.userId = userName;
    request.app = app;
    IContainerProxy proxy = backend.createProxy(request);
    activeProxies.add(proxy);
    try {
        backend.startProxy(proxy);
    } finally {
        if (proxy.getStatus() != ContainerProxyStatus.Up)
            activeProxies.remove(proxy);
    }
    try {
        URI target = new URI(proxy.getTarget());
        synchronized (mappingListeners) {
            for (MappingListener listener : mappingListeners) {
                listener.mappingAdded(proxy.getName(), target);
            }
        }
    } catch (URISyntaxException ignore) {
    }
    if (logService.isContainerLoggingEnabled()) {
        BiConsumer<File, File> outputAttacher = backend.getOutputAttacher(proxy);
        if (outputAttacher == null) {
            log.warn("Cannot log container output: " + backend.getClass() + " does not support output attaching.");
        } else {
            logService.attachToOutput(proxy, outputAttacher);
        }
    }
    log.info(String.format("Proxy activated [user: %s] [app: %s]", userName, appName));
    eventService.post(EventType.AppStart.toString(), userName, appName);
    return proxy;
}
Also used : ContainerProxyRequest(eu.openanalytics.shinyproxy.container.ContainerProxyRequest) IContainerProxy(eu.openanalytics.shinyproxy.container.IContainerProxy) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) ShinyApp(eu.openanalytics.shinyproxy.services.AppService.ShinyApp)

Aggregations

ShinyProxyException (eu.openanalytics.shinyproxy.ShinyProxyException)7 ContainerProxyRequest (eu.openanalytics.shinyproxy.container.ContainerProxyRequest)2 IContainerProxy (eu.openanalytics.shinyproxy.container.IContainerProxy)2 ShinyApp (eu.openanalytics.shinyproxy.services.AppService.ShinyApp)2 File (java.io.File)2 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)1 ContainerProxyStatus (eu.openanalytics.shinyproxy.container.ContainerProxyStatus)1 IContainerBackend (eu.openanalytics.shinyproxy.container.IContainerBackend)1 EventType (eu.openanalytics.shinyproxy.services.EventService.EventType)1 Utils (eu.openanalytics.shinyproxy.util.Utils)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 Cookie (io.undertow.server.handlers.Cookie)1 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1