gmunozfe commented on code in PR #2087:
URL: 
https://github.com/apache/incubator-kie-kogito-apps/pull/2087#discussion_r1713762095


##########
jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/management/JobServiceLeaderLivenessHealthCheck.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.jobs.service.management;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
+import org.eclipse.microprofile.health.Liveness;
+
+import jakarta.annotation.PostConstruct;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
+
+@Liveness
+@ApplicationScoped
+public class JobServiceLeaderLivenessHealthCheck implements HealthCheck {

Review Comment:
   Perhaps you can add a `Logger` instance to log information such as 
initialization, status changes, and expiration checks, but up to you.



##########
jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/management/JobServiceLeaderLivenessHealthCheck.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.jobs.service.management;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.eclipse.microprofile.config.inject.ConfigProperty;
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
+import org.eclipse.microprofile.health.Liveness;
+
+import jakarta.annotation.PostConstruct;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
+
+@Liveness
+@ApplicationScoped
+public class JobServiceLeaderLivenessHealthCheck implements HealthCheck {
+
+    private final AtomicBoolean enabled = new AtomicBoolean(false);
+
+    private final AtomicLong startTime = new AtomicLong();
+
+    private static final String EXPIRATION_IN_SECONDS = 
"kogito.jobs-service.management.leader-check.expiration-in-seconds";
+
+    @ConfigProperty(name = EXPIRATION_IN_SECONDS, defaultValue = "-1")
+    long expirationInSeconds;
+
+    @PostConstruct
+    void init() {
+        startTime.set(getCurrentTimeMillis());
+    }
+
+    @Override
+    public HealthCheckResponse call() {
+        final HealthCheckResponseBuilder responseBuilder = 
HealthCheckResponse.named("Get Leader Instance Timeout");
+        if (hasExpired() && !enabled.get()) {
+            return responseBuilder.down().build();

Review Comment:
   A possible improvement could be to include some params here in the response 
for troubleshooting, but perhaps can be redundant, wdyt?
   
   ```suggestion
               return responseBuilder.down()
                       .withData("expirationInSeconds", expirationInSeconds)
                       .withData("timeSinceStartInMillis", 
getCurrentTimeMillis() - startTime.get())
                       .build();
   ```



##########
jobs-service/jobs-service-common/src/test/java/org/kie/kogito/jobs/service/management/JobServiceLeaderLivenessHealthCheckTest.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.jobs.service.management;
+
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+class JobServiceLeaderLivenessHealthCheckTest {
+
+    private static final long START_TIME = 1234;
+
+    private JobServiceLeaderLivenessHealthCheck healthCheck;
+
+    @BeforeEach
+    void setUp() {
+        healthCheck = spy(new JobServiceLeaderLivenessHealthCheck());
+        doReturn(START_TIME).when(healthCheck).getCurrentTimeMillis();
+        healthCheck.init();
+    }
+
+    @Test
+    void timeoutNotSet() {
+        doReturn(START_TIME + 1000 * 
50).when(healthCheck).getCurrentTimeMillis();
+        assertThat(healthCheck.call().getStatus())
+                .isNotNull()
+                .isEqualTo(HealthCheckResponse.Status.UP);
+    }
+
+    @Test
+    void timeoutSetButNotReached() {
+        healthCheck.expirationInSeconds = 60;
+        doReturn(START_TIME + 1000 * 
10).when(healthCheck).getCurrentTimeMillis();
+        assertThat(healthCheck.call().getStatus())
+                .isNotNull()
+                .isEqualTo(HealthCheckResponse.Status.UP);
+    }
+
+    @Test
+    void timeoutSetAndReached() {
+        healthCheck.expirationInSeconds = 60;
+        doReturn(START_TIME + 1000 * 60 + 
1).when(healthCheck).getCurrentTimeMillis();
+        assertThat(healthCheck.call().getStatus())
+                .isNotNull()
+                .isEqualTo(HealthCheckResponse.Status.DOWN);
+    }

Review Comment:
   We could add also a test for state change and complete coverage:
   
   ```suggestion
       }
       
       @Test
       void statusChanged() {
           healthCheck.onMessagingStatusChange(new MessagingChangeEvent(true));
           doReturn(START_TIME + 1000 * 
10).when(healthCheck).getCurrentTimeMillis();
           HealthCheckResponse response = healthCheck.call();
           assertThat(response.getStatus())
                   .isNotNull()
                   .isEqualTo(HealthCheckResponse.Status.UP);
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to