Skip to content

Commit

Permalink
Add async APIs for shutting down the databases (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTim authored Aug 11, 2024
1 parent cff1c3e commit 614d3ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/FluentKit/Database/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ extension Database {
public protocol DatabaseDriver: Sendable {
func makeDatabase(with context: DatabaseContext) -> any Database
func shutdown()
func shutdownAsync() async
}

public extension DatabaseDriver {
func shutdownAsync() async {
shutdown()
}
}

public protocol DatabaseConfiguration: Sendable {
Expand Down
15 changes: 15 additions & 0 deletions Sources/FluentKit/Database/Databases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public final class Databases: @unchecked Sendable { // @unchecked is safe here;
self.lock.withLock { Set(self.configurations.keys) }
}

@available(*, noasync, message: "Drivers may call wait() and should not be used in an async context", renamed: "shutdownAsync()")
public func shutdown() {
self.lock.withLockVoid {
for driver in self.drivers.values {
Expand All @@ -149,6 +150,20 @@ public final class Databases: @unchecked Sendable { // @unchecked is safe here;
self.drivers = [:]
}
}

public func shutdownAsync() async {
var driversToShutdown: [any DatabaseDriver] = []

self.lock.withLockVoid {
for driver in self.drivers.values {
driversToShutdown.append(driver)
}
self.drivers = [:]
}
for driver in driversToShutdown {
await driver.shutdownAsync()
}
}

private func _requireConfiguration(for id: DatabaseID) -> any DatabaseConfiguration {
guard let configuration = self.configurations[id] else {
Expand Down

0 comments on commit 614d3ec

Please # to comment.