Skip to content

Commit

Permalink
Update sbt version to 1.9.2, add REDIS_HOST environment variable, and…
Browse files Browse the repository at this point in the history
… use host from configuration file in Redis and Sentinel classes
  • Loading branch information
tomiwu committed Apr 11, 2024
1 parent f968ba2 commit c7f95ef
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 299 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/push_main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build @horn/brando
on:
push:
branches:
- main
workflow_dispatch:

jobs:
build:
name: Build $ Push
runs-on: ubuntu-22.04
# Service containers to run with `container-job`
services:
# Label used to access the service container
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '11'
cache: 'sbt'
- name: Compile & Test
run: sbt test
env:
GITHUB_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }}
REDIS_HOST: "redis"
- name: Fetch version from build.sbt
id: current_version
run: echo "version=$(grep "version.*:=" version.sbt | sed 's/.*"\(.*\)"/\1/')" >> $GITHUB_OUTPUT
- name: Publish API to github package registry
run: sbt +publish
env:
GITHUB_TOKEN: ${{ secrets.DEPLOY_GITHUB_TOKEN }}
- uses: rickstaa/action-create-tag@v1
with:
tag: ${{ steps.current_version.outputs.version }}
tag_exists_error: false
52 changes: 41 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
.DS_Store
# bloop and metals
.bloop
.bsp
.metals
project/metals.sbt
project/project/metals.sbt
project/project/project/metals.sbt

# vs code
.vscode

# scala 3
.tasty

# sbt
project/project/
project/target/
target/
project/boot/
project/build/target/
project/plugins/target/
project/plugins/lib_managed/
project/plugins/src_managed/
*.log
.cache

# eclipse
build/
.classpath
.project
.settings
*.rdb
.idea/
.idea_modules/
.worksheet
bin/
.cache

# intellij idea
*.log
*.iml
*.ipr
*.iws
.idea

# mac
.DS_Store

# other?
.history
.scala_dependencies
.cache-main

# general
*.class
9 changes: 1 addition & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
name := "brando"
organization := "com.digital-achiever"
version := "3.2.0"
scalaVersion := "2.13.8"
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

// Sbt seems to have some issues with publishing packages with credentials and below line is an workaround
// for this bug: https://github.com/sbt/sbt/issues/3570
updateOptions := updateOptions.value.withGigahorse(false)

publishTo := Some("Horn SBT" at "https://sbt.horn.co/repository/internal")
credentials += Credentials(
"Repository Archiva Managed internal Repository",
"sbt.horn.co",
sys.env("HORN_SBT_USERNAME"),
sys.env("HORN_SBT_PASSWORD")
)
publishTo := Some("Horn SBT" at "https://maven.pkg.github.com/8eo/brando")

val akkaV = "2.6.19"

