-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Application.swift
152 lines (127 loc) · 4.43 KB
/
Application.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import NIO
public final class Application {
public var environment: Environment
public let eventLoopGroup: EventLoopGroup
public var userInfo: [AnyHashable: Any]
public let sync: Lock
private let configure: (inout Services) throws -> ()
private let threadPool: NIOThreadPool
private var didShutdown: Bool
public var running: Running? {
get {
self.sync.lock()
defer { self.sync.unlock() }
return self._running
}
set {
self.sync.lock()
defer { self.sync.unlock() }
self._running = newValue
}
}
private var _running: Running?
public var logger: Logger
private var _services: Services!
public struct Running {
public var stop: () -> Void
public init(stop: @escaping () -> Void) {
self.stop = stop
}
}
public init(
environment: Environment = .development,
configure: @escaping (inout Services) throws -> () = { _ in }
) {
self.environment = environment
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
self.userInfo = [:]
self.didShutdown = false
self.configure = configure
self.sync = Lock()
self.threadPool = .init(numberOfThreads: 1)
self.threadPool.start()
self.logger = .init(label: "codes.vapor.application")
}
public func makeServices() throws -> Services {
var s = Services.default()
try self.configure(&s)
s.register(Application.self) { c in
return self
}
s.register(NIOThreadPool.self) { c in
return self.threadPool
}
return s
}
public func makeContainer() -> EventLoopFuture<Container> {
return self.makeContainer(on: self.eventLoopGroup.next())
}
public func makeContainer(on eventLoop: EventLoop) -> EventLoopFuture<Container> {
do {
return try _makeContainer(on: eventLoop)
} catch {
return self.eventLoopGroup.next().makeFailedFuture(error)
}
}
private func _makeContainer(on eventLoop: EventLoop) throws -> EventLoopFuture<Container> {
let s = try self.makeServices()
return Container.boot(environment: self.environment, services: s, on: eventLoop)
}
// MARK: Run
public func boot() throws {
self._services = try self.makeServices()
try self._services.providers.forEach { try $0.willBoot(self) }
try self._services.providers.forEach { try $0.didBoot(self) }
}
public func run() throws {
self.logger = .init(label: "codes.vapor.application")
defer { self.shutdown() }
do {
try self.runCommands()
} catch {
self.logger.report(error: error)
throw error
}
}
public func runCommands() throws {
let eventLoop = self.eventLoopGroup.next()
try self.loadDotEnv(on: eventLoop).wait()
let c = try self.makeContainer(on: eventLoop).wait()
defer { c.shutdown() }
let command = try c.make(Commands.self).group()
let console = try c.make(Console.self)
try console.run(command, input: self.environment.commandInput)
}
private func loadDotEnv(on eventLoop: EventLoop) -> EventLoopFuture<Void> {
return DotEnvFile.load(
path: ".env",
fileio: .init(threadPool: self.threadPool),
on: eventLoop
).recover { error in
self.logger.debug("Could not load .env file: \(error)")
}
}
public func shutdown() {
self.logger.debug("Application shutting down")
if self._services != nil {
self._services.providers.forEach { $0.willShutdown(self) }
}
do {
try self.eventLoopGroup.syncShutdownGracefully()
} catch {
self.logger.error("EventLoopGroup failed to shutdown: \(error)")
}
do {
try self.threadPool.syncShutdownGracefully()
} catch {
self.logger.error("ThreadPool failed to shutdown: \(error)")
}
self.didShutdown = true
self.userInfo = [:]
}
deinit {
if !self.didShutdown {
assertionFailure("Application.shutdown() was not called before Application deinitialized.")
}
}
}