Skip to content

Commit

Permalink
GH-407 Simplify launching process (Resolve #407)
Browse files Browse the repository at this point in the history
GH-408 Application should exit if port is already in use (Fix #408)
GH-411 Log should be properly saved thanks to #407 (Fix #411)
  • Loading branch information
dzikoysk committed May 12, 2021
1 parent 7568313 commit a64816f
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .run/Reposilite.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Reposilite" type="Application" factoryName="Application">
<configuration default="false" name="Reposilite" type="Application" factoryName="Application" singleton="false">
<option name="MAIN_CLASS_NAME" value="org.panda_lang.reposilite.ReposiliteLauncher" />
<module name="reposilite-parent.reposilite-backend.main" />
<option name="VM_PARAMETERS" value="-Xmx14M -Dreposilite.debugEnabled=true -Dtinylog.writerActive.level=DEBUG" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import org.panda_lang.reposilite.utils.RunUtils;
import org.panda_lang.reposilite.utils.TimeUtils;
import org.panda_lang.utilities.commons.ValidationUtils;
import org.panda_lang.utilities.commons.function.ThrowingRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.*;
import java.util.Arrays;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;

public final class Reposilite {
Expand All @@ -57,7 +57,6 @@ public final class Reposilite {
private final FileSystemStorageProvider storageProvider;
private final Configuration configuration;
private final ReposiliteContextFactory contextFactory;
private final ReposiliteExecutor executor;
private final FailureService failureService;
private final Authenticator authenticator;
private final RepositoryAuthenticator repositoryAuthenticator;
Expand All @@ -70,7 +69,7 @@ public final class Reposilite {
private final ProxyService proxyService;
private final DeployService deployService;
private final FrontendProvider frontend;
private final ReposiliteHttpServer reactiveHttpServer;
private final ReposiliteHttpServer httpServer;
private final Console console;
private final Thread shutdownHook;
private long uptime;
Expand All @@ -89,7 +88,6 @@ public final class Reposilite {
this.storageProvider = FileSystemStorageProvider.of(Paths.get(""), this.configuration.diskQuota);
this.contextFactory = new ReposiliteContextFactory(configuration.forwardedIp);
this.failureService = new FailureService();
this.executor = new ReposiliteExecutor(testEnvEnabled, failureService);
this.tokenService = new TokenService(workingDirectory, storageProvider);
this.statsService = new StatsService(workingDirectory, failureService, storageProvider);
this.repositoryService = new RepositoryService();
Expand All @@ -102,7 +100,7 @@ public final class Reposilite {
this.lookupService = new LookupService(repositoryAuthenticator, repositoryService);
this.proxyService = new ProxyService(configuration.storeProxied, configuration.proxyPrivate, configuration.proxyConnectTimeout, configuration.proxyReadTimeout, configuration.proxied, repositoryService, failureService, storageProvider);
this.frontend = FrontendProvider.load(configuration);
this.reactiveHttpServer = new ReposiliteHttpServer(this, servlet);
this.httpServer = new ReposiliteHttpServer(this, servlet);
this.console = new Console(System.in, failureService);
this.shutdownHook = new Thread(RunUtils.ofChecked(failureService, this::shutdown));
}
Expand Down Expand Up @@ -133,9 +131,6 @@ public void load() throws Exception {
getLogger().info("Working directory: " + workingDirectory.toAbsolutePath());
getLogger().info("");

this.alive.set(true);
Runtime.getRuntime().addShutdownHook(this.shutdownHook);

getLogger().info("--- Loading data");
tokenService.loadTokens();

Expand All @@ -144,74 +139,66 @@ public void load() throws Exception {
getLogger().info("");

getLogger().info("--- Loading domain configurations");
for (ReposiliteConfiguration configuration : configurations()) {
configuration.configure(this);
}
Arrays.stream(configurations()).forEach(configuration -> configuration.configure(this));
}

public void start() throws Exception {
getLogger().info("Binding server at " + configuration.hostname + "::" + configuration.port);
public Reposilite start() throws Exception {
this.alive.set(true);
Thread.currentThread().setName("Reposilite | Main Thread");

CountDownLatch latch = new CountDownLatch(1);
getLogger().info("Binding server at " + configuration.hostname + "::" + configuration.port);
this.uptime = System.currentTimeMillis();

reactiveHttpServer.start(configuration, () -> {
getLogger().info("Done (" + TimeUtils.format(TimeUtils.getUptime(uptime)) + "s)!");

schedule(() -> {
console.defaultExecute("help");
try {
httpServer.start(configuration);
Runtime.getRuntime().addShutdownHook(shutdownHook);
} catch (Exception exception) {
getLogger().error("Failed to start Reposilite", exception);
shutdown();
return this;
}

getLogger().info("Collecting status metrics...");
console.defaultExecute("status");
getLogger().info("Done (" + TimeUtils.format(TimeUtils.getUptime(uptime)) + "s)!");
console.defaultExecute("help");

// disable console daemon in tests due to issues with coverage and interrupt method call
// https://github.com/jacoco/jacoco/issues/1066
if (!isTestEnvEnabled()) {
console.hook();
}
});
getLogger().info("Collecting status metrics...");
console.defaultExecute("status");

latch.countDown();
});
// disable console daemon in tests due to issues with coverage and interrupt method call
// https://github.com/jacoco/jacoco/issues/1066
if (!isTestEnvEnabled()) {
console.hook();
}

latch.await();
executor.await(() -> getLogger().info("Bye! Uptime: " + TimeUtils.format(TimeUtils.getUptime(uptime) / 60) + "min"));
return this;
}

public synchronized void forceShutdown() throws Exception {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
shutdown();
}

public synchronized void shutdown() throws Exception {
public synchronized void shutdown() {
if (!alive.get()) {
return;
}

this.alive.set(false);
getLogger().info("Shutting down " + configuration.hostname + "::" + configuration.port + " ...");

reactiveHttpServer.stop();
statsService.saveStats();
storageProvider.shutdown();
console.stop();
executor.stop();
}
Runtime.getRuntime().removeShutdownHook(shutdownHook);

public void schedule(ThrowingRunnable<?> runnable) {
executor.schedule(runnable);
getLogger().info("Shutting down " + configuration.hostname + "::" + configuration.port + " ...");
httpServer.stop();
}

public boolean isTestEnvEnabled() {
return testEnvEnabled;
}

public String getPrettyUptime() {
return TimeUtils.format(TimeUtils.getUptime(uptime) / 60) + "min";
}

public long getUptime() {
return System.currentTimeMillis() - uptime;
}

public ReposiliteHttpServer getHttpServer() {
return reactiveHttpServer;
return httpServer;
}

public FrontendProvider getFrontendService() {
Expand Down Expand Up @@ -274,10 +261,6 @@ public Console getConsole() {
return console;
}

public ReposiliteExecutor getExecutor() {
return executor;
}

public Path getWorkingDirectory() {
return workingDirectory;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.panda_lang.reposilite.repository.LookupController;
import org.panda_lang.reposilite.resource.FrontendHandler;
import org.panda_lang.reposilite.resource.WebJarsHandler;
import org.panda_lang.reposilite.utils.TimeUtils;
import org.panda_lang.utilities.commons.function.Option;

public final class ReposiliteHttpServer {
Expand All @@ -50,7 +51,7 @@ public final class ReposiliteHttpServer {
this.servlet = servlet;
}

void start(Configuration configuration, Runnable onStart) {
void start(Configuration configuration) {
DeployEndpoint deployEndpoint = new DeployEndpoint(reposilite.getContextFactory(), reposilite.getDeployService());

LookupController lookupController = new LookupController(
Expand All @@ -68,7 +69,6 @@ void start(Configuration configuration, Runnable onStart) {

CliController cliController = new CliController(
reposilite.getContextFactory(),
reposilite.getExecutor(),
reposilite.getAuthenticator(),
reposilite.getConsole());

Expand All @@ -88,14 +88,24 @@ void start(Configuration configuration, Runnable onStart) {
.after("/*", new PostAuthHandler())
.exception(Exception.class, new FailureHandler(reposilite.getFailureService()));

javalin.events(event -> {
event.serverStopping(() -> {
reposilite.getStorageProvider().shutdown();
reposilite.getStatsService().saveStats();
});
event.serverStopped(() -> {
Reposilite.getLogger().info("Bye! Uptime: " + reposilite.getPrettyUptime());
reposilite.getConsole().stop();
});
});

if (!servlet) {
javalin.start(configuration.hostname, configuration.port);
onStart.run();
}
}

void stop() {
getJavalin().peek(Javalin::stop);
Option<Javalin> stop() {
return getJavalin().map(Javalin::stop);
}

private Javalin create(Configuration configuration) {
Expand All @@ -104,7 +114,6 @@ private Javalin create(Configuration configuration) {
: Javalin.create(config -> configure(configuration, config));
}

@SuppressWarnings("deprecation")
private void configure(Configuration configuration, JavalinConfig config) {
Server server = new Server();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.panda_lang.reposilite;

final class ReposiliteRunner extends Thread {

@Override
public void run() {
super.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.panda_lang.reposilite.Reposilite;
import org.panda_lang.reposilite.ReposiliteContext;
import org.panda_lang.reposilite.ReposiliteContextFactory;
import org.panda_lang.reposilite.ReposiliteExecutor;
import org.panda_lang.reposilite.ReposiliteWriter;
import org.panda_lang.reposilite.auth.Authenticator;
import org.panda_lang.reposilite.auth.Session;
Expand All @@ -34,18 +33,15 @@ public final class CliController implements Consumer<WsConfig> {
private static final String AUTHORIZATION_PREFIX = "Authorization:";

private final ReposiliteContextFactory contextFactory;
private final ReposiliteExecutor reposiliteExecutor;
private final Authenticator authenticator;
private final Console console;

public CliController(
ReposiliteContextFactory contextFactory,
ReposiliteExecutor reposiliteExecutor,
Authenticator authenticator,
Console console) {

this.contextFactory = contextFactory;
this.reposiliteExecutor = reposiliteExecutor;
this.authenticator = authenticator;
this.console = console;
}
Expand Down Expand Up @@ -85,7 +81,7 @@ public void accept(WsConfig wsConfig) {

wsConfig.onMessage(messageContext -> {
Reposilite.getLogger().info("CLI | " + username + "> " + messageContext.message());
reposiliteExecutor.schedule(() -> console.defaultExecute(messageContext.message()));
console.defaultExecute(messageContext.message());
});

for (String message : ReposiliteWriter.getCache()) {
Expand Down
Loading

0 comments on commit a64816f

Please sign in to comment.