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 ServiceLifecycle instance.

    Declaration

    Swift

    public init(configuration: Configuration = .init())

    Parameters

    configuration

    Defines lifecycle Configuration

  • Starts the provided LifecycleTask array. Startup is performed in the order of items provided.

    Declaration

    Swift

    public func start(_ callback: @escaping (Error?) -> Void)

    Parameters

    callback

    The handler which is called after the start operation completes. The parameter will be nil on success and contain the Error otherwise.

  • Starts the provided LifecycleTask array and waits (blocking) until a shutdown Signal is captured or shutdown is called on another thread. Startup is performed in the order of items provided.

    Declaration

    Swift

    public func startAndWait() throws
  • Shuts down the LifecycleTask array provided in start or startAndWait. Shutdown is performed in reverse order of items provided.

    Declaration

    Swift

    public func shutdown(_ callback: @escaping (Error?) -> Void = { _ in })

    Parameters

    callback

    The handler which is called after the start operation completes. The parameter will be nil on success and contain the Error otherwise.

  • Waits (blocking) until shutdown Signal is captured or shutdown is invoked on another thread.

    Declaration

    Swift

    public func wait()

ServiceLifecycle

  • Setup a signal trap.

    Declaration

    Swift

    public static func trap(signal sig: Signal, handler: @escaping (Signal) -> Void, on queue: DispatchQueue = .global(), cancelAfterTrap: Bool = false) -> DispatchSourceSignal

    Parameters

    signal

    The signal to trap.

    handler

    closure to invoke when the signal is captured.

    on

    DispatchQueue to run the signal handler on (default global dispatch queue)

    cancelAfterTrap

    Defaults 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 DispatchSourceSignal for the given trap. The source must be cancelled by the caller.

  • A system signal

    See more

    Declaration

    Swift

    public struct Signal : Equatable, CustomStringConvertible
  • Declaration

    Swift

    public func register(_ tasks: [LifecycleTask])
  • ServiceLifecycle configuration options.

    See more

    Declaration

    Swift

    public struct Configuration