diff --git a/.env.development.template b/.env.development.template index c897bff0e..3539bf1af 100644 --- a/.env.development.template +++ b/.env.development.template @@ -3,3 +3,4 @@ DATABASE_PORT=6432 DATABASE_NAME=spi_dev DATABASE_USERNAME=spi_dev DATABASE_PASSWORD=xxx +REDIS_HOST=localhost \ No newline at end of file diff --git a/.env.testing.template b/.env.testing.template index 1118c84ba..a2b58f4f5 100644 --- a/.env.testing.template +++ b/.env.testing.template @@ -3,3 +3,4 @@ DATABASE_PORT=5432 DATABASE_NAME=spi_test DATABASE_USERNAME=spi_test DATABASE_PASSWORD=xxx +REDIS_HOST=localhost \ No newline at end of file diff --git a/Makefile b/Makefile index 5fdc944f5..d76fa3f8b 100644 --- a/Makefile +++ b/Makefile @@ -92,7 +92,13 @@ ingest: analyze: swift run Run analyze --limit 1 -db-up: db-up-dev db-up-test +redis-up-dev: + docker run --name spi_redis -p 6379:6379 -d redis/redis-stack:7.4.0-v1 + +redis-down-dev: + docker rm -f spi_redis + +db-up: db-up-dev db-up-test redis-up-dev db-up-dev: 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: -d \ postgres:13-alpine -db-down: db-down-dev db-down-test +db-down: db-down-dev db-down-test redis-down-dev db-down-dev: docker rm -f spi_dev diff --git a/Sources/App/Core/Dependencies/EnvironmentClient.swift b/Sources/App/Core/Dependencies/EnvironmentClient.swift index b0bcc40a8..836473183 100644 --- a/Sources/App/Core/Dependencies/EnvironmentClient.swift +++ b/Sources/App/Core/Dependencies/EnvironmentClient.swift @@ -49,7 +49,6 @@ struct EnvironmentClient { var plausibleBackendReportingSiteID: @Sendable () -> String? var processingBuildBacklog: @Sendable () -> Bool = { XCTFail("processingBuildBacklog"); return false } var random: @Sendable (_ range: ClosedRange) -> Double = { XCTFail("random"); return Double.random(in: $0) } - var runnerIds: @Sendable () -> [String] = { XCTFail("runnerIds"); return [] } enum FailureMode: String { case fetchMetadataFailed @@ -59,6 +58,8 @@ struct EnvironmentClient { case repositorySaveFailed case repositorySaveUniqueViolation } + var redisHostname: @Sendable () -> String = { "redis" } + var runnerIds: @Sendable () -> [String] = { XCTFail("runnerIds"); return [] } var shouldFail: @Sendable (_ failureMode: FailureMode) -> Bool = { _ in XCTFail("shouldFail"); return false } var siteURL: @Sendable () -> String = { XCTFail("siteURL"); return "" } } @@ -126,6 +127,12 @@ extension EnvironmentClient: DependencyKey { Environment.get("PROCESSING_BUILD_BACKLOG").flatMap(\.asBool) ?? false }, random: { range in Double.random(in: range) }, + redisHostname: { + // Defaulting this to `redis`, which is the service name in `app.yml`. + // This is also why `REDIS_HOST` is not set as an env variable in `app.yml`, + // it's a known value that needs no configuration outside of local use for testing. + Environment.get("REDIS_HOST") ?? "redis" + }, runnerIds: { Environment.decode("RUNNER_IDS", as: [String].self) ?? [] }, shouldFail: { failureMode in let shouldFail = Environment.decode("FAILURE_MODE", as: [String: Double].self) ?? [:] diff --git a/Sources/App/Core/Dependencies/RedisClient.swift b/Sources/App/Core/Dependencies/RedisClient.swift index d241e4f94..190a27d5b 100644 --- a/Sources/App/Core/Dependencies/RedisClient.swift +++ b/Sources/App/Core/Dependencies/RedisClient.swift @@ -109,15 +109,14 @@ private actor Redis { } private init() async throws { + @Dependency(\.environment) var environment let connection = RedisConnection.make( - configuration: try .init(hostname: Redis.hostname), + configuration: try .init(hostname: environment.redisHostname()), boundEventLoop: NIOSingletons.posixEventLoopGroup.any() ) self.client = try await connection.get() } - // This hostname has to match the redir service name in app.yml. - static let hostname = "redis" static let maxConnectionAttempts = 3 func set(key: String, value: String?, expiresIn: Duration?) async {