Skip to content

Commit

Permalink
make logging optional and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nervenes committed Nov 22, 2024
1 parent f70b838 commit f188481
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Furthermore, the group will setup signal listeners for the configured signals an
on each service.

```swift
import ServiceLifecycle

actor FooService: Service {
func run() async throws {
print("FooService starting")
Expand All @@ -78,13 +80,12 @@ struct Application {

let serviceGroup = ServiceGroup(
services: [service1, service2],
configuration: .init(gracefulShutdownSignals: [.sigterm]),
logger: logger
gracefulShutdownSignals: [.sigterm]
)

try await serviceGroup.run()
}
}

```

## Security
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ struct Application {

let serviceGroup = ServiceGroup(
// We are encoding the dependency hierarchy here by listing the fooService first
configuration: .init(
services: [
.init(service: fooService),
.init(service: barService)
],
logger: logger
),
services: [fooService, barService]
)

try await serviceGroup.run()
Expand Down Expand Up @@ -148,11 +142,8 @@ struct Application {
})

let serviceGroup = ServiceGroup(
configuration: .init(
services: [.init(service: streamingService)],
gracefulShutdownSignals: [.sigterm],
logger: logger
)
services: [streamingService],
gracefulShutdownSignals: [.sigterm]
)

try await serviceGroup.run()
Expand Down Expand Up @@ -210,11 +201,8 @@ struct Application {
})

let serviceGroup = ServiceGroup(
configuration: .init(
services: [.init(service: streamingService)],
gracefulShutdownSignals: [.sigterm],
logger: logger
)
services: [streamingService],
gracefulShutdownSignals: [.sigterm]
)

try await serviceGroup.run()
Expand Down Expand Up @@ -266,8 +254,7 @@ struct Application {
successTerminationBehavior: .shutdownGracefully,
failureTerminationBehavior: .shutdownGracefully
)
],
logger: logger
]
),
)

Expand Down
10 changes: 7 additions & 3 deletions Sources/ServiceLifecycle/ServiceGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,23 @@ public actor ServiceGroup: Sendable, Service {
services: [any Service],
gracefulShutdownSignals: [UnixSignal] = [],
cancellationSignals: [UnixSignal] = [],
logger: Logger
logger: Logger? = nil
) {
if logger == nil {
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
}

let configuration = ServiceGroupConfiguration(
services: services.map { ServiceGroupConfiguration.ServiceConfiguration(service: $0) },
gracefulShutdownSignals: gracefulShutdownSignals,
cancellationSignals: cancellationSignals,
logger: logger
logger: logger ?? .init(label: "")
)

self.init(configuration: configuration)
}

@available(*, deprecated)
@available(*, deprecated, renamed: "init(services:gracefulShutdownSignals:cancellationSignals:logger:)")
public init(
services: [any Service],
configuration: ServiceGroupConfiguration,
Expand Down
32 changes: 24 additions & 8 deletions Sources/ServiceLifecycle/ServiceGroupConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ public struct ServiceGroupConfiguration: Sendable {
/// - logger: The group's logger.
public init(
services: [ServiceConfiguration],
logger: Logger
logger: Logger? = nil
) {
if logger == nil {
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
}

self.services = services
self.logger = logger
self.logger = logger ?? .init(label: "")
}

/// Initializes a new ``ServiceGroupConfiguration``.
Expand All @@ -189,10 +193,14 @@ public struct ServiceGroupConfiguration: Sendable {
services: [ServiceConfiguration],
gracefulShutdownSignals: [UnixSignal] = [],
cancellationSignals: [UnixSignal] = [],
logger: Logger
logger: Logger? = nil
) {
if logger == nil {
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
}

self.services = services
self.logger = logger
self.logger = logger ?? .init(label: "")
self.gracefulShutdownSignals = gracefulShutdownSignals
self.cancellationSignals = cancellationSignals
}
Expand All @@ -204,10 +212,14 @@ public struct ServiceGroupConfiguration: Sendable {
/// - logger: The group's logger.
public init(
services: [Service],
logger: Logger
logger: Logger? = nil
) {
if logger == nil {
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
}

self.services = Array(services.map { ServiceConfiguration(service: $0) })
self.logger = logger
self.logger = logger ?? .init(label: "")
}

/// Initializes a new ``ServiceGroupConfiguration``.
Expand All @@ -221,10 +233,14 @@ public struct ServiceGroupConfiguration: Sendable {
services: [Service],
gracefulShutdownSignals: [UnixSignal] = [],
cancellationSignals: [UnixSignal] = [],
logger: Logger
logger: Logger? = nil
) {
if logger == nil {
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
}

self.services = Array(services.map { ServiceConfiguration(service: $0) })
self.logger = logger
self.logger = logger ?? .init(label: "")
self.gracefulShutdownSignals = gracefulShutdownSignals
self.cancellationSignals = cancellationSignals
}
Expand Down

0 comments on commit f188481

Please sign in to comment.