@ -29,7 +29,11 @@ import org.hyperledger.besu.nat.NatService;
import java.io.File ;
import java.io.File ;
import java.io.FileOutputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.nio.charset.StandardCharsets ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Path ;
import java.nio.file.StandardOpenOption ;
import java.util.Optional ;
import java.util.Optional ;
import java.util.Properties ;
import java.util.Properties ;
import java.util.concurrent.CompletableFuture ;
import java.util.concurrent.CompletableFuture ;
@ -53,6 +57,7 @@ public class Runner implements AutoCloseable {
private final NetworkRunner networkRunner ;
private final NetworkRunner networkRunner ;
private final NatService natService ;
private final NatService natService ;
private final Optional < Path > pidPath ;
private final Optional < JsonRpcHttpService > jsonRpc ;
private final Optional < JsonRpcHttpService > jsonRpc ;
private final Optional < GraphQLHttpService > graphQLHttp ;
private final Optional < GraphQLHttpService > graphQLHttp ;
private final Optional < WebSocketService > websocketRpc ;
private final Optional < WebSocketService > websocketRpc ;
@ -75,12 +80,14 @@ public class Runner implements AutoCloseable {
final Optional < MetricsService > metrics ,
final Optional < MetricsService > metrics ,
final BesuController < ? > besuController ,
final BesuController < ? > besuController ,
final Path dataDir ,
final Path dataDir ,
final Optional < Path > pidPath ,
final Optional < TransactionLogBloomCacher > transactionLogBloomCacher ,
final Optional < TransactionLogBloomCacher > transactionLogBloomCacher ,
final Blockchain blockchain ) {
final Blockchain blockchain ) {
this . vertx = vertx ;
this . vertx = vertx ;
this . networkRunner = networkRunner ;
this . networkRunner = networkRunner ;
this . natService = natService ;
this . natService = natService ;
this . graphQLHttp = graphQLHttp ;
this . graphQLHttp = graphQLHttp ;
this . pidPath = pidPath ;
this . jsonRpc = jsonRpc ;
this . jsonRpc = jsonRpc ;
this . websocketRpc = websocketRpc ;
this . websocketRpc = websocketRpc ;
this . metrics = metrics ;
this . metrics = metrics ;
@ -114,6 +121,7 @@ public class Runner implements AutoCloseable {
writeBesuPortsToFile ( ) ;
writeBesuPortsToFile ( ) ;
writeBesuNetworksToFile ( ) ;
writeBesuNetworksToFile ( ) ;
autoTransactionLogBloomCachingService . ifPresent ( AutoTransactionLogBloomCachingService : : start ) ;
autoTransactionLogBloomCachingService . ifPresent ( AutoTransactionLogBloomCachingService : : start ) ;
writePidFile ( ) ;
} catch ( final Exception ex ) {
} catch ( final Exception ex ) {
LOG . error ( "Startup failed" , ex ) ;
LOG . error ( "Startup failed" , ex ) ;
throw new IllegalStateException ( ex ) ;
throw new IllegalStateException ( ex ) ;
@ -256,6 +264,28 @@ public class Runner implements AutoCloseable {
"This file contains the IP Addresses (global and local) used by the running instance of Besu" ) ;
"This file contains the IP Addresses (global and local) used by the running instance of Besu" ) ;
}
}
private void writePidFile ( ) {
pidPath . ifPresent (
path - > {
String pid = "" ;
try {
pid = Long . toString ( ProcessHandle . current ( ) . pid ( ) ) ;
} catch ( Throwable t ) {
}
try {
Files . write (
path ,
pid . getBytes ( StandardCharsets . UTF_8 ) ,
StandardOpenOption . CREATE ,
StandardOpenOption . TRUNCATE_EXISTING ,
StandardOpenOption . WRITE ) ;
path . toFile ( ) . deleteOnExit ( ) ;
} catch ( IOException e ) {
LOG . error ( "Error writing PID file" , e ) ;
}
} ) ;
}
public Optional < Integer > getJsonRpcPort ( ) {
public Optional < Integer > getJsonRpcPort ( ) {
return jsonRpc . map ( service - > service . socketAddress ( ) . getPort ( ) ) ;
return jsonRpc . map ( service - > service . socketAddress ( ) . getPort ( ) ) ;
}
}