Skip to content

Commit f65d786

Browse files
Merge pull request #3580 from SwiftPackageIndex/local-redis
adding redis to local development
2 parents f7a7ccf + 6b46bcd commit f65d786

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

.env.development.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ DATABASE_PORT=6432
33
DATABASE_NAME=spi_dev
44
DATABASE_USERNAME=spi_dev
55
DATABASE_PASSWORD=xxx
6+
REDIS_HOST=localhost

.env.testing.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ DATABASE_PORT=5432
33
DATABASE_NAME=spi_test
44
DATABASE_USERNAME=spi_test
55
DATABASE_PASSWORD=xxx
6+
REDIS_HOST=localhost

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ ingest:
9292
analyze:
9393
swift run Run analyze --limit 1
9494

95-
db-up: db-up-dev db-up-test
95+
redis-up-dev:
96+
docker run --name spi_redis -p 6379:6379 -d redis/redis-stack:7.4.0-v1
97+
98+
redis-down-dev:
99+
docker rm -f spi_redis
100+
101+
db-up: db-up-dev db-up-test redis-up-dev
96102

97103
db-up-dev:
98104
docker run --name spi_dev -e POSTGRES_DB=spi_dev -e POSTGRES_USER=spi_dev -e POSTGRES_PASSWORD=xxx -p 6432:5432 -d postgres:16-alpine
@@ -111,7 +117,7 @@ db-up-test:
111117
-d \
112118
postgres:13-alpine
113119

114-
db-down: db-down-dev db-down-test
120+
db-down: db-down-dev db-down-test redis-down-dev
115121

116122
db-down-dev:
117123
docker rm -f spi_dev

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ struct EnvironmentClient {
4949
var plausibleBackendReportingSiteID: @Sendable () -> String?
5050
var processingBuildBacklog: @Sendable () -> Bool = { XCTFail("processingBuildBacklog"); return false }
5151
var random: @Sendable (_ range: ClosedRange<Double>) -> Double = { XCTFail("random"); return Double.random(in: $0) }
52-
var runnerIds: @Sendable () -> [String] = { XCTFail("runnerIds"); return [] }
5352

5453
enum FailureMode: String {
5554
case fetchMetadataFailed
@@ -59,6 +58,8 @@ struct EnvironmentClient {
5958
case repositorySaveFailed
6059
case repositorySaveUniqueViolation
6160
}
61+
var redisHostname: @Sendable () -> String = { "redis" }
62+
var runnerIds: @Sendable () -> [String] = { XCTFail("runnerIds"); return [] }
6263
var shouldFail: @Sendable (_ failureMode: FailureMode) -> Bool = { _ in XCTFail("shouldFail"); return false }
6364
var siteURL: @Sendable () -> String = { XCTFail("siteURL"); return "" }
6465
}
@@ -126,6 +127,12 @@ extension EnvironmentClient: DependencyKey {
126127
Environment.get("PROCESSING_BUILD_BACKLOG").flatMap(\.asBool) ?? false
127128
},
128129
random: { range in Double.random(in: range) },
130+
redisHostname: {
131+
// Defaulting this to `redis`, which is the service name in `app.yml`.
132+
// This is also why `REDIS_HOST` is not set as an env variable in `app.yml`,
133+
// it's a known value that needs no configuration outside of local use for testing.
134+
Environment.get("REDIS_HOST") ?? "redis"
135+
},
129136
runnerIds: { Environment.decode("RUNNER_IDS", as: [String].self) ?? [] },
130137
shouldFail: { failureMode in
131138
let shouldFail = Environment.decode("FAILURE_MODE", as: [String: Double].self) ?? [:]

Sources/App/Core/Dependencies/RedisClient.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@ private actor Redis {
109109
}
110110

111111
private init() async throws {
112+
@Dependency(\.environment) var environment
112113
let connection = RedisConnection.make(
113-
configuration: try .init(hostname: Redis.hostname),
114+
configuration: try .init(hostname: environment.redisHostname()),
114115
boundEventLoop: NIOSingletons.posixEventLoopGroup.any()
115116
)
116117
self.client = try await connection.get()
117118
}
118119

119-
// This hostname has to match the redir service name in app.yml.
120-
static let hostname = "redis"
121120
static let maxConnectionAttempts = 3
122121

123122
func set(key: String, value: String?, expiresIn: Duration?) async {

0 commit comments

Comments
 (0)