Skip to content

Commit

Permalink
Make logging optional and update docs (#189)
Browse files Browse the repository at this point in the history
Hello! 👋 This PR contains a few changes, notably:

- Updating the ``README.md`` to use the new initializer instead of the
deprecated one.
- Add a ``renamed`` to the deprecated initializer.
- Updating the docs to use the simpler initializer.
- Make the ``logging`` parameter on the initializers optional, the
motivation for this is in cases the user don't or can't do logging, they
don't have to construct a logger and bootstrap the logging system with
``SwiftLogNoOpLogHandler``, so this code:
```swift
LoggingSystem.bootstrap(SwiftLogNoOpLogHandler.init)
let serviceGroup = ServiceGroup(
    services: [service],
    cancellationSignals: [.sigint],
    logger: .init(label: "")
)
```
becomes
```swift
let serviceGroup = ServiceGroup(
    services: [service],
    cancellationSignals: [.sigint]
)
```
and it also allows the user to do logging outside the ServiceGroup
without necessarily doing logging within it.

If there is anything to improve, let me know! 😃

*thanks dimi and gwynne for helping along the way and answering my
questions :)*

---------

Co-authored-by: Franz Busch <[email protected]>
  • Loading branch information
nervenes and FranzBusch authored Dec 5, 2024
1 parent 0ee8bad commit 0af4033
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Sources/ServiceLifecycle/ServiceGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0af4033

Please sign in to comment.