Skip to content
This repository has been archived by the owner on Aug 19, 2018. It is now read-only.

Commit

Permalink
Added cleaning resources
Browse files Browse the repository at this point in the history
  • Loading branch information
segabriel committed Aug 18, 2018
1 parent 77178bb commit a15a637
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ public Flux<DiscoveryEvent> listen() {

@Override
public Mono<Void> shutdown() {
sink.complete();
return Mono.fromFuture(cluster.shutdown());
return Mono.defer(() -> {
sink.complete();
return cluster != null ? Mono.fromFuture(cluster.shutdown()) : Mono.empty();
});
}

private void configure(DiscoveryConfig config) {
Expand Down
26 changes: 21 additions & 5 deletions services/src/main/java/io/scalecube/services/Microservices.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private Mono<Microservices> start() {
.then(Mono.defer(this::doInjection))
.then(Mono.defer(() -> startGateway(call)))
.then(Mono.just(this));
});
})
.onErrorResume(t -> shutdown().then(Mono.error(t)));
}

private Mono<GatewayBootstrap> startGateway(Call call) {
Expand Down Expand Up @@ -322,7 +323,12 @@ private Mono<GatewayBootstrap> start(
}

private Mono<Void> shutdown() {
return Flux.fromIterable(gatewayInstances.values()).flatMap(Gateway::stop).then();
return Mono.defer(
() ->
gatewayInstances != null && !gatewayInstances.isEmpty()
? Mono.when(
gatewayInstances.values().stream().map(Gateway::stop).toArray(Mono[]::new))
: Mono.empty());
}

private InetSocketAddress gatewayAddress(String name, Class<? extends Gateway> gatewayClass) {
Expand Down Expand Up @@ -381,8 +387,12 @@ public ServiceDiscovery discovery() {
}

public Mono<Void> shutdown() {
return Mono.when(
discovery.shutdown(), gatewayBootstrap.shutdown(), transportBootstrap.shutdown());
return Mono.defer(
() ->
Mono.when(
discovery != null ? discovery.shutdown() : Mono.empty(),
gatewayBootstrap != null ? gatewayBootstrap.shutdown() : Mono.empty(),
transportBootstrap != null ? transportBootstrap.shutdown() : Mono.empty()));
}

private static class ServiceTransportBootstrap {
Expand Down Expand Up @@ -425,7 +435,13 @@ private Mono<ServiceTransportBootstrap> start(ServiceMethodRegistry methodRegist
}

private Mono<Void> shutdown() {
return Mono.when(serverTransport.stop(), transport.shutdown(executorService));
return Mono.defer(
() ->
Mono.when(
serverTransport != null ? serverTransport.stop() : Mono.empty(),
transport != null && executorService != null
? transport.shutdown(executorService)
: Mono.empty()));
}

private ClientTransport clientTransport() {
Expand Down

0 comments on commit a15a637

Please sign in to comment.