113 Commits
Author SHA1 Message Date
Franz Busch b71a961c12 Add support for SIGWINCH (#161)
# Motivation
`SIGWINCH` is sent to applications when the window resizes. This is useful to observe for terminal applications to reformat the logging one resize.

# Modification
This PR adds support for `SIGWINCH` in `UnixSignal`s.
2.2.0
2023-10-10 19:31:12 +01:00
Rick Newton-RogersandYim Lee 4d11d9817d Bump minimum Swift version to 5.7 (#160)
* Bump minimum Swift version to 5.7

Motivation:

Now that Swift 5.9 is GM we should update the supported versions and
remove 5.6

Modifications:

* Update `Package.swift`
* Delete the <5.7 docker compose files and make a 5.10 one
* Update the 5.9 docker file to stop using nightlies

Result:

Remove support for Swift 5.6, add 5.10

* Remove '--enable-test-discovery'

To see if it's causing CI build getting stuck

---------

Co-authored-by: Yim Lee <yim_lee@apple.com>
2023-10-05 10:44:21 +01:00
Gustavo Cairo 45a31b6f1c Add async gracefulShutdown (#158) 2.1.0 2023-09-13 14:37:09 +01:00
Franz Busch b69c630d58 Fix nightly CI 2.0.0 2023-08-24 10:26:50 +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.
2.0.0-beta.2
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-`.
2.0.0-beta.1
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 36f71f61d7 Add support for SIGQUIT (#146)
# Motivation
Some applications want to react to `SIGQUIT` which is similar to `SIGINT` but normally maps to a different key `C-\`.

# Modification
This PR adds support for `SIGQUIT` in the `UnixSignal`s module.

# Result
Support for `SIGQUIT`.
2023-08-14 11:18:31 +01:00
Franz Busch de1ef4ba30 Use #if canImport(Darwin) where possible (#144) 2023-06-21 14:08:40 +01:00
tomer doron cf346f7536 update code of conduct (#143)
motivation: align CoC across the swift project

changes: change CoC to link to the swift.org version
2023-06-06 19:23:48 +01:00
Franz Busch 4eaaf079a8 Rename shutdownGracefully to triggerGracefulShutdown on the ServiceGroup (#142) 2.0.0-alpha.3 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>
2.0.0-alpha.2
2023-05-11 11:04:39 +01:00
1ef3d969b3 Fixup in README.md (#133)
* Fixup in README.md

I think there is a protocol conformance missing in the example.

* Update README.md

Co-authored-by: Adam Fowler <adamfowler71@gmail.com>

---------

Co-authored-by: Adam Fowler <adamfowler71@gmail.com>
Co-authored-by: Franz Busch <privat@franz-busch.de>
2023-05-11 10:56:59 +01:00
Mahdi Bahrami af0f9f5180 Update README.md (#134) 2023-05-08 10:57:52 +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
2.0.0-alpha.1
2023-04-27 11:56:08 +01:00
Yim Lee 7b60c92186 Add docker-compose for Swift 5.9 (#131)
Also update `main` nightly to Ubuntu 22.04
2023-04-04 20:53:19 -07:00
tomer doron b7628eb6b3 add docker compose setup for swift 5.8 (#129)
motivation: prepare for swift 5.8

changes:
* update use of Locks to address NIO warnings
* update docker compose setup to use release version of 5.7
* add docker compose setup for 5.8
2023-04-04 19:54:32 -07:00
Yim Lee fbdb075d09 Add .spi.yml for Swift Package Index DocC support (#127) 2022-12-04 09:29:06 -08:00
Yim Lee 97df12eda3 Fix soundness script (#126) 2022-08-08 10:37:22 -07:00
Yim Lee 3dbb8b3a37 DocC setup (#125)
* DocC setup

* Fix formatting

* Fix CI for newer Swift versions

* Rename docc directory

* Revert non-docc related changes

* Revert non-docc related changes

* Use version-specific manifest for older Swift versions
2022-08-05 13:50:19 -07:00
tomer doron 22363fed31 add the ability to de-register a task (#121)
motivation: sometimes a task needs to be de-registered since the task was manually shutdown outside the lifecycle scope

changes:
* registration APIs now return a registration key which can be used as a cancellation token
* add API to de-register a task
* refactor state to use a registery instead of array of tasks
* add tests
1.0.0-alpha.11
2022-06-13 10:28:56 -07:00
tomer doron 2133bfa9c5 shutdown() should cancel the signal handlers installed by start() (#120) 2022-05-20 18:18:29 -07:00
tomer doron e63be9ef46 ci update (#119)
motivation: 5.6 is out

changes:
* use release version of 5.6
* add docker setup for 5.7 (using nightly for now)
2022-03-16 21:20:22 -07:00
Tobias 2bc97b030e Update availability checks for async function for all supported platforms (#118) 2022-01-05 17:21:42 -08:00
tomer doron da0cb159aa update doc generation script (#117)
motivation: source-kitten has changed how its invoked

changes:
* invoke source-kitten with the correct flags
* make location of source-kitten source checkout more robust
* udpate module list
2021-12-16 15:17:14 -08:00
Adam Fowler 531c6f8cd1 Use swift-atomics from v1.0.0 (#114)
* Use swift-atomics from v1.0.0
* swift-atomics 1.0.0 requires swift 5.3
1.0.0-alpha.10
2021-10-07 10:19:13 -07:00
tomer doron efb1d749af Use release version of 5.5 in CI (#112)
motivation: 5.5 release is available

changes: update docker CI setup to use the release version of 5.5
2021-09-22 17:13:43 -07:00
Josh Wright cea89ce043 Switch up compiler directives for concurrency (#111)
* Switch up compiler directives for concurrency
1.0.0-alpha.9
2021-09-22 11:46:51 -07:00
tomer doron 2744df51f9 unless shutdownIfNotStarted is set to true, dont shutdown items that failed to start (#107)
motivation: improve confusing behavior

changes:
* change startTask to return an array of started tasks instead of an index
* only shutdown successfully started tasks or ones that have explicitly set shutdownIfNotStarted to true
* add and adjust tests
1.0.0-alpha.8
2021-09-03 23:17:55 -07:00
tomer doron c825a58490 shutdown any task that have shutdownIfNotStarted set to true when shutting down a never-started-lifecycle (#109)
motivation: more reliable shutdown in edge cases where lifecycle never gets started

changes: when shutting down an idle lifecycle, try to shutdown any tasks that have shutdownIfNotStarted set to true
2021-09-02 08:48:04 -07:00
tomer doron f6c02f7b46 only install jazzy on latest ubuntu versions (#108)
motivation: jazzy no longer works on older version of ubuntu as ruby is too old

changes: update docker to only install jazzy on latest ubuntu versions
2021-09-01 22:45:00 -07:00
tomer doron 66fb9c089b fix repeated shutdown issue on linux (#104)
motivation: repeatedly calling shutdown in linux was broken due to multiple calls to singal() which work on darwin but not linux

changes:
* conditionalize the call to signal to once per process
* add test
2021-08-19 17:23:03 -07:00
tomer doron b00bee04fd add 5.5 ci (#105)
motivation: test with 5.5

changes: add 5.5. docker-compose and pin jazzy version
2021-08-19 14:45:13 -07:00
tomer doron 9a23b01e25 adopt SSWG security guidelines (#101)
* adopt SSWG security guidelines

add SECURITY.md detailing the security process

* Update README.md
2021-06-21 11:24:24 +09:00
tomer doron 84d2bc37d7 adjust to updated concurrency API (#102) 2021-06-09 19:00:28 -07:00
tomer doron 7f70674c22 add handlers that support async functions (#99)
motivation: async/await is coming in 5.5, take first steps to support it

changes:
* introduce handler initializers that takes async functions (@escaping () async throws -> Void, @escaping () async throws -> State)
* add tests
2021-05-12 21:41:15 -07:00
tomer doron ad8631e430 update 5.4 to release docker image (#98)
motivation: 5.4 is out!

changes: use release docker images instead of nightly
2021-05-06 17:57:32 -07:00
tomer doron 727db6c591 Update docker-compose.2004.main.yaml (#97)
motivation: fix nightly docker setup

changes: fix typo
2021-05-06 17:42:24 -07:00
Moritz Lang 103c143d52 Remove [Lifecycle] prefix from log messages (#95)
Motivation:

Since Swift Log 1.3.0, log handlers can include the source of a particular message,
rendering the [Lifecycle] prefix unnecessary.

Modifications:

Removed the [Lifecycle] prefix from all log messages by removing the underlying
log helper and calling out to the Logger itself.

Result:

Log messages written by a log handler which includes the location wont log "Lifecycle" twice.
2021-05-04 11:58:13 +09:00
tomer doron 4714c3cdfa add lifecycle metrics (#93)
motivation: startup/shutdown metrics are important for real-life services, for example spike in start metrics indicates a crash-loop

changes:
* add start and shutdown counters to ComponentLifecycle
* add start and shutdown timers to report duration of startup and shutdown operations
1.0.0-alpha.7
2021-03-18 11:39:26 -07:00
tomer doron 5577147e4e make the underlying component lifecycle private again (#91)
* make the underlying component lifecycle private again

motivation: there is no need to explose the underlying component lifecycle, since there are better composition solutions

changets: make ServiceLifecycle::underlying private (again)

* fixup
2021-03-18 09:12:50 +09:00