Refactor ThorpArchive.logEvent(StorageQueueEvent) (#141)

* [core] ThorpArchive Refactoring

* [core] ThorpArchive Refactoring

* [core] ThorpArchive refactored

* [core] ThorpArchive convert output to ConsoleOut.WithBatchMode messages

* [console] Console rename putMessageLnB to overload putMessageLn
This commit is contained in:
Paul Campbell 2019-08-01 22:39:52 +01:00 committed by GitHub
parent adbf8af2f3
commit e3b0260b6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 52 deletions

View file

@ -57,9 +57,8 @@ val zioDependencies = Seq(
) )
) )
// cli -> thorp-lib -> storage-aws -> core -> storage-api -> console -> domain // cli -> thorp-lib -> storage-aws -> core -> storage-api -> console -> config -> domain
// storage-api -> config -> domain // storage-api -> config -> filesystem
// config -> filesystem
lazy val thorp = (project in file(".")) lazy val thorp = (project in file("."))
.settings(commonSettings) .settings(commonSettings)
@ -114,7 +113,7 @@ lazy val console = (project in file("console"))
.settings(commonSettings) .settings(commonSettings)
.settings(zioDependencies) .settings(zioDependencies)
.settings(assemblyJarName in assembly := "console.jar") .settings(assemblyJarName in assembly := "console.jar")
.dependsOn(domain) .dependsOn(config)
lazy val config = (project in file("config")) lazy val config = (project in file("config"))
.settings(commonSettings) .settings(commonSettings)

View file

@ -3,6 +3,7 @@ package net.kemitix.thorp.console
import java.io.PrintStream import java.io.PrintStream
import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.atomic.AtomicReference
import net.kemitix.thorp.config.Config
import zio.{UIO, ZIO} import zio.{UIO, ZIO}
import scala.{Console => SConsole} import scala.{Console => SConsole}
@ -60,4 +61,8 @@ object Console {
final def putMessageLn(line: ConsoleOut): ZIO[Console, Nothing, Unit] = final def putMessageLn(line: ConsoleOut): ZIO[Console, Nothing, Unit] =
ZIO.accessM(_.console putStrLn line) ZIO.accessM(_.console putStrLn line)
final def putMessageLn(
line: ConsoleOut.WithBatchMode): ZIO[Console with Config, Nothing, Unit] =
ZIO.accessM(line() >>= _.console.putStrLn)
} }

View file

@ -1,11 +1,28 @@
package net.kemitix.thorp.console package net.kemitix.thorp.console
import net.kemitix.thorp.config.Config
import net.kemitix.thorp.domain.StorageQueueEvent.Action
import net.kemitix.thorp.domain.Terminal._
import net.kemitix.thorp.domain.{Bucket, RemoteKey, Sources} import net.kemitix.thorp.domain.{Bucket, RemoteKey, Sources}
import zio.{UIO, ZIO}
import scala.io.AnsiColor._
sealed trait ConsoleOut { sealed trait ConsoleOut {
def en: String def en: String
} }
object ConsoleOut { object ConsoleOut {
sealed trait WithBatchMode {
def en: String
def enBatch: String
def apply(): ZIO[Config, Nothing, String] =
Config.batchMode >>= selectLine
private def selectLine(batchMode: Boolean) =
if (batchMode) UIO(enBatch) else UIO(en)
}
case class ValidConfig( case class ValidConfig(
bucket: Bucket, bucket: Bucket,
prefix: RemoteKey, prefix: RemoteKey,
@ -18,4 +35,36 @@ object ConsoleOut {
s"Source: $sourcesList") s"Source: $sourcesList")
.mkString(", ") .mkString(", ")
} }
case class UploadComplete(remoteKey: RemoteKey)
extends ConsoleOut.WithBatchMode {
override def en: String =
s"${GREEN}Uploaded:$RESET ${remoteKey.key}$eraseToEndOfScreen"
override def enBatch: String =
s"Uploaded: ${remoteKey.key}"
}
case class CopyComplete(sourceKey: RemoteKey, targetKey: RemoteKey)
extends ConsoleOut.WithBatchMode {
override def en: String =
s"${GREEN}Copied:$RESET ${sourceKey.key} => ${targetKey.key}$eraseToEndOfScreen"
override def enBatch: String =
s"Copied: ${sourceKey.key} => ${targetKey.key}"
}
case class DeleteComplete(remoteKey: RemoteKey)
extends ConsoleOut.WithBatchMode {
override def en: String =
s"${GREEN}Deleted:$RESET ${remoteKey.key}$eraseToEndOfScreen"
override def enBatch: String =
s"Deleted: $remoteKey"
}
case class ErrorQueueEventOccurred(action: Action, e: Throwable)
extends ConsoleOut.WithBatchMode {
override def en: String =
s"${action.name} failed: ${action.keys}: ${e.getMessage}"
override def enBatch: String =
s"${RED}ERROR:$RESET ${action.name} ${action.keys}: ${e.getMessage}$eraseToEndOfScreen"
}
} }

