-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12e73e7
commit 5b02277
Showing
15 changed files
with
174 additions
and
79 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import ServiceLifecycle | ||
|
||
final class Lifecycle: ServiceLifecycle.Service { | ||
fileprivate var startTasks: [() async throws -> Void] = [] | ||
fileprivate var shutdownTasks: [() async throws -> Void] = [] | ||
|
||
var didStart = false | ||
var didStop = false | ||
|
||
func run() async throws { | ||
try await start() | ||
try await gracefulShutdown() | ||
try await shutdown() | ||
} | ||
|
||
func start() async throws { | ||
guard !didStart else { return } | ||
didStart = true | ||
for start in startTasks { | ||
try await start() | ||
} | ||
} | ||
|
||
func shutdown() async throws { | ||
guard !didStop else { return } | ||
didStop = true | ||
for shutdown in shutdownTasks.reversed() { | ||
try await shutdown() | ||
} | ||
} | ||
} | ||
|
||
extension Application { | ||
var lifecycle: Lifecycle { | ||
container.require() | ||
} | ||
} | ||
|
||
extension Container { | ||
static var lifecycle: Lifecycle { | ||
require() | ||
} | ||
|
||
public static func onStart(action: @escaping () async throws -> Void) { | ||
lifecycle.startTasks.append(action) | ||
} | ||
|
||
public static func onShutdown(action: @escaping () async throws -> Void) { | ||
lifecycle.shutdownTasks.append(action) | ||
} | ||
} |
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
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
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.