Skip to content

Commit

Permalink
Merge pull request #4 from lemo-nade-room/feature/fix-threadpool
Browse files Browse the repository at this point in the history
fix: 🐛 ThreadPoolのシャットダウン
  • Loading branch information
lemo-nade-room authored Sep 8, 2024
2 parents 0daec90 + 5a3e5a0 commit 2a807c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Sources/CLIKit/CLIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public struct CLIKitService: Sendable {
} catch let error {
console.error("\(error)")
}
await context.shutdown()
do {
try await context.shutdown()
} catch let error {
console.error("\(error)")
}
}

/// AsyncCommandを登録する
Expand Down
12 changes: 10 additions & 2 deletions Sources/CLIKit/CommandContext+initDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ extension CommandContext {
}
}

public var threadPool: NIOThreadPool? {
get async {
await storage.get(NIOThreadPool.self)
}
}

func initDatabase(sqliteURL: URL, migrations: Migrations, migrationLogLevel: Logger.Level) async throws {
let logger = await logger
let eventLoopGroup = await eventLoopGroup
Expand All @@ -54,7 +60,6 @@ extension CommandContext {
let databases = Databases(threadPool: threadPool, on: eventLoopGroup)
databases.use(.sqlite(.file(sqliteURL.absoluteString)), as: .sqlite)
await storage.set(key: Logger.self, logger)
await storage.set(key: MultiThreadedEventLoopGroup.self, eventLoopGroup)
await storage.set(key: NIOThreadPool.self, threadPool)
await storage.set(key: Databases.self, databases)

Expand Down Expand Up @@ -88,9 +93,12 @@ extension CommandContext {
}
}

func shutdown() async {
func shutdown() async throws {
if let databases = await databases {
await databases.shutdownAsync()
}
if let threadPool = await threadPool {
try await threadPool.shutdownGracefully()
}
}
}

0 comments on commit 2a807c7

Please sign in to comment.