Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ By default the ``ServiceGroup`` is cancelling the whole group if the one service
returns or throws. However, in some scenarios this is totally expected e.g. when
the ``ServiceGroup`` is used in a CLI tool to orchestrate some services while a
command is handled. To customize the behavior you set the
``ServiceGroupConfiguration/ServiceConfiguration/returnBehaviour`` and
``ServiceGroupConfiguration/ServiceConfiguration/throwBehaviour``. Both of them
``ServiceGroupConfiguration/ServiceConfiguration/successTerminationBehavior`` and
``ServiceGroupConfiguration/ServiceConfiguration/failureTerminationBehavior``. Both of them
offer three different options. The default behavior for both is
``ServiceGroupConfiguration/ServiceConfiguration/TerminationBehavior/cancelGroup``.
You can also choose to either ignore if a service returns/throws by setting it
Expand All @@ -261,7 +261,11 @@ struct Application {
configuration: .init(
services: [
.init(service: telemetryService),
.init(service: httpServer, returnBehavior: .shutdownGracefully, throwBehavior: .shutdownGracefully)
.init(
service: httpServer,
successTerminationBehavior: .shutdownGracefully,
failureTerminationBehavior: .shutdownGracefully
)
],
logger: logger
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public actor TCPEchoClient: Service {

Since the `run()` method contains long running work, returning from it is seen
as a failure and will lead to the ``ServiceGroup`` cancelling all other services
by cancelling the task that is running their respective `run()` method.
by cancelling the task that is running their respective `run()` method, unless
specified otherwise in ``ServiceGroupConfiguration/ServiceConfiguration/successTerminationBehavior``
and ``ServiceGroupConfiguration/ServiceConfiguration/failureTerminationBehavior``.

### Cancellation

Expand Down
6 changes: 4 additions & 2 deletions Sources/ServiceLifecycle/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public protocol Service: Sendable {
/// - Handling incoming connections and requests
/// - Background refreshes
///
/// - Important: Returning or throwing from this method is indicating a failure of the service and will cause the ``ServiceGroup``
/// to cancel the child tasks of all other running services.
/// - Important: Returning or throwing from this method indicates the service should stop and will cause the
/// ``ServiceGroup`` to follow behaviors for the child tasks of all other running services specified in
/// ``ServiceGroupConfiguration/ServiceConfiguration/successTerminationBehavior`` and
/// ``ServiceGroupConfiguration/ServiceConfiguration/failureTerminationBehavior``.
func run() async throws
}
13 changes: 7 additions & 6 deletions Sources/ServiceLifecycle/ServiceGroupConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public struct ServiceGroupConfiguration: Sendable {
}

public struct ServiceConfiguration: Sendable {
/// The behavior to follow when the service finishes its ``Service/run()`` method via returning or throwing.
public struct TerminationBehavior: Sendable, CustomStringConvertible {
internal enum _TerminationBehavior {
case cancelGroup
Expand All @@ -72,19 +73,19 @@ public struct ServiceGroupConfiguration: Sendable {
}
}

/// The service.
/// The service to which the initialized configuration applies.
public var service: any Service
/// The behavior when the service returns from its `run()` method.
/// The behavior when the service returns from its ``Service/run()`` method.
public var successTerminationBehavior: TerminationBehavior
/// The behavior when the service throws from its `run()` method.
/// The behavior when the service throws from its ``Service/run()`` method.
public var failureTerminationBehavior: TerminationBehavior

/// Initializes a new ``ServiceGroupConfiguration/ServiceConfiguration``.
///
/// - Parameters:
/// - service: The service.
/// - successTerminationBehavior: The behavior when the service returns from its `run()` method.
/// - failureTerminationBehavior: The behavior when the service throws from its `run()` method.
/// - service: The service to which the initialized configuration applies.
/// - successTerminationBehavior: The behavior when the service returns from its ``Service/run()`` method.
/// - failureTerminationBehavior: The behavior when the service throws from its ``Service/run()`` method.
public init(
service: any Service,
successTerminationBehavior: TerminationBehavior = .cancelGroup,
Expand Down