Skip to content

Overhaul service lifecycle for Structured Concurrency #130

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

Merged
merged 27 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
531e74a
Overhaul service lifecycle for Structured Concurrency
FranzBusch Mar 10, 2023
e748ec5
Remove `shutdownGracefully` method and switch to a handler based appr…
FranzBusch Mar 13, 2023
7350022
Code review from George
FranzBusch Mar 14, 2023
4c51837
Add extensions to `TaskGroup` and `ThrowingTaskGroup`
FranzBusch Mar 14, 2023
97de525
Remove long running property
FranzBusch Mar 14, 2023
e1c3013
Documentation
FranzBusch Mar 14, 2023
364e85d
Fix tests
FranzBusch Mar 14, 2023
020fbfa
Ensure ordering of shutdown
FranzBusch Mar 16, 2023
d1e717f
Test
FranzBusch Mar 17, 2023
68aaebb
Introduce ServiceLifecycleTestKit
FranzBusch Mar 17, 2023
be3e460
Add AsyncCancelOngracefulShutdownSequence
FranzBusch Mar 20, 2023
bcfccdf
Tests and docs for graceful shutdown
FranzBusch Mar 21, 2023
8c5c58a
Remove unsafe flags
FranzBusch Mar 21, 2023
590ea1e
Add article for application authors
FranzBusch Mar 22, 2023
a530153
Add correct licence and notice information for the things we copied f…
FranzBusch Mar 23, 2023
2e22daa
Code review
FranzBusch Mar 27, 2023
9797c76
Remove @_unsafeInheritExectuor
FranzBusch Mar 28, 2023
35af255
Code review
FranzBusch Mar 28, 2023
d7a56c3
Change log levels, rename to withGracefulShutdownHandler and support …
FranzBusch Mar 28, 2023
7cab86b
Add public `shutdownGracefully` method to the service runner
FranzBusch Apr 5, 2023
7e412d6
Code review and expose new cancelOnGracefulShutdown method
FranzBusch Apr 24, 2023
b6834b3
Extend graceful shutdown APIs and tolerate running outside of a `Serv…
FranzBusch Apr 25, 2023
c9f8424
Rename to `ServiceGroup`
FranzBusch Apr 25, 2023
c8d0efb
Enable Swift 5.6 support
FranzBusch Apr 25, 2023
8dfbcdd
Update soundess dockerfile
FranzBusch Apr 25, 2023
e82d688
Update Readme
FranzBusch Apr 25, 2023
e59ed5c
Make 5.6 build work
FranzBusch Apr 25, 2023
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
2 changes: 1 addition & 1 deletion .spi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1
builder:
configs:
- documentation_targets: [Lifecycle, LifecycleNIOCompat]
- documentation_targets: [ServiceLifecycle, UnixSignals]
15 changes: 14 additions & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
# file options

--swiftversion 5.0
--swiftversion 5.7
--exclude .build

# format options

--self insert
--patternlet inline
--ranges nospace
--stripunusedargs unnamed-only
--ifdef no-indent
--extensionacl on-declarations
--disable typeSugar # https://github.com/nicklockwood/SwiftFormat/issues/636
--disable andOperator
--disable wrapMultilineStatementBraces
--disable enumNamespaces
--disable redundantExtensionACL
--disable redundantReturn
--disable preferKeyPath
--disable sortedSwitchCases
--disable hoistTry
--disable hoistAwait
--disable redundantOptionalBinding

# rules
36 changes: 36 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

The ServiceLifecycle Project
===========================

Please visit the ServiceLifecycle web site for more information:

* https://github.com/swift-server/swift-service-lifecycle

Copyright 2019-2023 The ServiceLifecycle Project

The ServiceLifecycle Project licenses this file to you under the Apache License,
version 2.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at:

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.

Also, please refer to each LICENSE.txt file, which is located in
the 'license' directory of the distribution file, for the license terms of the
components that this product depends on.

---

This product contains derivations of the Lock and LockedValueBox implementations from SwiftNIO.

* LICENSE (Apache License 2.0):
* https://www.apache.org/licenses/LICENSE-2.0
* HOMEPAGE:
* https://github.com/apple/swift-nio

---
94 changes: 73 additions & 21 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,83 @@ import PackageDescription

let package = Package(
name: "swift-service-lifecycle",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.watchOS(.v6),
.tvOS(.v13),
],
products: [
.library(name: "Lifecycle", targets: ["Lifecycle"]),
.library(name: "LifecycleNIOCompat", targets: ["LifecycleNIOCompat"]),
.library(
name: "ServiceLifecycle",
targets: ["ServiceLifecycle"]
),
.library(
name: "ServiceLifecycleTestKit",
targets: ["ServiceLifecycleTestKit"]
),
.library(
name: "UnixSignals",
targets: ["UnixSignals"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-metrics.git", "1.0.0" ..< "3.0.0"),
.package(url: "https://github.com/swift-server/swift-backtrace.git", from: "1.1.1"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"), // used in tests
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emitting metric on application start and shutdown has been very valuable in real world production services, the canonical example is a monitoring this to detect an a spike in application restart because of crashes

Copy link
Member

@fabianfett fabianfett Mar 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I agree. But ServiceLifecycle created the weird situation in the past where ServiceLifecycle depended on a MetricSystem that adopters wanted to setup with ServiceLifecycle. Basically the first use occurred even before the system was actually ready to create Counters. A good example for this are MetricSystems that are sending their data to a server themselves.

Because of this, I'm a big fan of dropping the swift-metrics dependency here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see your point. Maybe the solution is for this library to take an abstract handler that can emit the specific metrics, or even something like a delegate so that the application / service can do the bridging

Copy link
Contributor Author

@FranzBusch FranzBusch Mar 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I agree. But ServiceLifecycle created the weird situation in the past where ServiceLifecycle depended on a MetricSystem that adopters wanted to setup with ServiceLifecycle. Basically the first use occurred even before the system was actually ready to create Counters. A good example for this are MetricSystems that are sending their data to a server themselves.

I don't think this applies anymore and we can safely take the dependency on swift-metrics. A potential metric backend should expose itself as a Service with a run() method. Furthermore, it should handle receiving metrics before the run() method is called and buffer them.

So the setup that I would recommend here is the following

// In Bootstrap.swift
import FooMetricsBackend

@main
struct Bootstrap {
    static func main() async throws {
        let fooMetricsBackend = FooMetricsBackend()
        MetricsSystem.bootstrap(fooMetricsBackend)
        
        let someService = SomeService()

        let serviceRunner = ServiceRunner(services: [fooMetricsBackend, someService], configuration: ...)
        try await serviceRunner.run()
    }
}

The thing we should discuss is if we want to provide these metrics out of the box or not. A user could just wrap the call to serviceRunner.run() inside metric counters.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right. i tend to think we do want to provide these metrics out of the box, same as we provide logging information and depend on swift-log. could be convinced otherwise if there is strong reason not to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on this, I would like to get the PR merged without metrics and then add it on top since the PR is getting quite big.

.package(
url: "https://github.com/apple/swift-log.git",
from: "1.5.2"
),
.package(
url: "https://github.com/apple/swift-docc-plugin",
from: "1.0.0"
),
.package(
url: "https://github.com/apple/swift-async-algorithms",
from: "0.1.0"
),
],
targets: [
.target(name: "Lifecycle", dependencies: [
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "Logging", package: "swift-log"),
.product(name: "Metrics", package: "swift-metrics"),
.product(name: "Backtrace", package: "swift-backtrace"),
]),

.target(name: "LifecycleNIOCompat", dependencies: [
"Lifecycle",
.product(name: "NIO", package: "swift-nio"),
]),

.testTarget(name: "LifecycleTests", dependencies: ["Lifecycle", "LifecycleNIOCompat"]),
.target(
name: "ServiceLifecycle",
dependencies: [
.product(
name: "Logging",
package: "swift-log"
),
.product(
name: "AsyncAlgorithms",
package: "swift-async-algorithms"
),
.target(name: "UnixSignals"),
.target(name: "ConcurrencyHelpers"),
]
),
.target(
name: "ServiceLifecycleTestKit",
dependencies: [
.target(name: "ServiceLifecycle"),
]
),
.target(
name: "UnixSignals",
dependencies: [
.target(name: "ConcurrencyHelpers"),
]
),
.target(
name: "ConcurrencyHelpers"
),
.testTarget(
name: "ServiceLifecycleTests",
dependencies: [
.target(name: "ServiceLifecycle"),
.target(name: "ServiceLifecycleTestKit"),
]
),
.testTarget(
name: "UnixSignalsTests",
dependencies: [
.target(name: "UnixSignals"),
]
),
]
)
37 changes: 0 additions & 37 deletions [email protected]

This file was deleted.

37 changes: 0 additions & 37 deletions [email protected]

This file was deleted.

37 changes: 0 additions & 37 deletions [email protected]

This file was deleted.

37 changes: 0 additions & 37 deletions [email protected]

This file was deleted.

37 changes: 0 additions & 37 deletions [email protected]

This file was deleted.

Loading