Skip to content

Commit

Permalink
feat: add automatic closure of operator on shut down
Browse files Browse the repository at this point in the history
Fixes #97
  • Loading branch information
metacosm committed Aug 20, 2021
1 parent 71040bf commit e0ae95f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ ConfigurationServiceBuildItem createConfigurationServiceAndOperator(

additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(OperatorProducer.class));

// if the app doesn't provide a main class, add the StartupListener
// if the app doesn't provide a main class, add the AppEventListener
if (index.getAllKnownImplementors(DotName.createSimple(QuarkusApplication.class.getName())).isEmpty()) {
additionalBeans.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(StartupListener.class).setDefaultScope(DotName.createSimple(Singleton.class.getName()))
.addBeanClass(AppEventListener.class).setDefaultScope(DotName.createSimple(Singleton.class.getName()))
.setUnremovable()
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.slf4j.LoggerFactory;

import io.javaoperatorsdk.operator.Operator;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;

public class StartupListener {
private static final Logger log = LoggerFactory.getLogger(StartupListener.class);
public class AppEventListener {
private static final Logger log = LoggerFactory.getLogger(AppEventListener.class);
private final Operator operator;
private final QuarkusConfigurationService configurationService;

public StartupListener(Operator operator, QuarkusConfigurationService configurationService) {
public AppEventListener(Operator operator, QuarkusConfigurationService configurationService) {
this.operator = operator;
this.configurationService = configurationService;
}
Expand All @@ -29,4 +30,9 @@ public void onStartup(@Observes StartupEvent event) {
}
operator.start();
}

public void onShutdown(@Observes ShutdownEvent event) {
log.info("Quarkus Java Operator SDK extension is shutting down");
operator.close();
}
}

0 comments on commit e0ae95f

Please sign in to comment.