Skip to content

withCancellationOrGracefulShutdownHandler #175

Closed
@adam-fowler

Description

@adam-fowler

A common pattern with services is

try await withTaskCancellationHandler {
    try await withGracefulShutdownHandler {
        doStuff()
    } onGracefulShutdown {
        stop()
    }
} onCancel: {
    stop()
}

Is it worthwhile providing a small helper API to do this eg

@_unsafeInheritExecutor
public func withCancellationOrGracefulShutdownHandler<T>(
    operation: () async throws -> T,
    onCancellationOrGracefulShutdown handler: @Sendable @escaping () -> Void
) async rethrows -> T {
    return try await withTaskCancellationHandler {
        try await withGracefulShutdownHandler(operation: operation, onGracefulShutdown: handler)
    } onCancel: {
        handler()
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions