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 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<Data>?
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)
}
}

View File

@ -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<ServerStatus>("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) {

View File

@ -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()