@@ -36,23 +36,23 @@ public protocol LifecycleTask {
36
36
public struct LifecycleHandler {
37
37
private let body : ( @escaping ( Error ? ) -> Void ) -> Void
38
38
39
- /// Initialize a `Lifecycle.Handler ` based on a completion handler.
39
+ /// Initialize a `LifecycleHandler ` based on a completion handler.
40
40
///
41
41
/// - parameters:
42
42
/// - callback: the underlying completion handler
43
43
public init ( _ callback: @escaping ( @escaping ( Error ? ) -> Void ) -> Void ) {
44
44
self . body = callback
45
45
}
46
46
47
- /// Asynchronous `Lifecycle.Handler ` based on a completion handler.
47
+ /// Asynchronous `LifecycleHandler ` based on a completion handler.
48
48
///
49
49
/// - parameters:
50
50
/// - callback: the underlying completion handler
51
51
public static func async ( _ callback: @escaping ( @escaping ( Error ? ) -> Void ) -> Void ) -> LifecycleHandler {
52
52
return LifecycleHandler ( callback)
53
53
}
54
54
55
- /// Asynchronous `Lifecycle.Handler ` based on a blocking, throwing function.
55
+ /// Asynchronous `LifecycleHandler ` based on a blocking, throwing function.
56
56
///
57
57
/// - parameters:
58
58
/// - body: the underlying function
@@ -67,7 +67,7 @@ public struct LifecycleHandler {
67
67
}
68
68
}
69
69
70
- /// Noop `Lifecycle.Handler `.
70
+ /// Noop `LifecycleHandler `.
71
71
public static var none : LifecycleHandler {
72
72
return LifecycleHandler { callback in
73
73
callback ( nil )
@@ -83,65 +83,74 @@ public struct LifecycleHandler {
83
83
84
84
public struct ServiceLifecycle {
85
85
private let configuration : Configuration
86
- private let lifecycle : ComponentLifecycle
87
86
88
- /// Creates a `ComponentLifecycle` instance.
87
+ /// The underlying `ComponentLifecycle` instance
88
+ ///
89
+ /// Designed for composition purposes, mainly for frameworks that need to offer both top-level start/stop functionality and composition into larger systems.
90
+ /// In other words, should not be used outside the context of building an Application framework.
91
+ private let underlying : ComponentLifecycle
92
+
93
+ /// Creates a `ServiceLifecycle` instance.
89
94
///
90
95
/// - parameters:
91
96
/// - configuration: Defines lifecycle `Configuration`
92
97
public init ( configuration: Configuration = . init( ) ) {
93
98
self . configuration = configuration
94
- self . lifecycle = ComponentLifecycle ( label: " Lifecycle " , logger: self . configuration. logger)
99
+ self . underlying = ComponentLifecycle ( label: self . configuration . label , logger: self . configuration. logger)
95
100
// setup backtrace trap as soon as possible
96
101
if configuration. installBacktrace {
97
- self . lifecycle . log ( " installing backtrace " )
102
+ self . log ( " installing backtrace " )
98
103
Backtrace . install ( )
99
104
}
100
105
}
101
106
102
- /// Starts the provided `LifecycleItem ` array.
107
+ /// Starts the provided `LifecycleTask ` array.
103
108
/// Startup is performed in the order of items provided.
104
109
///
105
110
/// - parameters:
106
111
/// - callback: The handler which is called after the start operation completes. The parameter will be `nil` on success and contain the `Error` otherwise.
107
112
public func start( _ callback: @escaping ( Error ? ) -> Void ) {
108
113
self . setupShutdownHook ( )
109
- self . lifecycle . start ( on: self . configuration. callbackQueue, callback)
114
+ self . underlying . start ( on: self . configuration. callbackQueue, callback)
110
115
}
111
116
112
- /// Starts the provided `LifecycleItem ` array and waits (blocking) until a shutdown `Signal` is captured or `shutdown` is called on another thread.
117
+ /// Starts the provided `LifecycleTask ` array and waits (blocking) until a shutdown `Signal` is captured or `shutdown` is called on another thread.
113
118
/// Startup is performed in the order of items provided.
114
119
public func startAndWait( ) throws {
115
120
self . setupShutdownHook ( )
116
- try self . lifecycle . startAndWait ( on: self . configuration. callbackQueue)
121
+ try self . underlying . startAndWait ( on: self . configuration. callbackQueue)
117
122
}
118
123
119
- /// Shuts down the `LifecycleItem ` array provided in `start` or `startAndWait`.
124
+ /// Shuts down the `LifecycleTask ` array provided in `start` or `startAndWait`.
120
125
/// Shutdown is performed in reverse order of items provided.
121
126
///
122
127
/// - parameters:
123
128
/// - callback: The handler which is called after the start operation completes. The parameter will be `nil` on success and contain the `Error` otherwise.
124
129
public func shutdown( _ callback: @escaping ( Error ? ) -> Void = { _ in } ) {
125
- self . lifecycle . shutdown ( callback)
130
+ self . underlying . shutdown ( callback)
126
131
}
127
132
128
133
/// Waits (blocking) until shutdown `Signal` is captured or `shutdown` is invoked on another thread.
129
134
public func wait( ) {
130
- self . lifecycle . wait ( )
135
+ self . underlying . wait ( )
131
136
}
132
137
133
138
private func setupShutdownHook( ) {
134
139
self . configuration. shutdownSignal? . forEach { signal in
135
- self . lifecycle . log ( " setting up shutdown hook on \( signal) " )
140
+ self . log ( " setting up shutdown hook on \( signal) " )
136
141
let signalSource = ServiceLifecycle . trap ( signal: signal, handler: { signal in
137
- self . lifecycle . log ( " intercepted signal: \( signal) " )
142
+ self . log ( " intercepted signal: \( signal) " )
138
143
self . shutdown ( )
139
144
} )
140
- self . lifecycle . shutdownGroup. notify ( queue: . global( ) ) {
145
+ self . underlying . shutdownGroup. notify ( queue: . global( ) ) {
141
146
signalSource. cancel ( )
142
147
}
143
148
}
144
149
}
150
+
151
+ private func log( _ message: String ) {
152
+ self . underlying. log ( message)
153
+ }
145
154
}
146
155
147
156
extension ServiceLifecycle {
@@ -186,45 +195,47 @@ extension ServiceLifecycle {
186
195
}
187
196
188
197
public extension ServiceLifecycle {
189
- /// Adds a `Task ` to a `Tasks ` collection.
198
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
190
199
///
191
200
/// - parameters:
192
- /// - tasks: one or more `Tasks `.
201
+ /// - tasks: one or more `LifecycleTask `.
193
202
func register( _ tasks: LifecycleTask ... ) {
194
- self . lifecycle . register ( tasks)
203
+ self . underlying . register ( tasks)
195
204
}
196
205
197
- /// Adds a `Task ` to a `Tasks ` collection.
206
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
198
207
///
199
208
/// - parameters:
200
- /// - tasks: array of `Tasks `.
209
+ /// - tasks: array of `LifecycleTask `.
201
210
func register( _ tasks: [ LifecycleTask ] ) {
202
- self . lifecycle . register ( tasks)
211
+ self . underlying . register ( tasks)
203
212
}
204
213
205
- /// Adds a `Task ` to a `Tasks ` collection.
214
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
206
215
///
207
216
/// - parameters:
208
217
/// - label: label of the item, useful for debugging.
209
- /// - start: `Handler ` to perform the startup.
210
- /// - shutdown: `Handler ` to perform the shutdown.
218
+ /// - start: `LifecycleHandler ` to perform the startup.
219
+ /// - shutdown: `LifecycleHandler ` to perform the shutdown.
211
220
func register( label: String , start: LifecycleHandler , shutdown: LifecycleHandler ) {
212
- self . lifecycle . register ( label: label, start: start, shutdown: shutdown)
221
+ self . underlying . register ( label: label, start: start, shutdown: shutdown)
213
222
}
214
223
215
- /// Adds a `Task ` to a `Tasks ` collection.
224
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
216
225
///
217
226
/// - parameters:
218
227
/// - label: label of the item, useful for debugging.
219
- /// - handler: `Handler ` to perform the shutdown.
228
+ /// - handler: `LifecycleHandler ` to perform the shutdown.
220
229
func registerShutdown( label: String , _ handler: LifecycleHandler ) {
221
- self . lifecycle . registerShutdown ( label: label, handler)
230
+ self . underlying . registerShutdown ( label: label, handler)
222
231
}
223
232
}
224
233
225
234
extension ServiceLifecycle {
226
- /// `Lifecycle ` configuration options.
235
+ /// `ServiceLifecycle ` configuration options.
227
236
public struct Configuration {
237
+ /// Defines the `label` for the lifeycle and its Logger
238
+ public var label : String
228
239
/// Defines the `Logger` to log with.
229
240
public var logger : Logger
230
241
/// Defines the `DispatchQueue` on which startup and shutdown callback handlers are run.
@@ -234,11 +245,13 @@ extension ServiceLifecycle {
234
245
/// Defines if to install a crash signal trap that prints backtraces.
235
246
public var installBacktrace : Bool
236
247
237
- public init ( logger: Logger = Logger ( label: " Lifecycle " ) ,
248
+ public init ( label: String = " Lifecycle " ,
249
+ logger: Logger ? = nil ,
238
250
callbackQueue: DispatchQueue = . global( ) ,
239
251
shutdownSignal: [ Signal ] ? = [ . TERM, . INT] ,
240
252
installBacktrace: Bool = true ) {
241
- self . logger = logger
253
+ self . label = label
254
+ self . logger = logger ?? Logger ( label: label)
242
255
self . callbackQueue = callbackQueue
243
256
self . shutdownSignal = shutdownSignal
244
257
self . installBacktrace = installBacktrace
@@ -275,7 +288,7 @@ public class ComponentLifecycle: LifecycleTask {
275
288
self . shutdownGroup. enter ( )
276
289
}
277
290
278
- /// Starts the provided `LifecycleItem ` array.
291
+ /// Starts the provided `LifecycleTask ` array.
279
292
/// Startup is performed in the order of items provided.
280
293
///
281
294
/// - parameters:
@@ -284,7 +297,7 @@ public class ComponentLifecycle: LifecycleTask {
284
297
self . start ( on: . global( ) , callback)
285
298
}
286
299
287
- /// Starts the provided `LifecycleItem ` array.
300
+ /// Starts the provided `LifecycleTask ` array.
288
301
/// Startup is performed in the order of items provided.
289
302
///
290
303
/// - parameters:
@@ -295,7 +308,7 @@ public class ComponentLifecycle: LifecycleTask {
295
308
self . _start ( on: queue, tasks: tasks, callback: callback)
296
309
}
297
310
298
- /// Starts the provided `LifecycleItem ` array and waits (blocking) until `shutdown` is called on another thread.
311
+ /// Starts the provided `LifecycleTask ` array and waits (blocking) until `shutdown` is called on another thread.
299
312
/// Startup is performed in the order of items provided.
300
313
///
301
314
/// - parameters:
@@ -313,7 +326,7 @@ public class ComponentLifecycle: LifecycleTask {
313
326
self . wait ( )
314
327
}
315
328
316
- /// Shuts down the `LifecycleItem ` array provided in `start` or `startAndWait`.
329
+ /// Shuts down the `LifecycleTask ` array provided in `start` or `startAndWait`.
317
330
/// Shutdown is performed in reverse order of items provided.
318
331
public func shutdown( _ callback: @escaping ( Error ? ) -> Void = { _ in } ) {
319
332
let setupShutdownListener = { ( queue: DispatchQueue ) in
@@ -482,18 +495,18 @@ public extension ComponentLifecycle {
482
495
}
483
496
}
484
497
485
- /// Adds a `Task ` to a `Tasks ` collection.
498
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
486
499
///
487
500
/// - parameters:
488
- /// - tasks: one or more `Tasks `.
501
+ /// - tasks: one or more `LifecycleTask `.
489
502
func register( _ tasks: LifecycleTask ... ) {
490
503
self . register ( tasks)
491
504
}
492
505
493
- /// Adds a `Task ` to a `Tasks ` collection.
506
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
494
507
///
495
508
/// - parameters:
496
- /// - tasks: array of `Tasks `.
509
+ /// - tasks: array of `LifecycleTask `.
497
510
func register( _ tasks: [ LifecycleTask ] ) {
498
511
self . stateLock. withLock {
499
512
guard case . idle = self . state else {
@@ -505,7 +518,7 @@ public extension ComponentLifecycle {
505
518
}
506
519
}
507
520
508
- /// Adds a `Task ` to a `Tasks ` collection.
521
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
509
522
///
510
523
/// - parameters:
511
524
/// - label: label of the item, useful for debugging.
@@ -515,7 +528,7 @@ public extension ComponentLifecycle {
515
528
self . register ( Task ( label: label, start: start, shutdown: shutdown) )
516
529
}
517
530
518
- /// Adds a `Task ` to a `Tasks ` collection.
531
+ /// Adds a `LifecycleTask ` to a `LifecycleTasks ` collection.
519
532
///
520
533
/// - parameters:
521
534
/// - label: label of the item, useful for debugging.
0 commit comments