-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2bf70c
commit 867dacd
Showing
8 changed files
with
220 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
qbit/core/src/main/java/io/advantageous/qbit/service/health/ServiceHealthManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.advantageous.qbit.service.health; | ||
|
||
/** | ||
* Allows service to notify health system of its status by accessing the low level ServiceQueue health. | ||
*/ | ||
public interface ServiceHealthManager { | ||
|
||
/** | ||
* Checks to see if service is failing. | ||
* | ||
* @return true if failing | ||
*/ | ||
boolean isFailing(); | ||
|
||
/** | ||
* Check to see if the service is ok. | ||
* | ||
* @return true if ok | ||
*/ | ||
boolean isOk(); | ||
|
||
/** | ||
* Mark the service as failing. | ||
*/ | ||
void setFailing(); | ||
|
||
/** | ||
* Mark the service as recovered. | ||
*/ | ||
void recover(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
qbit/core/src/main/java/io/advantageous/qbit/service/impl/ServiceHealthManagerDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.advantageous.qbit.service.impl; | ||
|
||
import io.advantageous.qbit.service.ServiceContext; | ||
import io.advantageous.qbit.service.ServiceQueue; | ||
import io.advantageous.qbit.service.health.ServiceHealthManager; | ||
import io.advantageous.reakt.Expected; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static io.advantageous.reakt.Expected.empty; | ||
import static io.advantageous.reakt.Expected.ofNullable; | ||
|
||
public class ServiceHealthManagerDefault implements ServiceHealthManager { | ||
|
||
|
||
final Logger logger = LoggerFactory.getLogger(ServiceHealthManagerDefault.class); | ||
private final Expected<Runnable> failCallback; | ||
private final Expected<Runnable> recoverCallback; | ||
private Expected<ServiceQueue> serviceQueue = empty(); | ||
private Expected<ServiceContext> serviceContext = empty(); | ||
|
||
public ServiceHealthManagerDefault(final Runnable failCallback, | ||
final Runnable recoverCallback) { | ||
this.failCallback = Expected.ofNullable(failCallback); | ||
this.recoverCallback = Expected.ofNullable(recoverCallback); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isFailing() { | ||
|
||
serviceQueue.ifEmpty(this::loadIfEmpty); | ||
|
||
if (serviceQueue.isPresent()) { | ||
return serviceQueue.get().failing(); | ||
} else { | ||
logger.warn("Service Queue was not found, but isFailing() was called on ServiceHealthManager"); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
private void loadIfEmpty() { | ||
serviceContext.ifEmpty(() -> { | ||
serviceContext = ofNullable(ServiceContext.serviceContext()); | ||
serviceContext.ifPresent(serviceContext1 -> serviceQueue = | ||
ofNullable(serviceContext1.currentService())); | ||
|
||
}); | ||
} | ||
|
||
@Override | ||
public boolean isOk() { | ||
|
||
serviceQueue.ifEmpty(this::loadIfEmpty); | ||
|
||
if (serviceQueue.isPresent()) { | ||
return !serviceQueue.get().failing(); | ||
} else { | ||
logger.warn("Service Queue was not found, but isOk() was called on ServiceHealthManager"); | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public void setFailing() { | ||
|
||
serviceQueue.ifEmpty(this::loadIfEmpty); | ||
|
||
|
||
if (serviceQueue.isPresent()) { | ||
serviceQueue.get().setFailing(); | ||
} else { | ||
logger.warn("Service Queue was not found, but setFailing() was called on ServiceHealthManager"); | ||
} | ||
failCallback.ifPresent(Runnable::run); | ||
} | ||
|
||
@Override | ||
public void recover() { | ||
|
||
serviceQueue.ifEmpty(this::loadIfEmpty); | ||
|
||
if (serviceQueue.isPresent()) { | ||
serviceQueue.get().recover(); | ||
} else { | ||
logger.warn("Service Queue was not found, but recover() was called on ServiceHealthManager"); | ||
} | ||
recoverCallback.ifPresent(Runnable::run); | ||
|
||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...core/src/test/java/io/advantageous/qbit/service/impl/ServiceHealthManagerDefaultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.advantageous.qbit.service.impl; | ||
|
||
import io.advantageous.qbit.service.ServiceBuilder; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class ServiceHealthManagerDefaultTest { | ||
|
||
|
||
@Test | ||
public void testIsFailing() throws Exception { | ||
|
||
BaseServiceQueueImpl.serviceThreadLocal.set(ServiceBuilder.serviceBuilder().setServiceObject(new Object()).build()); | ||
|
||
ServiceHealthManagerDefault serviceHealthManagerDefault = new ServiceHealthManagerDefault(null, null); | ||
|
||
serviceHealthManagerDefault.setFailing(); | ||
assertTrue(serviceHealthManagerDefault.isFailing()); | ||
serviceHealthManagerDefault.recover(); | ||
assertFalse(serviceHealthManagerDefault.isFailing()); | ||
|
||
|
||
serviceHealthManagerDefault = new ServiceHealthManagerDefault(null, null); | ||
BaseServiceQueueImpl.serviceThreadLocal.set(null); | ||
serviceHealthManagerDefault.setFailing(); | ||
assertFalse(serviceHealthManagerDefault.isFailing()); | ||
|
||
} | ||
|
||
@Test | ||
public void testIsOk() throws Exception { | ||
BaseServiceQueueImpl.serviceThreadLocal.set(ServiceBuilder.serviceBuilder().setServiceObject(new Object()).build()); | ||
|
||
ServiceHealthManagerDefault serviceHealthManagerDefault = new ServiceHealthManagerDefault(null, null); | ||
|
||
serviceHealthManagerDefault.recover(); | ||
assertTrue(serviceHealthManagerDefault.isOk()); | ||
serviceHealthManagerDefault.setFailing(); | ||
assertFalse(serviceHealthManagerDefault.isOk()); | ||
|
||
|
||
serviceHealthManagerDefault = new ServiceHealthManagerDefault(null, null); | ||
BaseServiceQueueImpl.serviceThreadLocal.set(null); | ||
serviceHealthManagerDefault.recover(); | ||
assertTrue(serviceHealthManagerDefault.isOk()); | ||
|
||
} | ||
|
||
} |