feat: Begins using swift argument parser and creating cli client dependency
All checks were successful
CI / Run Tests (push) Successful in 4m27s

This commit is contained in:
2024-11-16 22:32:32 -05:00
parent 3416ce1003
commit 6472d3cd1e
19 changed files with 657 additions and 342 deletions

View File

@@ -0,0 +1,86 @@
import ArgumentParser
import CliClient
import Dependencies
import Foundation
import Logging
import Models
import MQTTConnectionService
import MQTTManager
import MQTTNIO
import NIO
import PsychrometricClientLive
import SensorsService
import ServiceLifecycle
extension Application {
/// Run the controller.
///
struct Run: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "run",
abstract: "Run the controller."
)
@OptionGroup
var shared: 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)
do {
try await withDependencies {
$0.psychrometricClient = .liveValue
$0.mqtt = .live(client: mqtt, logger: logger)
} operation: {
let mqttConnection = MQTTConnectionService(logger: logger)
let sensors = SensorsService(sensors: .live, logger: logger)
var serviceGroupConfiguration = ServiceGroupConfiguration(
services: [
mqttConnection,
sensors
],
gracefulShutdownSignals: [.sigterm, .sigint],
logger: logger
)
serviceGroupConfiguration.maximumCancellationDuration = .seconds(5)
serviceGroupConfiguration.maximumGracefulShutdownDuration = .seconds(10)
let serviceGroup = ServiceGroup(configuration: serviceGroupConfiguration)
logger.info("Starting dewpoint-controller!")
try await serviceGroup.run()
}
try await mqtt.shutdown()
try await eventloopGroup.shutdownGracefully()
} catch {
try await eventloopGroup.shutdownGracefully()
}
}
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