diff --git a/README.md b/README.md index 4fb54ce..6fdd306 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,9 @@ Furthermore, the group will setup signal listeners for the configured signals an on each service. ```swift +import ServiceLifecycle +import Logging + actor FooService: Service { func run() async throws { print("FooService starting") @@ -72,19 +75,21 @@ actor FooService: Service { @main struct Application { + static let logger = Logger(label: "Application") + static func main() async throws { let service1 = FooService() let service2 = FooService() let serviceGroup = ServiceGroup( services: [service1, service2], - configuration: .init(gracefulShutdownSignals: [.sigterm]), + gracefulShutdownSignals: [.sigterm], logger: logger ) + try await serviceGroup.run() } } - ``` ## Security diff --git a/Sources/ServiceLifecycle/Docs.docc/How to adopt ServiceLifecycle in applications.md b/Sources/ServiceLifecycle/Docs.docc/How to adopt ServiceLifecycle in applications.md index 0364216..7f747e1 100644 --- a/Sources/ServiceLifecycle/Docs.docc/How to adopt ServiceLifecycle in applications.md +++ b/Sources/ServiceLifecycle/Docs.docc/How to adopt ServiceLifecycle in applications.md @@ -69,21 +69,21 @@ on services with a lower index. The following example shows how this can be applied to our `BarService`. ```swift +import ServiceLifecycle +import Logging + @main struct Application { + static let logger = Logger(label: "Application") + static func main() async throws { let fooService = FooServer() let barService = BarService(fooService: fooService) 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], + logger: logger ) try await serviceGroup.run() @@ -115,6 +115,9 @@ A common example of this is for applications that implement streaming behaviours. ```swift +import ServiceLifecycle +import Logging + struct StreamingService: Service { struct RequestStream: AsyncSequence { ... } struct ResponseWriter { @@ -140,6 +143,8 @@ struct StreamingService: Service { @main struct Application { + static let logger = Logger(label: "Application") + static func main() async throws { let streamingService = StreamingService(streamHandler: { requestStream, responseWriter in for await request in requestStream { @@ -148,11 +153,9 @@ struct Application { }) let serviceGroup = ServiceGroup( - configuration: .init( - services: [.init(service: streamingService)], - gracefulShutdownSignals: [.sigterm], - logger: logger - ) + services: [streamingService], + gracefulShutdownSignals: [.sigterm], + logger: logger ) try await serviceGroup.run() @@ -177,6 +180,9 @@ and what we want to do is stop the iteration. To do this we can use the `AsyncSequence`. The updated code looks like this: ```swift +import ServiceLifecycle +import Logging + struct StreamingService: Service { struct RequestStream: AsyncSequence { ... } struct ResponseWriter { @@ -202,6 +208,8 @@ struct StreamingService: Service { @main struct Application { + static let logger = Logger(label: "Application") + static func main() async throws { let streamingService = StreamingService(streamHandler: { requestStream, responseWriter in for await request in requestStream.cancelOnGracefulShutdown() { @@ -210,11 +218,9 @@ struct Application { }) let serviceGroup = ServiceGroup( - configuration: .init( - services: [.init(service: streamingService)], - gracefulShutdownSignals: [.sigterm], - logger: logger - ) + services: [streamingService], + gracefulShutdownSignals: [.sigterm],, + logger: logger ) try await serviceGroup.run() @@ -251,8 +257,13 @@ sure your telemetry service is gracefully shutdown after your HTTP server unexpectedly threw from its `run()` method. This setup could look like this: ```swift +import ServiceLifecycle +import Logging + @main struct Application { + static let logger = Logger(label: "Application") + static func main() async throws { let telemetryService = TelemetryService() let httpServer = HTTPServer() diff --git a/Sources/ServiceLifecycle/ServiceGroup.swift b/Sources/ServiceLifecycle/ServiceGroup.swift index 120983b..853cc6d 100644 --- a/Sources/ServiceLifecycle/ServiceGroup.swift +++ b/Sources/ServiceLifecycle/ServiceGroup.swift @@ -88,7 +88,7 @@ public actor ServiceGroup: Sendable, Service { self.init(configuration: configuration) } - @available(*, deprecated) + @available(*, deprecated, renamed: "init(services:gracefulShutdownSignals:cancellationSignals:logger:)") public init( services: [any Service], configuration: ServiceGroupConfiguration,