feat: Cleans up some of the shutdown logic so that the MQTTClient is disconnected properly.
This commit is contained in:
@@ -10,7 +10,7 @@ public extension MQTTConnectionManager {
|
||||
logger: Logger? = nil
|
||||
) -> Self {
|
||||
let manager = ConnectionManager(client: client, logger: logger)
|
||||
return .init { _ in
|
||||
return .init {
|
||||
try await manager.connect(cleanSession: cleanSession)
|
||||
|
||||
return manager.stream
|
||||
@@ -76,6 +76,8 @@ private actor ConnectionManager {
|
||||
|
||||
nonisolated func shutdown() {
|
||||
client.logger.trace("Shutting down connection.")
|
||||
client.removeCloseListener(named: name)
|
||||
client.removeShutdownListener(named: name)
|
||||
continuation.yield(.shuttingDown)
|
||||
continuation.finish()
|
||||
}
|
||||
|
||||
@@ -11,11 +11,10 @@ import ServiceLifecycle
|
||||
@DependencyClient
|
||||
public struct MQTTConnectionManager: Sendable {
|
||||
|
||||
public var connect: @Sendable (_ cleanSession: Bool) async throws -> AsyncStream<Event>
|
||||
public var connect: @Sendable () async throws -> AsyncStream<Event>
|
||||
public var shutdown: () -> Void
|
||||
|
||||
public enum Event: Sendable {
|
||||
case notStarted
|
||||
case connected
|
||||
case disconnected
|
||||
case shuttingDown
|
||||
@@ -43,14 +42,11 @@ public extension DependencyValues {
|
||||
public actor MQTTConnectionService: Service {
|
||||
@Dependency(\.mqttConnectionManager) var manager
|
||||
|
||||
private let cleanSession: Bool
|
||||
private nonisolated let logger: Logger?
|
||||
|
||||
public init(
|
||||
cleanSession: Bool = false,
|
||||
logger: Logger? = nil
|
||||
) {
|
||||
self.cleanSession = cleanSession
|
||||
self.logger = logger
|
||||
}
|
||||
|
||||
@@ -59,7 +55,7 @@ public actor MQTTConnectionService: Service {
|
||||
/// connection.
|
||||
public func run() async throws {
|
||||
try await withGracefulShutdownHandler {
|
||||
let stream = try await manager.connect(cleanSession)
|
||||
let stream = try await manager.connect()
|
||||
for await event in stream.cancelOnGracefulShutdown() {
|
||||
// We don't really need to do anything with the events, so just logging
|
||||
// for now. But we need to iterate on an async stream for the service to
|
||||
|
||||
@@ -153,10 +153,6 @@ private actor MQTTTopicListener {
|
||||
}
|
||||
}
|
||||
|
||||
client.addShutdownListener(named: name) { _ in
|
||||
self.shutdown()
|
||||
}
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
@@ -167,6 +163,8 @@ private actor MQTTTopicListener {
|
||||
nonisolated func shutdown() {
|
||||
client.logger.trace("Closing topic listener...")
|
||||
continuation.finish()
|
||||
client.removePublishListener(named: name)
|
||||
client.removeShutdownListener(named: name)
|
||||
Task { await self.setIsShuttingDown() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ struct Application {
|
||||
$0.topicPublisher = .live(client: mqtt)
|
||||
$0.mqttConnectionManager = .live(client: mqtt, logger: logger)
|
||||
} operation: {
|
||||
let mqttConnection = MQTTConnectionService(cleanSession: false, logger: logger)
|
||||
let mqttConnection = MQTTConnectionService(logger: logger)
|
||||
let sensors = SensorsService(sensors: .live, logger: logger)
|
||||
|
||||
var serviceGroupConfiguration = ServiceGroupConfiguration(
|
||||
|
||||
Reference in New Issue
Block a user