Remove uses of Task

This commit is contained in:
Christoph Hagen 2023-11-08 10:24:50 +01:00
parent 037d146aba
commit 39766467e6
3 changed files with 25 additions and 23 deletions

View File

@ -25,6 +25,8 @@ final class DeviceManager {
private let messagesToDeviceMetric: Metric<Int> private let messagesToDeviceMetric: Metric<Int>
private let scheduler: AsyncScheduler
/// Indicator for device availability /// Indicator for device availability
var deviceIsConnected: Bool { var deviceIsConnected: Bool {
deviceIsAuthenticated && !(connection?.isClosed ?? true) deviceIsAuthenticated && !(connection?.isClosed ?? true)
@ -33,7 +35,7 @@ final class DeviceManager {
/// A promise to finish the request once the device responds or times out /// A promise to finish the request once the device responds or times out
private var requestInProgress: EventLoopPromise<Data>? private var requestInProgress: EventLoopPromise<Data>?
init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64) { init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64, scheduler: AsyncScheduler) {
self.deviceKey = deviceKey self.deviceKey = deviceKey
self.remoteKey = remoteKey self.remoteKey = remoteKey
self.deviceTimeout = deviceTimeout self.deviceTimeout = deviceTimeout
@ -45,17 +47,20 @@ final class DeviceManager {
"sesame.messages", "sesame.messages",
name: "Forwarded Messages", name: "Forwarded Messages",
description: "The number of messages transmitted to the device") description: "The number of messages transmitted to the device")
self.scheduler = scheduler
} }
private func updateDeviceConnectionMetric() { private func updateDeviceConnectionMetric() {
Task { scheduler.schedule { [weak self] in
try? await deviceConnectedMetric.update(deviceIsConnected) guard let self else { return }
_ = try? await deviceConnectedMetric.update(deviceIsConnected)
} }
} }
private func updateMessageCountMetric() { private func updateMessageCountMetric() {
Task { scheduler.schedule { [weak self] in
let lastValue = await messagesToDeviceMetric.lastValue()?.value ?? 0 guard let self else { return }
let lastValue = await self.messagesToDeviceMetric.lastValue()?.value ?? 0
_ = try? await messagesToDeviceMetric.update(lastValue + 1) _ = try? await messagesToDeviceMetric.update(lastValue + 1)
} }
} }

View File

@ -14,15 +14,8 @@ enum ServerError: Error {
case invalidAuthenticationToken case invalidAuthenticationToken
} }
private let dateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
return df
}()
// configures your application // configures your application
public func configure(_ app: Application) async throws { public func configure(_ app: Application) throws {
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
let logFolder = storageFolder.appendingPathComponent("logs") let logFolder = storageFolder.appendingPathComponent("logs")
@ -30,7 +23,9 @@ public func configure(_ app: Application) async throws {
MetricObserver.standard = monitor MetricObserver.standard = monitor
let status = Metric<ServerStatus>("sesame.status") let status = Metric<ServerStatus>("sesame.status")
_ = try await status.update(.initializing) asyncScheduler.schedule {
_ = try await status.update(.initializing)
}
let configUrl = storageFolder.appendingPathComponent("config.json") let configUrl = storageFolder.appendingPathComponent("config.json")
let config = try Config(loadFrom: configUrl) let config = try Config(loadFrom: configUrl)
@ -43,7 +38,8 @@ public func configure(_ app: Application) async throws {
deviceManager = DeviceManager( deviceManager = DeviceManager(
deviceKey: deviceKey, deviceKey: deviceKey,
remoteKey: remoteKey, remoteKey: remoteKey,
deviceTimeout: config.deviceTimeout) deviceTimeout: config.deviceTimeout,
scheduler: asyncScheduler)
try routes(app) try routes(app)
@ -58,8 +54,14 @@ public func configure(_ app: Application) async throws {
} }
} }
_ = try await status.update(.nominal) asyncScheduler.schedule {
print("[\(dateFormatter.string(from: Date()))] Server started") _ = 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) { private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {

View File

@ -6,10 +6,5 @@ try LoggingSystem.bootstrap(from: &env)
let app = Application(env) let app = Application(env)
defer { app.shutdown() } defer { app.shutdown() }
private let semaphore = DispatchSemaphore(value: 0) try configure(app)
Task {
try await configure(app)
semaphore.signal()
}
semaphore.wait()
try app.run() try app.run()