Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
chore(deps): bump ZIO version (DEV-893) (#2056)
  • Loading branch information
subotic committed May 7, 2022
1 parent 2a695ec commit 933f91e
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 78 deletions.
8 changes: 7 additions & 1 deletion build.sbt
Expand Up @@ -133,7 +133,13 @@ lazy val webapi: Project = Project(id = "webapi", base = file("webapi"))
Test / exportJars := true
)
.settings(
scalacOptions ++= Seq("-feature", "-unchecked", "-deprecation", "-Yresolve-term-conflict:package"),
scalacOptions ++= Seq(
"-feature",
"-unchecked",
"-deprecation",
"-Yresolve-term-conflict:package",
"-Ymacro-annotations"
),
logLevel := Level.Info,
run / javaOptions := webapiJavaRunOptions,
javaAgents += Dependencies.aspectjweaver,
Expand Down
11 changes: 7 additions & 4 deletions project/Dependencies.scala
Expand Up @@ -20,17 +20,18 @@ object Dependencies {
val jenaVersion = "4.4.0"
val metricsVersion = "4.0.1"
val scalaVersion = "2.13.8"
val ZioVersion = "2.0.0-RC5"
val ZioVersion = "2.0.0-RC6"
val ZioHttpVersion = "2.0.0-RC4"
val ZioJsonVersion = "0.3.0-RC3"
val ZioConfigVersion = "3.0.0-RC8"
val ZioConfigVersion = "3.0.0-RC9"
val ZioSchemaVersion = "0.2.0-RC5"
val ZioLoggingVersion = "2.0.0-RC8"
val ZioLoggingVersion = "2.0.0-RC9"
val ZioZmxVersion = "2.0.0-RC4"
val ZioPreludeVersion = "1.0.0-RC13"

// ZIO - all Scala 3 compatible
val zio = "dev.zio" %% "zio" % ZioVersion
val zioMacros = "dev.zio" %% "zio-macros" % ZioVersion
val zioHttp = "io.d11" %% "zhttp" % ZioHttpVersion
val zioJson = "dev.zio" %% "zio-json" % ZioJsonVersion
val zioPrelude = "dev.zio" %% "zio-prelude" % ZioPreludeVersion
Expand Down Expand Up @@ -145,13 +146,15 @@ object Dependencies {
zioHttp,
zioJson,
zioLogging,
zioMacros,
zioPrelude,
zioTest % Test,
zioTestSbt % Test
)

val dspApiMainLibraryDependencies = Seq(
zio
zio,
zioMacros
)

val schemaApiLibraryDependencies = Seq(
Expand Down
8 changes: 6 additions & 2 deletions webapi/src/main/scala/org/knora/webapi/app/Main.scala
Expand Up @@ -25,8 +25,12 @@ import java.util.concurrent.TimeUnit
*/
object Main extends scala.App with LiveCore {

// The ZIO runtime used to run functional effects
val runtime = Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.fromInfo)
/**
* Unsafely creates a `Runtime` from a `ZLayer` whose resources will be
* allocated immediately, and not released until the `Runtime` is shut down or
* the end of the application.
*/
val runtime = Runtime.unsafeFromLayer(Logging.fromInfo)

// The effect for building a cache service manager, a IIIF service manager, and AppConfig.
val managers = for {
Expand Down
6 changes: 3 additions & 3 deletions webapi/src/main/scala/org/knora/webapi/core/Logging.scala
@@ -1,9 +1,9 @@
package org.knora.webapi.core

import zio.LogLevel
import zio.RuntimeConfigAspect
import zio.logging.LogFormat._
import zio.logging._
import zio.ZLayer

object Logging {
val logFormat = "[correlation-id = %s] %s"
Expand All @@ -12,14 +12,14 @@ object Logging {
val textFormat: LogFormat =
timestamp.fixed(32).color(LogColor.BLUE) |-| level.highlight.fixed(14) |-| line.highlight

val fromDebug: RuntimeConfigAspect = {
val fromDebug: ZLayer[Any, Nothing, Unit] = {
console(
logLevel = LogLevel.Debug,
format = textFormat
)
}

val fromInfo: RuntimeConfigAspect = {
val fromInfo: ZLayer[Any, Nothing, Unit] = {
console(
logLevel = LogLevel.Info,
format = textFormat
Expand Down
Expand Up @@ -11,10 +11,12 @@ import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.admin.responder.usersmessages.UserIdentifierADM
import org.knora.webapi.messages.store.cacheservicemessages.CacheServiceStatusResponse
import zio._
import zio.macros.accessible

/**
* Cache Service Interface
*/
@accessible
trait CacheService {
def putUserADM(value: UserADM): Task[Unit]
def getUserADM(identifier: UserIdentifierADM): Task[Option[UserADM]]
Expand Down Expand Up @@ -46,4 +48,3 @@ trait CacheService {
* } yield ()
* }}}
*/
object CacheService extends Accessible[CacheService]
Expand Up @@ -157,8 +157,8 @@ case class CacheServiceInMemImpl(
val emptyValueError = EmptyValue("The string value is empty. Aborting write to cache.")

(for {
key <- if (key.isEmpty()) Task.fail(emptyKeyError) else Task.succeed(key)
value <- if (value.isEmpty()) Task.fail(emptyValueError) else Task.succeed(value)
key <- if (key.isEmpty()) ZIO.fail(emptyKeyError) else ZIO.succeed(key)
value <- if (value.isEmpty()) ZIO.fail(emptyValueError) else ZIO.succeed(value)
_ <- lut.put(key, value).commit
} yield ()).tap(_ => ZIO.logDebug(s"Wrote key: $key with value: $value to cache."))
}
Expand Down Expand Up @@ -196,7 +196,7 @@ case class CacheServiceInMemImpl(
* Pings the in-memory cache to see if it is available.
*/
def ping(): Task[CacheServiceStatusResponse] =
Task.succeed(CacheServiceStatusOK)
ZIO.succeed(CacheServiceStatusOK)
}

/**
Expand Down
Expand Up @@ -147,7 +147,7 @@ case class CacheServiceRedisImpl(pool: JedisPool) extends CacheService {
* @param key the key.
* @param value the value.
*/
def putStringValue(key: String, value: String): Task[Unit] = Task.attempt {
def putStringValue(key: String, value: String): Task[Unit] = ZIO.attempt {

if (key.isEmpty)
throw EmptyKey("The key under which the value should be written is empty. Aborting writing to redis.")
Expand Down Expand Up @@ -176,18 +176,18 @@ case class CacheServiceRedisImpl(pool: JedisPool) extends CacheService {
conn <- ZIO.attempt(pool.getResource)
value <- ZIO.attemptBlocking(conn.get(key))
res <-
if (value == "nil".getBytes) Task.succeed(None)
else Task.succeed(Some(value))
if (value == "nil".getBytes) ZIO.succeed(None)
else ZIO.succeed(Some(value))
_ = conn.close()
} yield res
}.catchAll(ex => ZIO.logError(s"Reading string from Redis failed: ${ex.getMessage}") *> Task.succeed(None))
}.catchAll(ex => ZIO.logError(s"Reading string from Redis failed: ${ex.getMessage}") *> ZIO.succeed(None))

/**
* Removes values for the provided keys. Any invalid keys are ignored.
*
* @param keys the keys.
*/
def removeValues(keys: Set[String]): Task[Unit] = Task.attemptBlocking {
def removeValues(keys: Set[String]): Task[Unit] = ZIO.attemptBlocking {

// del takes a vararg so I nee to convert the set to a swq and then to vararg
val conn: Jedis = pool.getResource
Expand All @@ -206,7 +206,7 @@ case class CacheServiceRedisImpl(pool: JedisPool) extends CacheService {
* @param key the key.
* @param value the value.
*/
private def putBytesValue(key: String, value: Array[Byte]): Task[Unit] = Task.attemptBlocking {
private def putBytesValue(key: String, value: Array[Byte]): Task[Unit] = ZIO.attemptBlocking {

if (key.isEmpty)
throw EmptyKey("The key under which the value should be written is empty. Aborting writing to redis.")
Expand Down Expand Up @@ -236,15 +236,15 @@ case class CacheServiceRedisImpl(pool: JedisPool) extends CacheService {
conn <- ZIO.attempt(pool.getResource).onError(ZIO.logErrorCause(_)).orDie
value <- ZIO.attemptBlocking(conn.get(key.getBytes))
res <-
if (value == "nil".getBytes) Task.succeed(None)
else Task.succeed(Some(value))
if (value == "nil".getBytes) ZIO.succeed(None)
else ZIO.succeed(Some(value))
_ = conn.close()
} yield res

/**
* Flushes (removes) all stored content from the Redis store.
*/
def flushDB(requestingUser: UserADM): Task[Unit] = Task.attemptBlocking {
def flushDB(requestingUser: UserADM): Task[Unit] = ZIO.attemptBlocking {

if (!requestingUser.isSystemUser) {
throw ForbiddenException("Only the system user is allowed to perform this operation.")
Expand All @@ -265,7 +265,7 @@ case class CacheServiceRedisImpl(pool: JedisPool) extends CacheService {
/**
* Pings the Redis store to see if it is available.
*/
def ping(): Task[CacheServiceStatusResponse] = Task.attemptBlocking {
def ping(): Task[CacheServiceStatusResponse] = ZIO.attemptBlocking {

val conn: Jedis = pool.getResource
try {
Expand All @@ -274,7 +274,7 @@ case class CacheServiceRedisImpl(pool: JedisPool) extends CacheService {
} finally {
conn.close()
}
}.catchAll(ex => ZIO.logError(s"Ping failed: ${ex.getMessage}") *> Task.succeed(CacheServiceStatusNOK))
}.catchAll(ex => ZIO.logError(s"Ping failed: ${ex.getMessage}") *> ZIO.succeed(CacheServiceStatusNOK))
}

object CacheServiceRedisImpl {
Expand Down
Expand Up @@ -9,7 +9,9 @@ import org.knora.webapi.messages.store.sipimessages.SipiGetTextFileRequest
import org.knora.webapi.messages.store.sipimessages.SipiGetTextFileResponse
import org.knora.webapi.messages.v2.responder.SuccessResponseV2
import zio._
import zio.macros.accessible

@accessible
trait IIIFService {

/**
Expand Down Expand Up @@ -50,5 +52,3 @@ trait IIIFService {
*/
def getStatus(): Task[IIIFServiceStatusResponse]
}

object IIIFService extends Accessible[IIIFService]
9 changes: 8 additions & 1 deletion webapi/src/main/scala/org/knora/webapi/util/ActorUtil.scala
Expand Up @@ -25,6 +25,13 @@ import scala.util.Try

object ActorUtil {

/**
* Unsafely creates a `Runtime` from a `ZLayer` whose resources will be
* allocated immediately, and not released until the `Runtime` is shut down or
* the end of the application.
*/
private val runtime = Runtime.unsafeFromLayer(Logging.fromInfo)

/**
* Transforms ZIO Task returned to the receive method of an actor to a message. Used mainly during the refactoring
* phase, to be able to return ZIO inside an Actor.
Expand All @@ -37,7 +44,7 @@ object ActorUtil {
* Since this is the "edge" of the ZIO world for now, we need to log all errors that ZIO has potentially accumulated
*/
def zio2Message[A](sender: ActorRef, zioTask: zio.Task[A], log: LoggingAdapter, appConfig: AppConfig): Unit =
Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.fromInfo)
runtime
.unsafeRunTask(
zioTask.foldCauseZIO(cause => handleCause(cause, sender), success => ZIO.succeed(sender ! success))
)
Expand Down
3 changes: 1 addition & 2 deletions webapi/src/test/scala/org/knora/webapi/AsyncCoreSpec.scala
Expand Up @@ -52,7 +52,6 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpecLike
import zio.&
import zio.Runtime
import zio.RuntimeConfig
import zio.ZEnvironment
import zio.ZIO
import zio.ZLayer
Expand Down Expand Up @@ -117,7 +116,7 @@ abstract class AsyncCoreSpec(_system: ActorSystem)
val log: LoggingAdapter = akka.event.Logging(system, this.getClass)

// The ZIO runtime used to run functional effects
val runtime = Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.fromInfo)
val runtime = Runtime.unsafeFromLayer(Logging.fromInfo)

// The effect for building a cache service manager and a IIIF service manager.
val managers = for {
Expand Down
3 changes: 1 addition & 2 deletions webapi/src/test/scala/org/knora/webapi/CoreSpec.scala
Expand Up @@ -30,7 +30,6 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import zio.&
import zio.Runtime
import zio.RuntimeConfig
import zio.ZEnvironment
import zio.ZIO
import zio.ZLayer
Expand Down Expand Up @@ -130,7 +129,7 @@ abstract class CoreSpec(_system: ActorSystem)
val log: LoggingAdapter = akka.event.Logging(system, this.getClass)

// The ZIO runtime used to run functional effects
val runtime = Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.fromInfo)
val runtime = Runtime.unsafeFromLayer(Logging.fromInfo)

// The effect for building a cache service manager and a IIIF service manager.
val managers = for {
Expand Down
3 changes: 1 addition & 2 deletions webapi/src/test/scala/org/knora/webapi/E2ESpec.scala
Expand Up @@ -50,7 +50,6 @@ import org.scalatest.wordspec.AnyWordSpecLike
import spray.json._
import zio.&
import zio.Runtime
import zio.RuntimeConfig
import zio.ZEnvironment
import zio.ZIO
import zio.ZLayer
Expand Down Expand Up @@ -116,7 +115,7 @@ class E2ESpec(_system: ActorSystem)
val log: LoggingAdapter = akka.event.Logging(system, this.getClass)

// The ZIO runtime used to run functional effects
val runtime = Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.fromInfo)
val runtime = Runtime.unsafeFromLayer(Logging.fromInfo)

// The effect for building a cache service manager and a IIIF service manager.
lazy val managers = for {
Expand Down
3 changes: 1 addition & 2 deletions webapi/src/test/scala/org/knora/webapi/ITKnoraLiveSpec.scala
Expand Up @@ -46,7 +46,6 @@ import org.scalatest.wordspec.AnyWordSpecLike
import spray.json._
import zio.&
import zio.Runtime
import zio.RuntimeConfig
import zio.ZEnvironment
import zio.ZIO
import zio.ZLayer
Expand Down Expand Up @@ -106,7 +105,7 @@ class ITKnoraLiveSpec(_system: ActorSystem)
val log: LoggingAdapter = akka.event.Logging(system, this.getClass)

// The ZIO runtime used to run functional effects
val runtime = Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.fromInfo)
val runtime = Runtime.unsafeFromLayer(Logging.fromInfo)

/**
* The effect for building a cache service manager, a IIIF service manager,
Expand Down
3 changes: 1 addition & 2 deletions webapi/src/test/scala/org/knora/webapi/R2RSpec.scala
Expand Up @@ -50,7 +50,6 @@ import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import zio.&
import zio.Runtime
import zio.RuntimeConfig
import zio.ZEnvironment
import zio.ZIO
import zio.ZLayer
Expand Down Expand Up @@ -106,7 +105,7 @@ class R2RSpec
implicit val timeout: Timeout = Timeout(settings.defaultTimeout)

// The ZIO runtime used to run functional effects
val runtime = Runtime(ZEnvironment.empty, RuntimeConfig.default @@ Logging.fromInfo)
val runtime = Runtime.unsafeFromLayer(Logging.fromInfo)

// The effect for building a cache service manager and a IIIF service manager.
lazy val managers = for {
Expand Down
Expand Up @@ -8,7 +8,7 @@ import scala.concurrent.duration.FiniteDuration

object AppConfigSpec extends ZIOSpec[AppConfig] {

val layer = ZLayer.make[AppConfig](AppConfig.live)
val bootstrap = ZLayer.make[AppConfig](AppConfig.live)

def spec = suite("ApplicationConfigSpec")(
test("successfully provide the application configuration") {
Expand Down
Expand Up @@ -413,8 +413,6 @@ class KnoraSipiIntegrationV2ITSpec
loginToken = lr.token

loginToken.nonEmpty should be(true)

logger.debug("token: {}", loginToken)
}

"create a resource with a still image file" in {
Expand Down

0 comments on commit 933f91e

Please sign in to comment.