Skip to content

Commit

Permalink
GH-74 Restore cache of latest exception in status (Resolve #74)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed May 24, 2020
1 parent 68dbdd6 commit 5cf3c26
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/panda_lang/reposilite/Reposilite.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public long getUptime() {
return System.currentTimeMillis() - uptime;
}

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

Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/panda_lang/reposilite/ReposiliteHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
import org.panda_lang.reposilite.repository.LookupController;
import org.panda_lang.reposilite.repository.LookupService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

public final class ReposiliteHttpServer {

private final Reposilite reposilite;
private final Collection<Exception> exceptions;
private Javalin javalin;

ReposiliteHttpServer(Reposilite reposilite) {
this.reposilite = reposilite;
this.exceptions = new ArrayList<>();
}

void start(Configuration configuration, Runnable onStart) {
Expand All @@ -28,6 +34,10 @@ void start(Configuration configuration, Runnable onStart) {
.get("/*", lookupController)
.head("/*", lookupController)
.put("/*", new DeployController(reposilite))
.exception(Exception.class, (exception, ctx) -> {
exception.printStackTrace();
exceptions.add(exception);
})
.start(configuration.getHostname(), configuration.getPort());

onStart.run();
Expand All @@ -38,12 +48,16 @@ private void config(JavalinConfig config) {
config.showJavalinBanner = false;
}

void stop() {
javalin.stop();
}

public boolean isAlive() {
return true;
return Objects.requireNonNull(javalin.server()).server().isRunning();
}

void stop() {
javalin.stop();
public Collection<Exception> getExceptions() {
return exceptions;
}

}
20 changes: 17 additions & 3 deletions src/main/java/org/panda_lang/reposilite/console/StatusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,36 @@
import org.panda_lang.reposilite.utils.TimeUtils;
import org.panda_lang.utilities.commons.console.Effect;

import java.util.Collection;

final class StatusCommand implements NanoCommand {

@Override
public boolean call(Reposilite reposilite) {
Reposilite.getLogger().info("");
Reposilite.getLogger().info("Reposilite " + ReposiliteConstants.VERSION + " Status");
Reposilite.getLogger().info(" Active: " + Effect.GREEN_BOLD + reposilite.getReactiveHttpServer().isAlive() + Effect.RESET);
Reposilite.getLogger().info(" Active: " + Effect.GREEN_BOLD + reposilite.getHttpServer().isAlive() + Effect.RESET);
Reposilite.getLogger().info(" Uptime: " + TimeUtils.format(reposilite.getUptime() / 1000.0 / 60.0) + "min");
Reposilite.getLogger().info(" Memory usage of process: " + getMemoryUsage());
Reposilite.getLogger().info(" Cached elements: " + reposilite.getMetadataService().getCacheSize());
// reposilite.getHttpServer().getLatestError().peek(throwable -> Reposilite.getLogger().error(" Latest exception", throwable));
printExceptions(reposilite.getHttpServer().getExceptions());
Reposilite.getLogger().info("");

return true;
}

private void printExceptions(Collection<Exception> exceptions) {
if (exceptions.isEmpty()) {
return;
}

Reposilite.getLogger().info("List of cached exceptions:");
int count = 0;

for (Exception throwable : exceptions) {
Reposilite.getLogger().error("Exception " + (++count), throwable);
}
}

private String getMemoryUsage() {
return TimeUtils.format((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024.0D / 1024.0D) + "M";
}
Expand Down

0 comments on commit 5cf3c26

Please sign in to comment.