-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PATCH] Amended Forex Service and Added Refresh Task
- Loading branch information
1 parent
d689678
commit ec19f61
Showing
22 changed files
with
320 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
app { | ||
http { | ||
host = "0.0.0.0" | ||
port = 8080 | ||
port = 8081 | ||
timeout = 40 seconds | ||
} | ||
oneframe { | ||
token = "10dc303535874aeccc86a8251e6992f5" | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package forex | ||
|
||
import cats.effect.{ExitCode, IO} | ||
import forex.components.scheduler.tasks.TaskScheduler | ||
import forex.thirdparties.oneframae.OneFrameForexRatesHandler | ||
|
||
object StartupTasks { | ||
def process(): IO[ExitCode] = { | ||
TaskScheduler.scheduleForexCacheRefreshJob | ||
IO(OneFrameForexRatesHandler.handleCache()).as(ExitCode.Success) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
forex-mtl/src/main/scala/forex/components/cache/Algebra.scala
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,11 @@ | ||
package forex.components.cache | ||
|
||
import forex.services.rates.errors._ | ||
|
||
|
||
trait Algebra { | ||
|
||
def put[A](key:String, value: A) : Boolean | ||
|
||
def get(key:String) : Either[Error, String] | ||
} |
7 changes: 7 additions & 0 deletions
7
forex-mtl/src/main/scala/forex/components/cache/Protocol.scala
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,7 @@ | ||
package forex.components.cache | ||
|
||
import forex.components.cache.redis.Redis | ||
|
||
object Protocol { | ||
def redis(segment: String) : Algebra = new Redis(segment) | ||
} |
24 changes: 24 additions & 0 deletions
24
forex-mtl/src/main/scala/forex/components/cache/redis/Redis.scala
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,24 @@ | ||
package forex.components.cache.redis | ||
|
||
import com.redis.RedisClient | ||
import forex.components.cache.Algebra | ||
import forex.programs.rates.ErrorCodes | ||
import forex.services.rates.errors._ | ||
|
||
class Redis(segment: String) extends Algebra { | ||
|
||
private[redis] lazy val redisClient: RedisClient = new RedisClient("localhost", 6379) | ||
|
||
override def put[A](key: String, value: A): Boolean = { | ||
redisClient.set(key = ForexRedisHelper.getSegmentPrefixedKey(segment, key), value = value) | ||
} | ||
|
||
override def get(key:String): Either[Error, String] = { | ||
redisClient.get(ForexRedisHelper.getSegmentPrefixedKey(segment, key)) match { | ||
case Some(value) => Right(value).withLeft[Error] | ||
case None => Left(Error.RateLookupFailed(ErrorCodes.cacheFetchFailed, "Value Not Found In Cache")) | ||
} | ||
|
||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
forex-mtl/src/main/scala/forex/components/cache/redis/RedisHelper.scala
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,17 @@ | ||
package forex.components.cache.redis | ||
|
||
abstract class RedisHelper { | ||
def getSegmentPrefixedKey(segement: String, key:String): String | ||
} | ||
|
||
object Segments { | ||
final val forexRates = "FOREX_RATES" | ||
} | ||
|
||
object ForexRedisHelper extends RedisHelper { | ||
override def getSegmentPrefixedKey(segment: String, key: String): String = segment ++ "::" ++ key | ||
|
||
def getFormattedKey(from: String, to: String): String = { | ||
from + "&" + to | ||
} | ||
} |
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,8 @@ | ||
package forex | ||
|
||
import forex.components.cache.Algebra | ||
|
||
package object components { | ||
type Cache = Algebra | ||
final val CacheAPI = cache.Protocol | ||
} |
45 changes: 45 additions & 0 deletions
45
forex-mtl/src/main/scala/forex/components/scheduler/tasks/TaskScheduler.scala
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,45 @@ | ||
package forex.components.scheduler.tasks | ||
|
||
import cats.effect.{Concurrent, IO, IOApp, Timer} | ||
import cats.implicits.toFlatMapOps | ||
import forex.domain.Tasks | ||
import forex.thirdparties.oneframae.OneFrameForexRatesHandler | ||
import org.log4s.{Logger, getLogger} | ||
|
||
import scala.concurrent.duration._ | ||
|
||
class TaskScheduler(task: Tasks, f: IO[Unit], frequency: FiniteDuration) extends IOApp .Simple { | ||
|
||
val logger: Logger = getLogger(getClass) | ||
|
||
private def schedule[F[_] : Concurrent : Timer](task: F[Unit], frequency: FiniteDuration) : F[Unit] = { | ||
Timer[F].sleep(frequency).flatMap(_ => task) | ||
} | ||
|
||
private[tasks] def scheduleJob(): Unit = { | ||
if (TasksLibrary.isTaskSpawned(this.task)) { | ||
logger.info("Job Already Spawned...") | ||
} else { | ||
schedule[IO](this.f, this.frequency) | ||
TasksLibrary.addSpawnedTask(task) match { | ||
case Some(_) => logger.info("Task Successfully Added To Task Library.") | ||
case None => logger.warn("Task Addition to Library Failed. Audit and fix the issue.") | ||
} | ||
} | ||
} | ||
|
||
override def run: IO[Unit] = { | ||
IO(scheduleJob()) | ||
} | ||
} | ||
|
||
object TaskScheduler { | ||
def apply(task: Tasks, f: IO[Unit], frequency: FiniteDuration): TaskScheduler = { | ||
TaskScheduler(task, f, frequency) | ||
} | ||
|
||
def scheduleForexCacheRefreshJob = { | ||
TaskScheduler(Tasks.FOREX_JOB, OneFrameForexRatesHandler.handleCache(), 4.minutes) | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
forex-mtl/src/main/scala/forex/components/scheduler/tasks/TasksLibrary.scala
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,13 @@ | ||
package forex.components.scheduler.tasks | ||
|
||
import forex.domain.Tasks | ||
|
||
import scala.collection.mutable | ||
|
||
object TasksLibrary { | ||
private val tasksSpawned: mutable.Map[String, Boolean] = mutable.Map[String, Boolean]() | ||
|
||
def isTaskSpawned(task: Tasks) : Boolean = tasksSpawned(task.name()) | ||
|
||
private[tasks] def addSpawnedTask(task: Tasks) : Option[Boolean] = tasksSpawned.put(task.name(), true) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package forex.domain | ||
|
||
sealed trait Tasks { | ||
protected var taskName: String = null | ||
protected def setName() : Unit | ||
def name() : String = this.taskName | ||
} | ||
|
||
object Tasks { | ||
case object FOREX_JOB extends Tasks { | ||
override def setName(): Unit = this.taskName = "FOREX_JOB" | ||
} | ||
} | ||
|
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
10 changes: 10 additions & 0 deletions
10
forex-mtl/src/main/scala/forex/programs/rates/ErrorCodes.scala
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,10 @@ | ||
package forex.programs.rates | ||
|
||
object ErrorCodes { | ||
final val cachePopulationFailed: String = "FX_CacheNotPopulated" | ||
final val cacheFetchFailed: String = "FX_CacheFetchFailed" | ||
final val cacheInitFailed: String = "FX_CachecInitializationFailed" | ||
final val cacheRefreshFailed: String = "FX_CacheRefreshFailed" | ||
final val fxRateLookUpFailed: String = "FX_RatesFetchFailed" | ||
final val internalError: String = "FX_InternalError" | ||
} |
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
Oops, something went wrong.