Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make logging optional and update docs #189

Merged
merged 7 commits into from
Dec 5, 2024
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
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
Loading