@@ -81,6 +81,8 @@ public struct LifecycleHandler {
81
81
82
82
// MARK: - ServiceLifecycle
83
83
84
+ /// `ServiceLifecycle` provides a basic mechanism to cleanly startup and shutdown the application, freeing resources in order before exiting.
85
+ /// By default, also install shutdown hooks based on `Signal` and backtraces.
84
86
public struct ServiceLifecycle {
85
87
private let configuration : Configuration
86
88
@@ -194,41 +196,10 @@ extension ServiceLifecycle {
194
196
}
195
197
}
196
198
197
- public extension ServiceLifecycle {
198
- /// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
199
- ///
200
- /// - parameters:
201
- /// - tasks: one or more `LifecycleTask`.
202
- func register( _ tasks: LifecycleTask ... ) {
203
- self . underlying. register ( tasks)
204
- }
205
-
206
- /// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
207
- ///
208
- /// - parameters:
209
- /// - tasks: array of `LifecycleTask`.
210
- func register( _ tasks: [ LifecycleTask ] ) {
199
+ extension ServiceLifecycle : LifecycleTasksContainer {
200
+ public func register( _ tasks: [ LifecycleTask ] ) {
211
201
self . underlying. register ( tasks)
212
202
}
213
-
214
- /// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
215
- ///
216
- /// - parameters:
217
- /// - label: label of the item, useful for debugging.
218
- /// - start: `LifecycleHandler` to perform the startup.
219
- /// - shutdown: `LifecycleHandler` to perform the shutdown.
220
- func register( label: String , start: LifecycleHandler , shutdown: LifecycleHandler ) {
221
- self . underlying. register ( label: label, start: start, shutdown: shutdown)
222
- }
223
-
224
- /// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
225
- ///
226
- /// - parameters:
227
- /// - label: label of the item, useful for debugging.
228
- /// - handler: `LifecycleHandler` to perform the shutdown.
229
- func registerShutdown( label: String , _ handler: LifecycleHandler ) {
230
- self . underlying. registerShutdown ( label: label, handler)
231
- }
232
203
}
233
204
234
205
extension ServiceLifecycle {
@@ -265,7 +236,7 @@ struct ShutdownError: Error {
265
236
266
237
// MARK: - ComponentLifecycle
267
238
268
- /// `Lifecycle ` provides a basic mechanism to cleanly startup and shutdown the application, freeing resources in order before exiting.
239
+ /// `ComponentLifecycle ` provides a basic mechanism to cleanly startup and shutdown a subsystem in a larger application, freeing resources in order before exiting.
269
240
public class ComponentLifecycle : LifecycleTask {
270
241
public let label : String
271
242
private let logger : Logger
@@ -480,42 +451,35 @@ public class ComponentLifecycle: LifecycleTask {
480
451
}
481
452
}
482
453
483
- public extension ComponentLifecycle {
484
- internal struct Task : LifecycleTask {
485
- let label : String
486
- let start : LifecycleHandler
487
- let shutdown : LifecycleHandler
488
-
489
- func start( _ callback: @escaping ( Error ? ) -> Void ) {
490
- self . start. run ( callback)
454
+ extension ComponentLifecycle : LifecycleTasksContainer {
455
+ public func register( _ tasks: [ LifecycleTask ] ) {
456
+ self . stateLock. withLock {
457
+ guard case . idle = self . state else {
458
+ preconditionFailure ( " invalid state, \( self . state) " )
459
+ }
491
460
}
492
-
493
- func shutdown( _ callback: @escaping ( Error ? ) -> Void ) {
494
- self . shutdown. run ( callback)
461
+ self . tasksLock. withLock {
462
+ self . tasks. append ( contentsOf: tasks)
495
463
}
496
464
}
465
+ }
497
466
467
+ /// A container of `LifecycleTask`, used to register additional `LifecycleTask`
468
+ public protocol LifecycleTasksContainer {
498
469
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
499
470
///
500
471
/// - parameters:
501
- /// - tasks: one or more `LifecycleTask`.
502
- func register( _ tasks: LifecycleTask ... ) {
503
- self . register ( tasks)
504
- }
472
+ /// - tasks: array of `LifecycleTask`.
473
+ func register( _ tasks: [ LifecycleTask ] )
474
+ }
505
475
476
+ extension LifecycleTasksContainer {
506
477
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
507
478
///
508
479
/// - parameters:
509
- /// - tasks: array of `LifecycleTask`.
510
- func register( _ tasks: [ LifecycleTask ] ) {
511
- self . stateLock. withLock {
512
- guard case . idle = self . state else {
513
- preconditionFailure ( " invalid state, \( self . state) " )
514
- }
515
- }
516
- self . tasksLock. withLock {
517
- self . tasks. append ( contentsOf: tasks)
518
- }
480
+ /// - tasks: one or more `LifecycleTask`.
481
+ public func register( _ tasks: LifecycleTask ... ) {
482
+ self . register ( tasks)
519
483
}
520
484
521
485
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
@@ -524,16 +488,30 @@ public extension ComponentLifecycle {
524
488
/// - label: label of the item, useful for debugging.
525
489
/// - start: `Handler` to perform the startup.
526
490
/// - shutdown: `Handler` to perform the shutdown.
527
- func register( label: String , start: LifecycleHandler , shutdown: LifecycleHandler ) {
528
- self . register ( Task ( label: label, start: start, shutdown: shutdown) )
491
+ public func register( label: String , start: LifecycleHandler , shutdown: LifecycleHandler ) {
492
+ self . register ( _LifecycleTask ( label: label, start: start, shutdown: shutdown) )
529
493
}
530
494
531
495
/// Adds a `LifecycleTask` to a `LifecycleTasks` collection.
532
496
///
533
497
/// - parameters:
534
498
/// - label: label of the item, useful for debugging.
535
499
/// - handler: `Handler` to perform the shutdown.
536
- func registerShutdown( label: String , _ handler: LifecycleHandler ) {
500
+ public func registerShutdown( label: String , _ handler: LifecycleHandler ) {
537
501
self . register ( label: label, start: . none, shutdown: handler)
538
502
}
539
503
}
504
+
505
+ internal struct _LifecycleTask : LifecycleTask {
506
+ let label : String
507
+ let start : LifecycleHandler
508
+ let shutdown : LifecycleHandler
509
+
510
+ func start( _ callback: @escaping ( Error ? ) -> Void ) {
511
+ self . start. run ( callback)
512
+ }
513
+
514
+ func shutdown( _ callback: @escaping ( Error ? ) -> Void ) {
515
+ self . shutdown. run ( callback)
516
+ }
517
+ }
0 commit comments