Compare commits
No commits in common. "18fd850413ca95d0e37baf842bf06c48388e5333" and "ab2a14e00b0c5cc042d220ed10fb7ef59bd18035" have entirely different histories.
18fd850413
...
ab2a14e00b
@ -39,21 +39,21 @@ extension Config {
|
|||||||
|
|
||||||
init(loadFrom url: URL) throws {
|
init(loadFrom url: URL) throws {
|
||||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||||
print("No configuration file found at \(url.path)")
|
log("No configuration file found at \(url.path)")
|
||||||
fatalError("No configuration file found")
|
fatalError("No configuration file found")
|
||||||
}
|
}
|
||||||
let data: Data
|
let data: Data
|
||||||
do {
|
do {
|
||||||
data = try Data(contentsOf: url)
|
data = try Data(contentsOf: url)
|
||||||
} catch {
|
} catch {
|
||||||
print("Failed to read config data: \(error)")
|
log("Failed to read config data: \(error)")
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
self = try JSONDecoder().decode(Config.self, from: data)
|
self = try JSONDecoder().decode(Config.self, from: data)
|
||||||
} catch {
|
} catch {
|
||||||
print("Failed to decode config data: \(error)")
|
log("Failed to decode config data: \(error)")
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import ClairvoyantBinaryCodable
|
|||||||
var deviceManager: DeviceManager!
|
var deviceManager: DeviceManager!
|
||||||
|
|
||||||
private var provider: VaporMetricProvider!
|
private var provider: VaporMetricProvider!
|
||||||
private var status: Metric<ServerStatus>!
|
|
||||||
|
|
||||||
private var asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
private var asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
||||||
|
|
||||||
@ -22,16 +21,6 @@ enum ServerError: Error {
|
|||||||
case invalidAuthenticationToken
|
case invalidAuthenticationToken
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateStatus(_ newStatus: ServerStatus) {
|
|
||||||
asyncScheduler.schedule {
|
|
||||||
do {
|
|
||||||
try await status.update(newStatus)
|
|
||||||
} catch {
|
|
||||||
print("Failed to update server status: \(error)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// configures your application
|
// configures your application
|
||||||
public func configure(_ app: Application) throws {
|
public func configure(_ app: Application) throws {
|
||||||
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
@ -44,8 +33,10 @@ public func configure(_ app: Application) throws {
|
|||||||
let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log")
|
let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log")
|
||||||
MetricObserver.standard = monitor
|
MetricObserver.standard = monitor
|
||||||
|
|
||||||
status = Metric<ServerStatus>("sesame.status")
|
let status = Metric<ServerStatus>("sesame.status")
|
||||||
updateStatus(.initializing)
|
asyncScheduler.schedule {
|
||||||
|
_ = try await status.update(.initializing)
|
||||||
|
}
|
||||||
|
|
||||||
app.http.server.configuration.port = config.port
|
app.http.server.configuration.port = config.port
|
||||||
|
|
||||||
@ -61,27 +52,27 @@ public func configure(_ app: Application) throws {
|
|||||||
provider.registerRoutes(app)
|
provider.registerRoutes(app)
|
||||||
monitor.saveCurrentListOfMetricsToLogFolder()
|
monitor.saveCurrentListOfMetricsToLogFolder()
|
||||||
|
|
||||||
updateStatus(.nominal)
|
|
||||||
|
|
||||||
// Update the metric of the device status to ensure that it is accurate
|
|
||||||
asyncScheduler.schedule {
|
asyncScheduler.schedule {
|
||||||
|
_ = try await status.update(.nominal)
|
||||||
await deviceManager.updateDeviceConnectionMetric()
|
await deviceManager.updateDeviceConnectionMetric()
|
||||||
}
|
}
|
||||||
|
|
||||||
log("[\(df.string(from: Date()))] Server started")
|
log("[\(df.string(from: Date()))] Server started")
|
||||||
|
|
||||||
|
// Gracefully shut down by closing potentially open socket
|
||||||
|
// Must be done after app is running, otherwise error is thrown
|
||||||
|
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
|
||||||
|
_ = app.server.onShutdown.always { _ in
|
||||||
|
print("[\(df.string(from: Date()))] Server shutdown")
|
||||||
|
asyncScheduler.schedule {
|
||||||
|
await deviceManager.removeDeviceConnection()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func shutdown() {
|
public func shutdown() {
|
||||||
print("[\(df.string(from: Date()))] Server shutdown")
|
try? asyncScheduler.syncShutdownGracefully()
|
||||||
asyncScheduler.schedule {
|
|
||||||
// Gracefully shut down by closing potentially open socket
|
|
||||||
await deviceManager.removeDeviceConnection()
|
|
||||||
do {
|
|
||||||
try await asyncScheduler.shutdownGracefully()
|
|
||||||
} catch {
|
|
||||||
print("Failed to shut down MultiThreadedEventLoopGroup: \(error)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {
|
private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {
|
||||||
|
Loading…
Reference in New Issue
Block a user