* Overhaul service lifecycle for Structured Concurrency
# Motivation
Since the release of Swift Concurrency the server ecosystem has adopted `async`/`await` in a lot of APIs making the user experience amazing. Another thing that Swift Concurrency introduced is Structured Concurrency which allows to create a task tree that with parent to child relations. Structured Concurrency has many benefits such as automatic cancellation and task local propagation.
The current state of this library predates the introduction of Swift Concurrency and is not inline anymore with how applications are structured that want to leverage Concurrency.
# Modification
This PR overhauls the implementation of service lifecycle completely. The reason for this is that the current implementation is very focused on how NIO based applications work before the introduction of Concurrency. Furthermore, Structured Concurrency is actually replacing part of the current functionality by providing new scoping mechanisms.
The overhauled implementation provides two primitive types. Anew `Service` protocol and a `ServiceRunner`. The former is providing a clear API which services have to conform to so that they can be run by the `ServiceRunner`.
An example usage of the types looks like this
```swift
actor FooService: Service {
func run() async throws {}
func shutdownGracefully() async throws {}
}
actor BarService: Service {
func run() async throws {}
func shutdownGracefully() async throws {}
}
let fooService = FooService()
let barService = BarService()
let runner = ServiceRunner(
services: [fooService, barService],
configuration: .init(),
logger: logger
)
try await runner.run()
```
# Result
We now have a service lifecycle library that integrates nicely with Structured Concurrency. Its value add is that it solves to complex setup of the task group with the signal handling. Furthermore, it provides a currency type `Service` that can be passed to inject services.
* Remove `shutdownGracefully` method and switch to a handler based approach.
* Code review from George
* Add extensions to `TaskGroup` and `ThrowingTaskGroup`
* Remove long running property
* Documentation
* Fix tests
* Ensure ordering of shutdown
* Test
# Motivation
# Modification
# Result
* Introduce ServiceLifecycleTestKit
* Add AsyncCancelOngracefulShutdownSequence
* Tests and docs for graceful shutdown
# Motivation
# Modification
# Result
* Remove unsafe flags
* Add article for application authors
* Add correct licence and notice information for the things we copied from swift-nio
* Code review
* Remove @_unsafeInheritExectuor
* Code review
* Change log levels, rename to withGracefulShutdownHandler and support nested service runner graceful shutdown
* Add public `shutdownGracefully` method to the service runner
* Code review and expose new cancelOnGracefulShutdown method
* Extend graceful shutdown APIs and tolerate running outside of a `ServiceRunner`
* Rename to `ServiceGroup`
* Enable Swift 5.6 support
* Update soundess dockerfile
* Update Readme
* Make 5.6 build work
Swift Service Lifecycle
Swift Service Lifecycle provides a basic mechanism to cleanly start up and shut down the application, freeing resources in order before exiting.
It also provides a Signal-based shutdown hook, to shut down on signals like TERM or INT.
Swift Service Lifecycle was designed with the idea that every application has some startup and shutdown workflow-like-logic which is often sensitive to failure and hard to get right. The library codes this common need in a safe and reusable way that is non-framework specific, and designed to be integrated with any server framework or directly in an application. Furthermore, it integrates natively with Structured Concurrency.
This is the beginning of a community-driven open-source project actively seeking contributions, be it code, documentation, or ideas. What Swift Service Lifecycle provides today is covered in the API docs, but it will continue to evolve with community input.
Getting started
If you have a server-side Swift application or a cross-platform (e.g. Linux, macOS) application, and you would like to manage its startup and shutdown lifecycle, Swift Service Lifecycle is a great idea. Below you will find all you need to know to get started.
Adding the dependency
To add a dependency on the package, declare it in your Package.swift:
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "1.0.0-alpha.2"),
and to your application target, add ServiceLifecycle to your dependencies:
.target(name: "MyApplication", dependencies: [.product(name: "ServiceLifecycle", package: "swift-service-lifecycle")]),
Using ServiceLifecycle
Below is a short usage example however you can find detailed documentation on how to use ServiceLifecycle over here.
ServiceLifecycle consists of two main building blocks. First, the Service protocol and secondly
the ServiceGroup. As a library or application developer you should model your long-running work
as services that implement the Service protocol. The protocol only requires a single func run() async throws
method to be implemented.
Afterwards, in your application you can use the ServiceGroup to orchestrate multiple services.
The group will spawn a child task for each service and call the respective run method in the child task.
Furthermore, the group will setup signal listeners for the configured signals and trigger a graceful shutdown
on each service.
actor FooService {
func run() async throws {
print("FooService starting")
try await Task.sleep(for: .seconds(10))
print("FooService done")
}
}
@main
struct Application {
static func main() async throws {
let service1 = FooService()
let service2 = FooService()
let serviceGroup = ServiceGroup(
services: [service1, service2],
configuration: .init(gracefulShutdownSignals: [.sigterm])
)
try await serviceGroup.run()
}
}
Security
Please see SECURITY.md for details on the security process.