28 Commits
Author SHA1 Message Date
Franz Busch f70b838872 [CI] Adopt GitHub actions (#187)
# Motivation

We want to switch our CI to GitHub actions.

# Modification

This PR sets up the reusable workflows from NIO.

# Result

More modern CI.
2024-10-31 12:17:45 +01:00
Peter Adams 05d3852a55 Stop using renamed pattern which can cause compiler crash (#183)
Motivation:

Deprecation with rename fixit has been seen to cause compiler crashes
in 5.x compiler versions.

Modifications:

Deprecate with message for 5.x compilers.

Result:

Compilation without failure in all scenarios
2024-05-30 10:52:32 +01:00
Iceman 877d7494e2 Global cancelOnGracefulShutdown returns non nil value (#181)
* Fix global cancelOnGracefulShutdown return type

* Deprecate cancelOnGracefulShutdown and add cancelWhenGracefulShutdown

* Add doc comment even for the deprecated function
2024-04-26 14:12:25 +01:00
Fabian Fett d7fe0e7314 cancelOnGracefulShutdown hangs, if cancellation is not immediately (#177)
* Add test case

* Fix tests

* Make code simpler

* Fix Sendable

* swift-format
2024-02-22 11:43:51 +00:00
Adam Fowler 3a186eae18 Add withTaskCancellationOrGracefulShutdownHandler (#176)
* Add withTaskCancellationOrGracefulShutdownHandler

* Test withTaskCancellationOrGracefulShutdownHandler
2024-02-19 08:15:38 +00:00
Franz Busch 55f45e39dd Conform ServiceGroup to Service (#172) 2024-02-10 17:02:43 +00:00
Franz Busch cdd6040d0d Fix logic issues during graceful shutdown (#171)
* Fix logic issues during graceful shutdown

# Motivation

This should fix the remaining issues raised in https://github.com/swift-server/swift-service-lifecycle/pull/166. The problem here was that if a service finished/threw out of order then we were wrongly treating this as if the service that we are currently shutting down finished.

# Modification
This PR ensures that we use the same `services` array during the graceful shutdown to nil out services that have finished. This way we correctly keep track of any service that finished. Additionally, there was a separate bug where we started to shutdown the next service to early if another service threw and had the termination behaviour of `shutdownGracefully`.

# Result
No more incorrect shutdown orderings.

* Fix one more race condition
2024-01-26 12:05:08 +00:00
Tibor BödecsandFranz Busch b21c43a7c1 Update readme instructions & version (#164)
* update readme instructions & version

* add full package example

* Update README.md

Co-authored-by: Franz Busch <privat@franz-busch.de>

* Update README.md

Co-authored-by: Franz Busch <privat@franz-busch.de>

* add getting started to docc index

---------

Co-authored-by: Franz Busch <privat@franz-busch.de>
2024-01-25 06:29:19 -08:00
Max Desiatov 05031271bf Update TerminationBehavior doc comments and notes (#165)
* Mention `TerminationBehavior` in library adoption docs

`ServiceGroupConfiguration.ServiceConfiguration.TerminationBehavior` did not seem to be mentioned in the docs and specifically library adoption docs gave the impression that cancellation on returning from `run` is the only option.

* Update relevant documentation
2024-01-25 14:24:30 +00:00
Franz Busch 0907d4aa34 Make sure we return after gracefully shutdown (#170)
* Make sure we return after gracefully shutdown

# Motivation

After calling `shutdownGracefully` in our `ServiceGroup` we know that one of two things happened:
1. We got an error so something went wrong
2. We got no error so all services finished

In the 2. case, there might still be a few child tasks in the task group e.g. the graceful shutdown timeout task. We are currently still iterating the remaining child task results which might lead into unexpected fatal errors.

# Modification
This PR makes sure that after we have successfully shutdown we are stopping iterating the child task results.

# Result
No more unexpected fatal errors.

* Fix flaky test
2024-01-25 06:15:50 -08:00
Franz Busch 026ed0d5b1 Adopt AsyncAlgorithms (#167)
# Motivation

Now that `swift-async-algorithms` has a `1.0.0` tag we can depend on it instead of shipping our own version of `merge`.
2023-12-05 09:39:21 +00:00
Franz Busch d673fdc912 Add a configurable escalation behaviour (#162)
* Add a configurable escalation behaviour

# Motivation
The service group is often running multiple services and orchestrates shutdown for them. We have seen that sometimes some services never shutdown nor do they respond to task cancellation properly. This can become quite problematic when the whole application is waiting for a service to cancel and otherwise appears to be healthy but in reality can't serve any traffic.

# Modification
This PR adds a new configuration to escalate both graceful shutdown and cancellation. The escalation order is graceful shutdown -> task cancellation -> `fatalError`. The `fatalError` acts a last resort to make sure applications are never stuck.

* George review

* Fix some smaller issues
2023-10-13 06:02:25 -07:00
Gustavo Cairo 45a31b6f1c Add async gracefulShutdown (#158) 2023-09-13 14:37:09 +01:00
Franz Busch 5b1c2defff Vendor swift-async-algorithms (#157)
# Motivation
We are about to tag a `2.0.0` and cannot depend on any non major package.

# Modification
This vendors in `swift-async-algorithms` so that we can use the `AsyncMergeSequence`. In the future, once `swift-async-algorithms` is `1.0.0` itself we can swap back.

# Result
No more non-major dependency.
2023-08-24 02:14:41 -07:00
Honza Dvorsky e4592be5e6 Docs fix - put group.addTask inside the loop (#156)
* Docs fix - put group.addTask inside the loop

* Update How to adopt ServiceLifecycle in applications.md
2023-08-24 10:09:15 +01:00
Honza Dvorsky 2becb2433a Fix a typo in docs (#155) 2023-08-24 10:09:06 +01:00
Franz Busch 2dac25dfb7 Add convenience init to the group (#154)
# Motivation
Right now creating a `ServiceGroup` always requires creating a `ServiceGroupConfiguration`. This is making the barrier to use the group higher than needed for the 99% use-case.

# Modification
This PR adds a new init on the `ServiceGroup` that takes an array of services, graceful shutdown & cancellation signals and the logger.

# Result
Easier use of the service group for the 99% use-case.
2023-08-23 16:26:03 +01:00
Franz Busch 06ec110192 Propagate service error during graceful shutdown (#153)
# Motivation

When a service threw during graceful shutdown but had its behaviour set to `gracefulShutdown` itself then we were not rethrowing the error.

# Modification
This PR makes sure that the first service that throws an error which doesn't have its `failureTerminationBehavior` to `.ignore` gets rethrown by the `ServiceGroup`.
2023-08-23 11:19:20 +01:00
Franz Busch 98a9396dcb Propagate service error when gracefully shutting down (#152)
# Motivation

Propagate the service error when we shutdown the group after the service threw.
2023-08-22 20:54:36 +01:00
Franz Busch 1adb530da6 Only log at debug levels (#151) 2023-08-22 20:45:03 +01:00
Franz Busch 496c81cd91 Clarify log statement around graceful shutdown (#150) 2023-08-22 09:48:43 +01:00
Franz Busch 748fcf39d0 Prefix default logging keys (#149)
# Motivation
To avoid colliding with logging keys of adopters we should prefix our default metadata logging keys.

# Modification
This PR prefixes our default logging keys with `sl-`.
2023-08-16 13:29:57 +01:00
Franz Busch f58dbbfaa9 Add support for return/ThrowBehavior of services (#148)
* Add support for `return/ThrowBehavior` of services

# Motivation

Currently the `ServiceGroup` cancels the whole group when a service returns or throws. This is fine for most server applications but it doesn't work for CLI tools since they mostly want to use the group to orchestrate the services as long as the user command is handled and then return cleanly from the group. Furthermore, even on server setups one might want to customize the behavior of what happens when a service returns/throws e.g. one might want to shutdown the group after their HTTP server throws so that a telemetry service flushes out all the remaining data.

# Modification

This PR does a few things:
1. It creates a new `ServiceConfiguration` and `TerminationBehavior` in the `ServiceGroupConfiguration`. Those can be used to declare the services that are run and what happens when the service returns or throws
2. Adds a new `cancellationSignals` to to the `ServiceGroupConfiguration` which allows cancellation to trigger based on a signal
3. Makes sure that any given service is only retained as long as necessary i.e. when a service returns the group is not retaining it anymore. This allows freeing of resources as early as possible
4. Breaking: Removes the `Hashable` conformance on the configuration structs. This was wrong in the first place and something we should just avoid doing in general.

# Result
We now give the user even more control about the group's behaviors.

* Review

* Review

* Fix CI
2023-08-16 13:22:22 +01:00
Franz Busch 4eaaf079a8 Rename shutdownGracefully to triggerGracefulShutdown on the ServiceGroup (#142) 2023-06-01 09:03:11 +02:00
Franz Busch 7a8226c92d Add logger argument in the README (#141)
# Motivation
We were missing a `logger` argument for the `ServiceGroup` in the README.

# Modification
Adds the missing `logger` argument and renames a file

# Result
Fixes https://github.com/swift-server/swift-service-lifecycle/issues/139
2023-05-30 11:22:58 +01:00
Franz Busch 486048424f Remove comment about the need of cancelAll (#138) 2023-05-30 11:08:20 +01:00
Simon LeebandSimon Leeb 5074896b5a Fixes cancelOnGracefulShutdown issue (#135) (#136)
Motivation:
+ cancelOnGracefulShutdown was getting stuck on macOS

Modifications:
+ added explicit group.cancelAll() to all execution paths

Co-authored-by: Simon Leeb <simon.leeb@sinformatics.at>
2023-05-11 11:04:39 +01:00
Franz Busch fafab8f7ad Overhaul service lifecycle for Structured Concurrency (#130)
* 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
2023-04-27 11:56:08 +01:00