@@ -35,42 +35,45 @@ import ServiceLauncher
35
35
// initialize the lifecycle container
36
36
var lifecycle = Lifecycle ()
37
37
38
- // register a resource that should be shutdown when the application exists.
39
- // in this case, we are registering a SwiftNIO EventLoopGroup
38
+ // register a resource that should be shut down when the application exits.
39
+ //
40
+ // in this case, we are registering a SwiftNIO `EventLoopGroup`
40
41
// and passing its `syncShutdownGracefully` function to be called on shutdown
41
42
let eventLoopGroup = MultiThreadedEventLoopGroup (numberOfThreads : System.coreCount )
42
43
lifecycle.registerShutdown (
43
44
name : " eventLoopGroup" ,
44
45
eventLoopGroup.syncShutdownGracefully
45
46
)
46
47
47
- // register another resource that should be shutdown when the application exits.
48
- // in this case, we are registering an HTTPClient
48
+ // register another resource that should be shut down when the application exits.
49
+ //
50
+ // in this case, we are registering an `HTTPClient`
49
51
// and passing its `syncShutdown` function to be called on shutdown
50
52
let httpClient = HTTPClient (eventLoopGroupProvider : .shared (eventLoopGroup))
51
53
lifecycle.registerShutdown (
52
- name : " HTTPClient " ,
54
+ name : " httpClient " ,
53
55
httpClient.syncShutdown
54
56
)
55
57
56
58
// start the application
59
+ //
57
60
// start handlers passed using the `register` function
58
- // will be called in the order the items were registered in
61
+ // will be called in the order they were registered in
59
62
lifecycle.start () { error in
60
- // this is the start completion handler.
61
- // if an error occurred you can log it here
63
+ // start completion handler.
64
+ // if a startup error occurred you can capture it here
62
65
if let error = error {
63
66
logger.error (" failed starting \( self ) ☠️: \( error ) " )
64
67
} else {
65
68
logger.info (" \( self ) started successfully 🚀" )
66
69
}
67
70
}
68
- // wait for the application to exist
69
- // this is a blocking operation that typically waits for
70
- // for a signal configured at lifecycle.start (default is `INT` and `TERM`)
71
- // or another thread calling lifecycle.shutdown (atypical)
71
+ // wait for the application to exit
72
+ //
73
+ // this is a blocking operation that typically waits for a signal
74
+ // the signal can be configured at ` lifecycle.start`, and defaults to `INT` and `TERM`
72
75
// shutdown handlers passed using the `register` or `registerShutdown` functions
73
- // will be called in the reverse order the items were registered in
76
+ // will be called in the reverse order they were registered in
74
77
lifecycle.wait ()
75
78
```
76
79
@@ -203,7 +206,7 @@ lifecycle.register(
203
206
start : .eventLoopFuture (foo.start ),
204
207
shutdown : .eventLoopFuture (foo.shutdown )
205
208
)
206
- ```
209
+ ```
207
210
208
211
-------
209
212
0 commit comments