6464import org .slf4j .LoggerFactory ;
6565import org .springframework .context .ApplicationEventPublisher ;
6666import org .springframework .context .event .EventListener ;
67+ import org .springframework .core .env .Environment ;
6768import org .springframework .integration .leader .event .OnGrantedEvent ;
6869import org .springframework .integration .leader .event .OnRevokedEvent ;
6970import org .springframework .scheduling .annotation .Async ;
7071import org .springframework .scheduling .annotation .Scheduled ;
7172
73+ import javax .annotation .PostConstruct ;
74+ import javax .annotation .PreDestroy ;
7275import javax .inject .Inject ;
7376import java .time .Duration ;
7477import java .time .Instant ;
8285import java .util .concurrent .ExecutorService ;
8386import java .util .concurrent .TimeUnit ;
8487
88+ import static eu .openanalytics .containerproxy .service .ProxyService .PROPERTY_STOP_PROXIES_ON_SHUTDOWN ;
8589import 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