Skip to content

Commit 2efa2a0

Browse files
committed
Remove long running property
1 parent 8d24edd commit 2efa2a0

File tree

3 files changed

+16
-44
lines changed

3 files changed

+16
-44
lines changed

Sources/ServiceLifecycle/Service.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414

1515
/// This is the basic protocol that a service has to implement.
1616
public protocol Service: Sendable {
17-
/// This indicates if the service is expected to be running for the entire duration.
18-
/// If a long running service is returning from the ``Service/run()-1ougx`` method it is treated
19-
/// as an unexpected early exit and all other services are going be cancelled.
20-
var isLongRunning: Bool { get }
21-
2217
/// This method is called when the ``ServiceRunner`` is starting all the services.
2318
///
2419
/// Concrete implementation should execute their long running work in this method such as:
2520
/// - Handling incoming connections and requests
2621
/// - Background refreshes
22+
///
23+
/// - Important: Returning or throwing from this method is indicating a failure of the service and will cause the ``ServiceRunner``
24+
/// to cancel the child tasks of all other running services.
2725
func run() async throws
2826
}

Sources/ServiceLifecycle/ServiceRunner.swift

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import Logging
1616
import UnixSignals
1717

18-
/// A ``ServiceRunner`` is responsible for running a number of services, setting up signal handling and shutting down services in the correct order.
18+
/// A ``ServiceRunner`` is responsible for running a number of services, setting up signal handling and signalling graceful shutdown to the services.
1919
public actor ServiceRunner: Sendable {
2020
/// The internal state of the ``ServiceRunner``.
2121
private enum State {
@@ -128,8 +128,6 @@ public actor ServiceRunner: Sendable {
128128
}
129129
}
130130

131-
var finishedServiceCount = 0
132-
133131
// We are going to wait for any of the services to finish or
134132
// the signal sequence to throw an error.
135133
while !group.isEmpty {
@@ -138,41 +136,17 @@ public actor ServiceRunner: Sendable {
138136

139137
switch result {
140138
case .serviceFinished(let service):
141-
if service.isLongRunning {
142-
// If a long running service finishes early we treat this as an unexpected
143-
// early exit and have to cancel the rest of the services.
144-
self.logger.error(
145-
"Service finished unexpectedly",
146-
metadata: [
147-
self.configuration.logging.serviceKey: "\(service)",
148-
]
149-
)
150-
151-
group.cancelAll()
152-
return .failure(ServiceRunnerError.serviceFinishedUnexpectedly())
153-
} else {
154-
// This service finished early but we expected it.
155-
// So we are just going to wait for the next service
156-
self.logger.debug(
157-
"Service finished",
158-
metadata: [
159-
self.configuration.logging.serviceKey: "\(service)",
160-
]
161-
)
162-
163-
// We have to keep track of how many services finished to make sure
164-
// to stop when only the signal child task is left
165-
finishedServiceCount += 1
166-
if finishedServiceCount == self.services.count {
167-
// Every service finished. We can cancel the signal handling now
168-
// and return
169-
group.cancelAll()
170-
return .success(())
171-
} else {
172-
// There are still running services that we have to wait for
173-
continue
174-
}
175-
}
139+
// If a long running service finishes early we treat this as an unexpected
140+
// early exit and have to cancel the rest of the services.
141+
self.logger.error(
142+
"Service finished unexpectedly",
143+
metadata: [
144+
self.configuration.logging.serviceKey: "\(service)",
145+
]
146+
)
147+
148+
group.cancelAll()
149+
return .failure(ServiceRunnerError.serviceFinishedUnexpectedly())
176150

177151
case .serviceThrew(let service, let error):
178152
// One of the servers threw an error. We have to cancel everything else now.

Sources/ServiceLifecycle/ServiceRunnerConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct ServiceRunnerConfiguration: Hashable, Sendable {
2929
/// The logging key used for logging an error.
3030
public var errorKey = "error"
3131

32-
/// Initializes a new ``ServiceRunnerConfiguration/LoggingConfiguration-swift.struct``.
32+
/// Initializes a new ``ServiceRunnerConfiguration/LoggingConfiguration``.
3333
public init() {}
3434
}
3535

0 commit comments

Comments
 (0)