View file

@ -1,21 +1,17 @@
package net.kemitix.thorp.core package net.kemitix.thorp.core
import net.kemitix.thorp.config.Config import net.kemitix.thorp.config.Config
import net.kemitix.thorp.console.ConsoleOut.{
CopyComplete,
DeleteComplete,
ErrorQueueEventOccurred,
UploadComplete
}
import net.kemitix.thorp.console._ import net.kemitix.thorp.console._
import net.kemitix.thorp.domain.StorageQueueEvent import net.kemitix.thorp.domain.StorageQueueEvent
import net.kemitix.thorp.domain.StorageQueueEvent.{ import net.kemitix.thorp.domain.StorageQueueEvent._
CopyQueueEvent,
DeleteQueueEvent,
DoNothingQueueEvent,
ErrorQueueEvent,
ShutdownQueueEvent,
UploadQueueEvent
}
import net.kemitix.thorp.domain.Terminal._
import net.kemitix.thorp.storage.api.Storage import net.kemitix.thorp.storage.api.Storage
import zio.TaskR import zio.{TaskR, ZIO}
import scala.io.AnsiColor._
trait ThorpArchive { trait ThorpArchive {
@ -26,47 +22,18 @@ trait ThorpArchive {
): TaskR[Storage with Console with Config, StorageQueueEvent] ): TaskR[Storage with Console with Config, StorageQueueEvent]
def logEvent( def logEvent(
event: StorageQueueEvent event: StorageQueueEvent): TaskR[Console with Config, StorageQueueEvent] =
): TaskR[Console with Config, StorageQueueEvent] =
event match { event match {
case UploadQueueEvent(remoteKey, _) => case UploadQueueEvent(remoteKey, _) =>
for { ZIO(event) <* Console.putMessageLn(UploadComplete(remoteKey))
batchMode <- Config.batchMode
_ <- TaskR.when(batchMode)(
Console.putStrLn(s"Uploaded: ${remoteKey.key}"))
_ <- TaskR.when(!batchMode)(
Console.putStrLn(
s"${GREEN}Uploaded:$RESET ${remoteKey.key}$eraseToEndOfScreen"))
} yield event
case CopyQueueEvent(sourceKey, targetKey) => case CopyQueueEvent(sourceKey, targetKey) =>
for { ZIO(event) <* Console.putMessageLn(CopyComplete(sourceKey, targetKey))
batchMode <- Config.batchMode
_ <- TaskR.when(batchMode)(
Console.putStrLn(s"Copied: ${sourceKey.key} => ${targetKey.key}"))
_ <- TaskR.when(!batchMode)(
Console.putStrLn(
s"${GREEN}Copied:$RESET ${sourceKey.key} => ${targetKey.key}$eraseToEndOfScreen")
)
} yield event
case DeleteQueueEvent(remoteKey) => case DeleteQueueEvent(remoteKey) =>
for { ZIO(event) <* Console.putMessageLn(DeleteComplete(remoteKey))
batchMode <- Config.batchMode
_ <- TaskR.when(batchMode)(Console.putStrLn(s"Deleted: $remoteKey"))
_ <- TaskR.when(!batchMode)(
Console.putStrLn(
s"${GREEN}Deleted:$RESET ${remoteKey.key}$eraseToEndOfScreen"))
} yield event
case ErrorQueueEvent(action, _, e) => case ErrorQueueEvent(action, _, e) =>
for { ZIO(event) <* Console.putMessageLn(ErrorQueueEventOccurred(action, e))
batchMode <- Config.batchMode case DoNothingQueueEvent(_) => ZIO(event)
_ <- TaskR.when(batchMode)( case ShutdownQueueEvent() => ZIO(event)
Console.putStrLn(
s"${action.name} failed: ${action.keys}: ${e.getMessage}"))
_ <- TaskR.when(!batchMode)(Console.putStrLn(
s"${RED}ERROR:$RESET ${action.name} ${action.keys}: ${e.getMessage}$eraseToEndOfScreen"))
} yield event
case DoNothingQueueEvent(_) => TaskR(event)
case ShutdownQueueEvent() => TaskR(event)
} }
} }