Skip to content

Commit

Permalink
Merge pull request #55 from Peergos/fix/shutdown-hook
Browse files Browse the repository at this point in the history
add shutdown hook implicitly so client does not need to
  • Loading branch information
ianopolous authored Sep 15, 2023
2 parents 574baa4 + 463c153 commit c038007
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 16 additions & 3 deletions src/main/java/org/peergos/EmbeddedIpfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public List<HashedBlock> getBlocks(List<Want> wants, Set<PeerId> peers, boolean

public void start() {
LOG.info("Starting IPFS...");
Thread shutdownHook = new Thread(() -> {
LOG.info("Stopping Ipfs server...");
try {
this.stop().join();
} catch (Exception ex) {
ex.printStackTrace();
}
});
Runtime.getRuntime().addShutdownHook(shutdownHook);
node.start().join();
IdentifyBuilder.addIdentifyProtocol(node);
LOG.info("Node started and listening on " + node.listenAddresses());
Expand All @@ -93,9 +102,13 @@ public void start() {
}

public CompletableFuture<Void> stop() throws Exception {
records.close();
blockProvider.stop();
return node.stop();
if (records != null) {
records.close();
}
if (blockProvider != null) {
blockProvider.stop();
}
return node != null ? node.stop() : CompletableFuture.completedFuture(null);
}

public static Blockstore buildBlockStore(Config config, Path ipfsPath) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/peergos/Nabu.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.sun.net.httpserver.HttpServer;
import io.ipfs.multiaddr.MultiAddress;
import io.netty.handler.codec.http.*;
import org.peergos.client.*;
import org.peergos.config.*;
import org.peergos.net.APIHandler;
import org.peergos.net.HttpProxyHandler;
import org.peergos.protocol.dht.DatabaseRecordStore;
import org.peergos.protocol.http.*;
import org.peergos.util.HttpUtil;
import org.peergos.util.JSONParser;
import org.peergos.util.JsonHelper;
import org.peergos.util.Logging;
Expand Down Expand Up @@ -73,9 +70,8 @@ public Nabu(Args args) throws Exception {
apiServer.start();

Thread shutdownHook = new Thread(() -> {
LOG.info("Stopping server...");
LOG.info("Stopping API server...");
try {
ipfs.stop().join();
apiServer.stop(3); //wait max 3 seconds
} catch (Exception ex) {
ex.printStackTrace();
Expand Down

0 comments on commit c038007

Please sign in to comment.