Expand Down
6 changes: 6 additions & 0 deletions github.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
credentials += Credentials(
"GitHub Package Registry",
"maven.pkg.github.com",
"8eo",
sys.env("GITHUB_TOKEN")
)
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.5.1
sbt.version=1.9.2
2 changes: 2 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
brando{
connection{
timeout = 2s
host = "localhost"
host = ${?REDIS_HOST}

#Delay before trying to reconnect
retry.delay = 1 s
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/brando/Redis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.concurrent.duration._

object Redis {
def apply(
host: String = "localhost",
host: Option[String]= None,
port: Int = 6379,
database: Int = 0,
auth: Option[String] = None,
Expand All @@ -21,7 +21,7 @@ object Redis {

val config = ConfigFactory.load()
Props(classOf[Redis],
host,
host.getOrElse(config.getString("brando.connection.host")),
port,
database,
auth,
Expand Down
32 changes: 20 additions & 12 deletions src/main/scala/brando/Sentinel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ import com.typesafe.config.ConfigFactory
import scala.concurrent.duration._

object Sentinel {

val host = ConfigFactory.load().getString("brando.connection.host")

def apply(
sentinels: Seq[Server] = Seq(Server("localhost", 26379)),
listeners: Set[ActorRef] = Set(),
connectionTimeout: Option[FiniteDuration] = None,
connectionHeartbeatDelay: Option[FiniteDuration] = None): Props = {
sentinels: Seq[Server] = Seq(Server(host, 26379)),
listeners: Set[ActorRef] = Set(),
connectionTimeout: Option[FiniteDuration] = None,
connectionHeartbeatDelay: Option[FiniteDuration] = None
): Props = {

val config = ConfigFactory.load()
Props(classOf[Sentinel], sentinels, listeners,
connectionTimeout.getOrElse(
config.getDuration("brando.connection.timeout", TimeUnit.MILLISECONDS).millis),
connectionHeartbeatDelay)
Props(
classOf[Sentinel],
sentinels,
listeners,
connectionTimeout.getOrElse(config.getDuration("brando.connection.timeout", TimeUnit.MILLISECONDS).millis),
connectionHeartbeatDelay
)
}

case class Server(host: String, port: Int)
Expand All @@ -31,14 +38,15 @@ class Sentinel(
var sentinels: Seq[Sentinel.Server],
var listeners: Set[ActorRef],
connectionTimeout: FiniteDuration,
connectionHeartbeatDelay: Option[FiniteDuration]) extends Actor {
connectionHeartbeatDelay: Option[FiniteDuration]
) extends Actor {

import Sentinel._

implicit val timeout = Timeout(connectionTimeout)

var connection = context.system.deadLetters
var retries = 0
var retries = 0

override def preStart: Unit = {
listeners.map(context.watch)
Expand All @@ -64,8 +72,8 @@ class Sentinel(
case Connect(Server(host, port) :: tail)
notifyStateChange(Connection.Connecting(host, port))
retries += 1
connection = context.actorOf(Props(classOf[Connection],
self, host, port, connectionTimeout, connectionHeartbeatDelay))
connection =
context.actorOf(Props(classOf[Connection], self, host, port, connectionTimeout, connectionHeartbeatDelay))

case x: Connection.Connected
context.become(connected)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/brando/ShardManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class ShardManager(
(shard, sentinelClient) match {
case (RedisShard(id, host, port, database, auth), _)
val brando =
context.actorOf(Redis(host, port, database, auth, listeners,
context.actorOf(Redis(Some(host), port, database, auth, listeners,
Some(connectionTimeout), Some(connectionRetryDelay), None,
connectionHeartbeatDelay))
add(shard, brando)
Expand Down
33 changes: 18 additions & 15 deletions src/test/scala/brando/RedisClientSentinelTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package brando
import akka.actor._
import akka.testkit._
import org.scalatest._
import com.typesafe.config.ConfigFactory

class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTest")) with FunSpecLike
with ImplicitSender {

import Connection._
import Sentinel._

val host = ConfigFactory.load().getString("brando.connection.host")

describe("RedisClientSentinel") {
describe("when connecting") {
it("should use sentinel to resolve the ip and port") {
Expand All @@ -25,21 +28,21 @@ class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTe
val sentinelProbe = TestProbe()

val sentinel = system.actorOf(Sentinel(
sentinels = Seq(Server("localhost", 26379)),
sentinels = Seq(Server(host, 26379)),
listeners = Set(sentinelProbe.ref)))
val brando = system.actorOf(RedisSentinel(
master = "mymaster",
sentinelClient = sentinel,
listeners = Set(redisProbe.ref)))

sentinelProbe.expectMsg(
Connecting("localhost", 26379))
Connecting(host, 26379))
sentinelProbe.expectMsg(
Connected("localhost", 26379))
Connected(host, 26379))
redisProbe.expectMsg(
Connecting("127.0.0.1", 6379))
Connecting(host, 6379))
redisProbe.expectMsg(
Connected("127.0.0.1", 6379))
Connected(host, 6379))
}
}

Expand All @@ -49,35 +52,35 @@ class RedisClientSentinelTest extends TestKit(ActorSystem("RedisClientSentinelTe
val sentinelProbe = TestProbe()

val sentinel = system.actorOf(Sentinel(
sentinels = Seq(Server("localhost", 26379)),
sentinels = Seq(Server(host, 26379)),
listeners = Set(sentinelProbe.ref)))
val brando = system.actorOf(RedisSentinel(
master = "mymaster",
sentinelClient = sentinel,
listeners = Set(redisProbe.ref)))

sentinelProbe.expectMsg(
Connecting("localhost", 26379))
Connecting(host, 26379))
sentinelProbe.expectMsg(
Connected("localhost", 26379))
Connected(host, 26379))
redisProbe.expectMsg(
Connecting("127.0.0.1", 6379))
Connecting(host, 6379))
redisProbe.expectMsg(
Connected("127.0.0.1", 6379))
Connected(host, 6379))

brando ! Disconnected("127.0.0.1", 6379)
brando ! Disconnected(host, 6379)

redisProbe.expectMsg(
Disconnected("127.0.0.1", 6379))
Disconnected(host, 6379))
redisProbe.expectMsg(
Connecting("127.0.0.1", 6379))
Connecting(host, 6379))
redisProbe.expectMsg(
Connected("127.0.0.1", 6379))
Connected(host, 6379))
}

it("should return a failure when disconnected") {
val sentinel = system.actorOf(Sentinel(
sentinels = Seq(Server("localhost", 26379))))
sentinels = Seq(Server(host, 26379))))
val brando = system.actorOf(RedisSentinel(
master = "mymaster",
sentinelClient = sentinel))
Expand Down
Loading

0 comments on commit c7f95ef

Please sign in to comment.