feat: Working on cli client and tests
Some checks failed
CI / Run Tests (push) Failing after 3m7s

This commit is contained in:
2024-11-17 22:23:44 -05:00
parent 6472d3cd1e
commit ce18c44363
12 changed files with 287 additions and 73 deletions

View File

@@ -13,11 +13,11 @@ extension Application {
static let configuration: CommandConfiguration = .init(
commandName: "debug",
abstract: "Debug the environment variables."
abstract: "Debug the environment variables and command line arguments."
)
@OptionGroup
var shared: SharedOptions
var options: SharedOptions
@Flag(
name: [.customLong("show-password")],
@@ -31,7 +31,7 @@ extension Application {
print("--------------------------")
print("Running debug command...")
if let envFile = shared.envFile {
if let envFile = options.envFile {
print("Reading env file: \(envFile)")
print("--------------------------")
} else {
@@ -41,12 +41,12 @@ extension Application {
print("Loading EnvVars")
print("--------------------------")
let envVars = try await client.makeEnvVars(shared.envVarsRequest(logger: logger))
let envVars = try await client.makeEnvVars(options.envVarsRequest(logger: logger))
printEnvVars(envVars: envVars, showPassword: showPassword)
print("--------------------------")
if let logLevel = shared.logLevel, let level = logLevel() {
print("Log Level option: \(level)")
if let logLevel = options.logLevel {
print("Log Level option: \(logLevel)")
print("--------------------------")
} else {
print("Log Level option: nil")

View File

@@ -23,14 +23,13 @@ extension Application {
)
@OptionGroup
var shared: SharedOptions
var options: SharedOptions
mutating func run() async throws {
@Dependency(\.cliClient) var cliClient
let eventloopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
var logger = Logger(label: "dewpoint-controller")
let mqtt = try await setup(eventLoopGroup: eventloopGroup, logger: &logger)
let (mqtt, logger) = try await cliClient.setupRun(options: options)
logger.info("Setting up environment...")
do {
try await withDependencies {
@@ -48,6 +47,9 @@ extension Application {
gracefulShutdownSignals: [.sigterm, .sigint],
logger: logger
)
// These settings prevent services from running forever after we've
// received a shutdown signal. In general it should not needed unless the
// services don't shutdown their async streams properly.
serviceGroupConfiguration.maximumCancellationDuration = .seconds(5)
serviceGroupConfiguration.maximumGracefulShutdownDuration = .seconds(10)
@@ -57,30 +59,32 @@ extension Application {
try await serviceGroup.run()
}
// Here we've received a shutdown signal and shutdown all the
// services.
try await mqtt.shutdown()
try await eventloopGroup.shutdownGracefully()
} catch {
try await eventloopGroup.shutdownGracefully()
// If something fails, shutdown the mqtt client.
try await mqtt.shutdown()
}
}
private func setup(
eventLoopGroup: MultiThreadedEventLoopGroup,
logger: inout Logger
) async throws -> MQTTClient {
@Dependency(\.cliClient) var cliClient
let environment = try await cliClient.makeEnvVars(shared.envVarsRequest(logger: logger))
logger.logLevel = cliClient.logLevel(environment)
return try cliClient.makeClient(.init(
envVars: environment,
eventLoopGroup: eventLoopGroup,
logger: logger
))
}
}
}
// MARK: - Helpers
private extension CliClient {
func setupRun(
eventLoopGroup: MultiThreadedEventLoopGroup = .init(numberOfThreads: 1),
loggerLabel: String = "dewpoint-controller",
options: Application.SharedOptions
) async throws -> (MQTTClient, Logger) {
var logger = Logger(label: loggerLabel)
let environment = try await makeEnvVars(options.envVarsRequest(logger: logger))
logger.logLevel = logLevel(environment)
let client = try makeClient(.init(
environment: environment,
eventLoopGroup: eventLoopGroup,
logger: logger
))
return (client, logger)
}
}

View File

@@ -17,7 +17,7 @@ extension Application {
name: [.short, .customLong("log-level")],
help: "Set the logging level."
)
var logLevel: LogLevelContainer?
var logLevelContainer: LogLevelContainer?
@Option(
name: [.short, .long],
@@ -29,6 +29,8 @@ extension Application {
.init(envFilePath: envFile, logger: logger, version: version)
}
var logLevel: Logger.Level? { logLevelContainer?.logLevel }
}
}

View File

@@ -1,10 +0,0 @@
import Models
@_spi(Internal)
public extension Array where Element == TemperatureAndHumiditySensor {
static var live: Self {
TemperatureAndHumiditySensor.Location.allCases.map { location in
TemperatureAndHumiditySensor(location: location)
}
}
}