From 39766467e6fe4e9ae5df0ccd7ded06cd4b1d0583 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 8 Nov 2023 10:24:50 +0100 Subject: [PATCH] Remove uses of Task --- Sources/App/DeviceManager.swift | 15 ++++++++++----- Sources/App/configure.swift | 26 ++++++++++++++------------ Sources/Run/main.swift | 7 +------ 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Sources/App/DeviceManager.swift b/Sources/App/DeviceManager.swift index b7e4c84..03667ce 100644 --- a/Sources/App/DeviceManager.swift +++ b/Sources/App/DeviceManager.swift @@ -25,6 +25,8 @@ final class DeviceManager { private let messagesToDeviceMetric: Metric + private let scheduler: AsyncScheduler + /// Indicator for device availability var deviceIsConnected: Bool { deviceIsAuthenticated && !(connection?.isClosed ?? true) @@ -33,7 +35,7 @@ final class DeviceManager { /// A promise to finish the request once the device responds or times out private var requestInProgress: EventLoopPromise? - init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64) { + init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64, scheduler: AsyncScheduler) { self.deviceKey = deviceKey self.remoteKey = remoteKey self.deviceTimeout = deviceTimeout @@ -45,17 +47,20 @@ final class DeviceManager { "sesame.messages", name: "Forwarded Messages", description: "The number of messages transmitted to the device") + self.scheduler = scheduler } private func updateDeviceConnectionMetric() { - Task { - try? await deviceConnectedMetric.update(deviceIsConnected) + scheduler.schedule { [weak self] in + guard let self else { return } + _ = try? await deviceConnectedMetric.update(deviceIsConnected) } } private func updateMessageCountMetric() { - Task { - let lastValue = await messagesToDeviceMetric.lastValue()?.value ?? 0 + scheduler.schedule { [weak self] in + guard let self else { return } + let lastValue = await self.messagesToDeviceMetric.lastValue()?.value ?? 0 _ = try? await messagesToDeviceMetric.update(lastValue + 1) } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index b5747b5..a65fdcf 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -14,15 +14,8 @@ enum ServerError: Error { case invalidAuthenticationToken } -private let dateFormatter: DateFormatter = { - let df = DateFormatter() - df.dateStyle = .short - df.timeStyle = .short - return df -}() - // configures your application -public func configure(_ app: Application) async throws { +public func configure(_ app: Application) throws { let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let logFolder = storageFolder.appendingPathComponent("logs") @@ -30,7 +23,9 @@ public func configure(_ app: Application) async throws { MetricObserver.standard = monitor let status = Metric("sesame.status") - _ = try await status.update(.initializing) + asyncScheduler.schedule { + _ = try await status.update(.initializing) + } let configUrl = storageFolder.appendingPathComponent("config.json") let config = try Config(loadFrom: configUrl) @@ -43,7 +38,8 @@ public func configure(_ app: Application) async throws { deviceManager = DeviceManager( deviceKey: deviceKey, remoteKey: remoteKey, - deviceTimeout: config.deviceTimeout) + deviceTimeout: config.deviceTimeout, + scheduler: asyncScheduler) try routes(app) @@ -58,8 +54,14 @@ public func configure(_ app: Application) async throws { } } - _ = try await status.update(.nominal) - print("[\(dateFormatter.string(from: Date()))] Server started") + asyncScheduler.schedule { + _ = try await status.update(.nominal) + } + + let df = DateFormatter() + df.dateStyle = .short + df.timeStyle = .short + print("[\(df.string(from: Date()))] Server started") } private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) { diff --git a/Sources/Run/main.swift b/Sources/Run/main.swift index 4e625fb..5a1383a 100644 --- a/Sources/Run/main.swift +++ b/Sources/Run/main.swift @@ -6,10 +6,5 @@ try LoggingSystem.bootstrap(from: &env) let app = Application(env) defer { app.shutdown() } -private let semaphore = DispatchSemaphore(value: 0) -Task { - try await configure(app) - semaphore.signal() -} -semaphore.wait() +try configure(app) try app.run()