Skip to content

Commit e82d688

Browse files
committed
Update Readme
1 parent 8dfbcdd commit e82d688

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,41 @@ and to your application target, add `ServiceLifecycle` to your dependencies:
2828

2929
### Using ServiceLifecycle
3030

31-
You can find the documentation on how to use ServiceLifecycle over [here](https://swiftpackageindex.com/swift-server/swift-service-lifecycle/main/documentation/lifecycle).
31+
Below is a short usage example however you can find detailed documentation on how to use ServiceLifecycle over [here](https://swiftpackageindex.com/swift-server/swift-service-lifecycle/main/documentation/lifecycle).
3232

33-
Do not hesitate to get in touch as well, over on https://forums.swift.org/c/server.
33+
ServiceLifecycle consists of two main building blocks. First, the `Service` protocol and secondly
34+
the `ServiceGroup`. As a library or application developer you should model your long-running work
35+
as services that implement the `Service` protocol. The protocol only requires a single `func run() async throws`
36+
method to be implemented.
37+
Afterwards, in your application you can use the `ServiceGroup` to orchestrate multiple services.
38+
The group will spawn a child task for each service and call the respective `run` method in the child task.
39+
Furthermore, the group will setup signal listeners for the configured signals and trigger a graceful shutdown
40+
on each service.
41+
42+
```swift
43+
actor FooService {
44+
func run() async throws {
45+
print("FooService starting")
46+
try await Task.sleep(for: .seconds(10))
47+
print("FooService done")
48+
}
49+
}
50+
51+
@main
52+
struct Application {
53+
static func main() async throws {
54+
let service1 = FooService()
55+
let service2 = FooService()
56+
57+
let serviceGroup = ServiceGroup(
58+
services: [service1, service2],
59+
configuration: .init(gracefulShutdownSignals: [.sigterm])
60+
)
61+
try await serviceGroup.run()
62+
}
63+
}
64+
65+
```
3466

3567
## Security
3668

0 commit comments

Comments
 (0)