-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Async Lifecycle Handlers (#3193)
* Add async functions on Lifecycle handler * Hook up async shutdown * Hook up the rest of the lifecycle handler * Hookup async stuff * Add some docs for Lifecycle handlers * Add async tests for lifecycle handler * Fix the tests * Clarify some of the docs * Try and reduce a flaky test * Fix the tests * Redisable test as its extremely flaky
- Loading branch information
Showing
8 changed files
with
248 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,74 @@ | ||
/// Provides a way to hook into lifecycle events of a Vapor application. You can register | ||
/// your handlers with the ``Application`` to be notified when the application | ||
/// is about to start up, has started up and is about to shutdown | ||
/// | ||
/// For example | ||
/// ```swift | ||
/// struct LifecycleLogger: LifecycleHander { | ||
/// func willBootAsync(_ application: Application) async throws { | ||
/// application.logger.info("Application about to boot up") | ||
/// } | ||
/// | ||
/// func didBootAsync(_ application: Application) async throws { | ||
/// application.logger.info("Application has booted up") | ||
/// } | ||
/// | ||
/// func shutdownAsync(_ application: Application) async { | ||
/// application.logger.info("Will shutdown") | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// You can then register your handler with the application: | ||
/// | ||
/// ```swift | ||
/// application.lifecycle.use(LifecycleLogger()) | ||
/// ``` | ||
/// | ||
public protocol LifecycleHandler: Sendable { | ||
/// Called when the application is about to boot up | ||
func willBoot(_ application: Application) throws | ||
/// Called when the application has booted up | ||
func didBoot(_ application: Application) throws | ||
/// Called when the application is about to shutdown | ||
func shutdown(_ application: Application) | ||
/// Called when the application is about to boot up. This is the asynchronous version | ||
/// of ``willBoot(_:)-9zn``. When adopting the async APIs you should ensure you | ||
/// provide a compatitble implementation for ``willBoot(_:)-8anu6`` as well if you | ||
/// want to support older users still running in a non-async context | ||
/// **Note** your application must be running in an asynchronous context and initialised with | ||
/// ``Application/make(_:_:)`` for this handler to be called | ||
func willBootAsync(_ application: Application) async throws | ||
/// Called when the application is about to boot up. This is the asynchronous version | ||
/// of ``didBoot(_:)-wfef``. When adopting the async APIs you should ensure you | ||
/// provide a compatitble implementation for ``didBoot(_:)-wfef`` as well if you | ||
/// want to support older users still running in a non-async context | ||
/// **Note** your application must be running in an asynchronous context and initialised with | ||
/// ``Application/make(_:_:)`` for this handler to be called | ||
func didBootAsync(_ application: Application) async throws | ||
/// Called when the application is about to boot up. This is the asynchronous version | ||
/// of ``shutdown(_:)-2clwm``. When adopting the async APIs you should ensure you | ||
/// provide a compatitble implementation for ``shutdown(_:)-2clwm`` as well if you | ||
/// want to support older users still running in a non-async context | ||
/// **Note** your application must be running in an asynchronous context and initialised with | ||
/// ``Application/make(_:_:)`` for this handler to be called | ||
func shutdownAsync(_ application: Application) async | ||
} | ||
|
||
extension LifecycleHandler { | ||
public func willBoot(_ application: Application) throws { } | ||
public func didBoot(_ application: Application) throws { } | ||
public func shutdown(_ application: Application) { } | ||
|
||
public func willBootAsync(_ application: Application) async throws { | ||
try self.willBoot(application) | ||
} | ||
|
||
public func didBootAsync(_ application: Application) async throws { | ||
try self.didBoot(application) | ||
} | ||
|
||
public func shutdownAsync(_ application: Application) async { | ||
self.shutdown(application) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.