blended.mgmt.rest.internal.RemoteContainerStatePersistor.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blended.mgmt.rest_2.12 Show documentation
Show all versions of blended.mgmt.rest_2.12 Show documentation
REST interface to accept POST's from distributed containers. These will be delegated to the container registry.
The newest version!
package blended.mgmt.rest.internal
import blended.persistence.PersistenceService
import blended.updater.config.{Mapper, RemoteContainerState}
import blended.util.logging.Logger
import scala.collection.JavaConverters._
import scala.util.{Failure, Success}
class RemoteContainerStatePersistor(persistenceService : PersistenceService) {
import RemoteContainerStatePersistor._
private[this] val log = Logger[RemoteContainerStatePersistor]
def findAll() : List[RemoteContainerState] = {
val result = persistenceService.findAll(pClass)
log.debug(s"loaded ${result.size} entries from db")
result.toList.flatMap { s =>
Mapper.unmapRemoteContainerState(s) match {
case Success(s) => List(s)
case Failure(e) =>
log.warn(e)(s"Could not create RemoteContainerState from persisted map (ignoring this entry): ${s}")
List()
}
}
}
def updateState(state : RemoteContainerState) : Unit = {
log.debug(s"About to persist remote container state: ${state}")
val deleteCount = persistenceService.deleteByExample(pClass, Map(
"containerInfo" -> Map(
"containerId" -> state.containerInfo.containerId
).asJava
).asJava)
log.debug(s"deleted ${deleteCount} old entries")
val entry = persistenceService.persist(pClass, Mapper.mapRemoteContainerState(state))
log.debug(s"persisted 1 new entry: ${entry}")
}
}
object RemoteContainerStatePersistor {
val pClass = "RemoteContainerState"
}