Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api method 'stop' #5202

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import bisq.proto.grpc.SendBtcRequest;
import bisq.proto.grpc.SetTxFeeRatePreferenceRequest;
import bisq.proto.grpc.SetWalletPasswordRequest;
import bisq.proto.grpc.StopRequest;
import bisq.proto.grpc.TakeOfferRequest;
import bisq.proto.grpc.TxInfo;
import bisq.proto.grpc.UnlockWalletRequest;
Expand Down Expand Up @@ -193,6 +194,7 @@ public static void run(String[] args) {
var offersService = grpcStubs.offersService;
var paymentAccountsService = grpcStubs.paymentAccountsService;
var priceService = grpcStubs.priceService;
var shutdownService = grpcStubs.shutdownService;
var tradesService = grpcStubs.tradesService;
var versionService = grpcStubs.versionService;
var walletsService = grpcStubs.walletsService;
Expand Down Expand Up @@ -741,6 +743,16 @@ public static void run(String[] args) {
out.println(disputeAgentType + " registered");
return;
}
case stop: {
if (new SimpleMethodOptionParser(args).parse().isForHelp()) {
out.println(getMethodHelp(helpService, method));
return;
}
var request = StopRequest.newBuilder().build();
shutdownService.stop(request);
out.println("server shutdown signal received");
return;
}
default: {
throw new RuntimeException(format("unhandled method '%s'", method));
}
Expand Down Expand Up @@ -894,6 +906,8 @@ private static void printHelp(OptionParser parser, @SuppressWarnings("SameParame
"Encrypt wallet with password, or set new password on encrypted wallet");
stream.format(rowFormat, "", "[--new-wallet-password=<new-password>]", "");
stream.println();
stream.format(rowFormat, stop.name(), "", "Shut down the server");
stream.println();
stream.println("Method Help Usage: bisq-cli [options] <method> --help");
stream.println();
} catch (IOException ex) {
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main/java/bisq/cli/GrpcStubs.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bisq.proto.grpc.OffersGrpc;
import bisq.proto.grpc.PaymentAccountsGrpc;
import bisq.proto.grpc.PriceGrpc;
import bisq.proto.grpc.ShutdownServerGrpc;
import bisq.proto.grpc.TradesGrpc;
import bisq.proto.grpc.WalletsGrpc;

Expand All @@ -39,6 +40,7 @@ public class GrpcStubs {
public final OffersGrpc.OffersBlockingStub offersService;
public final PaymentAccountsGrpc.PaymentAccountsBlockingStub paymentAccountsService;
public final PriceGrpc.PriceBlockingStub priceService;
public final ShutdownServerGrpc.ShutdownServerBlockingStub shutdownService;
public final TradesGrpc.TradesBlockingStub tradesService;
public final WalletsGrpc.WalletsBlockingStub walletsService;

Expand All @@ -60,6 +62,7 @@ public GrpcStubs(String apiHost, int apiPort, String apiPassword) {
this.offersService = OffersGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.paymentAccountsService = PaymentAccountsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.priceService = PriceGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.shutdownService = ShutdownServerGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.tradesService = TradesGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.walletsService = WalletsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
}
Expand Down
3 changes: 2 additions & 1 deletion cli/src/main/java/bisq/cli/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ public enum Method {
takeoffer,
unlockwallet,
unsettxfeerate,
withdrawfunds
withdrawfunds,
stop
}
22 changes: 22 additions & 0 deletions core/src/main/resources/help/stop-help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
stop

NAME
----
stop - stop the server

SYNOPSIS
--------
stop

DESCRIPTION
-----------
Shutdown the RPC server.

OPTIONS
-------

EXAMPLES
--------
To shutdown the server:
$ ./bisq-cli --password=xyz --port=9998 stop

2 changes: 2 additions & 0 deletions daemon/src/main/java/bisq/daemon/grpc/GrpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public GrpcServer(CoreContext coreContext,
GrpcOffersService offersService,
GrpcPaymentAccountsService paymentAccountsService,
GrpcPriceService priceService,
GrpcShutdownService shutdownService,
GrpcVersionService versionService,
GrpcGetTradeStatisticsService tradeStatisticsService,
GrpcTradesService tradesService,
Expand All @@ -65,6 +66,7 @@ public GrpcServer(CoreContext coreContext,
.addService(interceptForward(offersService, offersService.interceptors()))
.addService(interceptForward(paymentAccountsService, paymentAccountsService.interceptors()))
.addService(interceptForward(priceService, priceService.interceptors()))
.addService(shutdownService)
.addService(interceptForward(tradeStatisticsService, tradeStatisticsService.interceptors()))
.addService(interceptForward(tradesService, tradesService.interceptors()))
.addService(interceptForward(versionService, versionService.interceptors()))
Expand Down
59 changes: 59 additions & 0 deletions daemon/src/main/java/bisq/daemon/grpc/GrpcShutdownService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.daemon.grpc;

import bisq.core.app.BisqHeadlessApp;

import bisq.common.UserThread;

import bisq.proto.grpc.ShutdownServerGrpc;
import bisq.proto.grpc.StopReply;
import bisq.proto.grpc.StopRequest;

import io.grpc.stub.StreamObserver;

import javax.inject.Inject;

import lombok.extern.slf4j.Slf4j;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

@Slf4j
class GrpcShutdownService extends ShutdownServerGrpc.ShutdownServerImplBase {

private final GrpcExceptionHandler exceptionHandler;

@Inject
public GrpcShutdownService(GrpcExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}

@Override
public void stop(StopRequest req,
StreamObserver<StopReply> responseObserver) {
try {
log.info("Shutdown request received.");
var reply = StopReply.newBuilder().build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
UserThread.runAfter(BisqHeadlessApp.getShutDownHandler(), 500, MILLISECONDS);
} catch (Throwable cause) {
exceptionHandler.handleException(cause, responseObserver);
}
}
}
15 changes: 15 additions & 0 deletions proto/src/main/proto/grpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ message GetTradeStatisticsReply {
repeated TradeStatistics3 TradeStatistics = 1;
}

///////////////////////////////////////////////////////////////////////////////////////////
// Shutdown
///////////////////////////////////////////////////////////////////////////////////////////

service ShutdownServer {
rpc Stop (StopRequest) returns (StopReply) {
}
}

message StopRequest {
}

message StopReply {
}

///////////////////////////////////////////////////////////////////////////////////////////
// Trades
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down