Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async APIs for shutting down the databases #614

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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