Skip to content

Commit 7faa207

Browse files
committed
Fix #32638: stop DelegateProxies on shutdown
1 parent 1ea9c73 commit 7faa207

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/backend/dispatcher/ProxyDispatcherService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
import org.springframework.stereotype.Service;
3232

3333
import javax.annotation.PostConstruct;
34+
import javax.annotation.PreDestroy;
35+
import java.util.ArrayList;
3436
import java.util.HashMap;
37+
import java.util.List;
3538
import java.util.Map;
3639

3740
@Service
@@ -42,6 +45,7 @@ public class ProxyDispatcherService {
4245
private final IProxySharingStoreFactory storeFactory;
4346
private final ConfigurableListableBeanFactory beanFactory;
4447
private final DefaultProxyDispatcher defaultProxyDispatcher;
48+
private final List<AutoCloseable> closeables = new ArrayList<>();
4549

4650
public ProxyDispatcherService(IProxySpecProvider proxySpecProvider,
4751
IProxySharingStoreFactory storeFactory,
@@ -62,6 +66,7 @@ public void init() {
6266

6367
ProxySharingScaler proxySharingScaler = createProxySharingScaler(seatStore, proxySpec, delegateProxyStore);
6468
createBean(proxySharingScaler, "proxySharingScaler_" + proxySpec.getId());
69+
closeables.add(proxySharingScaler);
6570

6671
ProxySharingDispatcher proxySharingDispatcher = new ProxySharingDispatcher(proxySpec, delegateProxyStore, seatStore);
6772
createBean(proxySharingDispatcher, "proxySharingDispatcher_" + proxySpec.getId());
@@ -87,4 +92,12 @@ private <T> void createBean(T bean, String beanName) {
8792
beanFactory.registerSingleton(beanName, initializedBean);
8893
}
8994

95+
@PreDestroy
96+
public void shutdown() throws Exception {
97+
// when using beanFactory.registerSingleton, @PreDestroy is not called
98+
for (AutoCloseable closeable : closeables) {
99+
closeable.close();
100+
}
101+
}
102+
90103
}

src/main/java/eu/openanalytics/containerproxy/backend/dispatcher/proxysharing/ProxySharingScaler.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,14 @@
6464
import org.slf4j.LoggerFactory;
6565
import org.springframework.context.ApplicationEventPublisher;
6666
import org.springframework.context.event.EventListener;
67+
import org.springframework.core.env.Environment;
6768
import org.springframework.integration.leader.event.OnGrantedEvent;
6869
import org.springframework.integration.leader.event.OnRevokedEvent;
6970
import org.springframework.scheduling.annotation.Async;
7071
import org.springframework.scheduling.annotation.Scheduled;
7172

73+
import javax.annotation.PostConstruct;
74+
import javax.annotation.PreDestroy;
7275
import javax.inject.Inject;
7376
import java.time.Duration;
7477
import java.time.Instant;
@@ -82,9 +85,10 @@
8285
import java.util.concurrent.ExecutorService;
8386
import java.util.concurrent.TimeUnit;
8487

88+
import static eu.openanalytics.containerproxy.service.ProxyService.PROPERTY_STOP_PROXIES_ON_SHUTDOWN;
8589
import static net.logstash.logback.argument.StructuredArguments.kv;
8690

87-
public class ProxySharingScaler {
91+
public class ProxySharingScaler implements AutoCloseable {
8892

8993
protected static String publicPathPrefix = "/api/route/";
9094
protected final ExecutorService executor = ExecutorServiceFactory.create("ProxySharingScaler");
@@ -95,6 +99,7 @@ public class ProxySharingScaler {
9599
private final Logger logger = LoggerFactory.getLogger(getClass());
96100
private final ProxySpec proxySpec;
97101
private final String proxySpecHash;
102+
private boolean stopAppsOnShutdown;
98103
protected ReconcileStatus lastReconcileStatus = ReconcileStatus.Stable;
99104
private Instant lastScaleUp = null;
100105

@@ -116,6 +121,8 @@ public class ProxySharingScaler {
116121
private ILeaderService leaderService;
117122
@Inject
118123
private ApplicationEventPublisher applicationEventPublisher;
124+
@Inject
125+
private Environment environment;
119126

120127
public ProxySharingScaler(ISeatStore seatStore, ProxySpec proxySpec, IDelegateProxyStore delegateProxyStore) {
121128
this.specExtension = proxySpec.getSpecExtension(ProxySharingSpecExtension.class);
@@ -130,6 +137,11 @@ public ProxySharingScaler(ISeatStore seatStore, ProxySpec proxySpec, IDelegatePr
130137
}
131138
}
132139

140+
@PostConstruct
141+
public void init() {
142+
stopAppsOnShutdown = environment.getProperty(PROPERTY_STOP_PROXIES_ON_SHUTDOWN, Boolean.class, true);
143+
}
144+
133145
public static void setPublicPathPrefix(String publicPathPrefix) {
134146
ProxySharingScaler.publicPathPrefix = publicPathPrefix;
135147
}
@@ -201,7 +213,7 @@ public void onRemoveDelegateProxiesEvent(RemoveDelegateProxiesEvent event) {
201213
if ((event.getSpecId() != null && !Objects.equals(event.getSpecId(), proxySpec.getId()))
202214
|| !leaderService.isLeader()
203215
|| (event.getId() != null && delegateProxyStore.getDelegateProxy(event.getId()) == null)
204-
) {
216+
) {
205217
// only handle events for this spec
206218
return;
207219
}
@@ -631,6 +643,14 @@ public void stopAll() {
631643
}
632644
}
633645

646+
@Override
647+
public void close() throws Exception {
648+
if (!stopAppsOnShutdown) {
649+
return;
650+
}
651+
stopAll();
652+
}
653+
634654
private String getPublicPath(String targetId) {
635655
return publicPathPrefix + targetId + "/";
636656
}

0 commit comments

Comments
 (0)