Skip to content

Commit

Permalink
UNOMI-863: set up OSGi DS to allow config update in the HeathCheck se…
Browse files Browse the repository at this point in the history
…rvice. (#708)
  • Loading branch information
dgriffon authored Nov 7, 2024
1 parent d6ee5bd commit 43bebe7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unomi-ci-build-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
unit-tests:
name: Execute unit tests
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Set up JDK 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public String get(String configKey) {
return this.config.get(configKey);
}

public int getSize() {
return this.config.size();
}

public boolean isEnabled() {
return enabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,28 @@ public class HealthCheckService {
@Reference
protected HttpService httpService;

@Reference(cardinality = ReferenceCardinality.MANDATORY, updated = "updated")
private HealthCheckConfig config;

public HealthCheckService() {
LOGGER.info("Building healthcheck service...");
}

public void setConfig(HealthCheckConfig config) {
this.config = config;
}

@Activate
public void activate() throws ServletException, NamespaceException {
if (config.isEnabled()) {
LOGGER.info("Activating healthcheck service...");
executor = Executors.newSingleThreadExecutor();
httpService.registerServlet("/health/check", new HealthCheckServlet(this), null,
new HealthCheckHttpContext(config.get(CONFIG_AUTH_REALM)));
this.registered = true;
} else {
LOGGER.info("Healthcheck service is disabled");
LOGGER.info("Activating healthcheck service...");
executor = Executors.newSingleThreadExecutor();
if (!registered) {
setConfig(config);
}
}

public void updated() throws ServletException, NamespaceException {
@Reference(service = HealthCheckConfig.class, policy = ReferencePolicy.DYNAMIC, updated = "setConfig")
private void setConfig(HealthCheckConfig config) throws ServletException, NamespaceException {
this.config = config;
if (httpService == null ) {
LOGGER.info("Healthcheck config with {} entrie(s) did not update the service as not fully started yet.", config.getSize());
return;
}
if (config.isEnabled()) {
LOGGER.info("Updating healthcheck service...");
if (registered) {
Expand All @@ -84,10 +81,16 @@ public void updated() throws ServletException, NamespaceException {
new HealthCheckHttpContext(config.get(CONFIG_AUTH_REALM)));
registered = true;
} else {
httpService.unregister("/health/check");
registered = false;
LOGGER.info("Healthcheck service is disabled");
}
}

private void unsetConfig(HealthCheckConfig config) {
this.config = null;
}

@Deactivate
public void deactivate() {
LOGGER.info("Deactivating healthcheck service...");
Expand All @@ -112,7 +115,7 @@ protected void unbind(HealthCheckProvider provider) {
}

public List<HealthCheckResponse> check() throws RejectedExecutionException {
if (config.isEnabled()) {
if (config !=null && config.isEnabled()) {
LOGGER.debug("Health check called");
if (busy) {
throw new RejectedExecutionException("Health check already in progress");
Expand Down

0 comments on commit 43bebe7

Please sign in to comment.