ServiceLifecycle
public struct ServiceLifecycle
extension ServiceLifecycle: LifecycleTasksContainer
ServiceLifecycle provides a basic mechanism to cleanly startup and shutdown the application, freeing resources in order before exiting.
By default, also install shutdown hooks based on Signal and backtraces.
-
Creates a
ServiceLifecycleinstance.Declaration
Swift
public init(configuration: Configuration = .init())Parameters
configurationDefines lifecycle
Configuration -
Starts the provided
LifecycleTaskarray. Startup is performed in the order of items provided.Declaration
Swift
public func start(_ callback: @escaping (Error?) -> Void)Parameters
callbackThe handler which is called after the start operation completes. The parameter will be
nilon success and contain theErrorotherwise. -
Starts the provided
LifecycleTaskarray and waits (blocking) until a shutdownSignalis captured orshutdownis called on another thread. Startup is performed in the order of items provided.Declaration
Swift
public func startAndWait() throws -
Shuts down the
LifecycleTaskarray provided instartorstartAndWait. Shutdown is performed in reverse order of items provided.Declaration
Swift
public func shutdown(_ callback: @escaping (Error?) -> Void = { _ in })Parameters
callbackThe handler which is called after the start operation completes. The parameter will be
nilon success and contain theErrorotherwise. -
Waits (blocking) until shutdown
Signalis captured orshutdownis invoked on another thread.Declaration
Swift
public func wait()
-
Setup a signal trap.
Declaration
Parameters
signalThe signal to trap.
handlerclosure to invoke when the signal is captured.
onDispatchQueue to run the signal handler on (default global dispatch queue)
cancelAfterTrapDefaults to false, which means the signal handler can be run multiple times. If true, the DispatchSignalSource will be cancelled after being trapped once.
Return Value
a
DispatchSourceSignalfor the given trap. The source must be cancelled by the caller. -
A system signal
See moreDeclaration
Swift
public struct Signal : Equatable, CustomStringConvertible -
Declaration
Swift
public func register(_ tasks: [LifecycleTask]) -
See moreServiceLifecycleconfiguration options.Declaration
Swift
public struct Configuration
View on GitHub
Install in Dash
ServiceLifecycle Structure Reference