-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhooks.go
72 lines (67 loc) · 2.16 KB
/
hooks.go
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
package mars
func runStartupHooks() {
for _, hook := range startupHooks {
hook()
}
}
func runShutdownHooks() {
for _, hook := range shutdownHooks {
hook()
}
}
var startupHooks []func()
var shutdownHooks []func()
// Register a function to be run at app startup.
//
// The order you register the functions will be the order they are run.
// You can think of it as a FIFO queue.
// This process will happen after the config file is read
// and before the server is listening for connections.
//
// Ideally, your application should have only one call to init() in the file init.go.
// The reason being that the call order of multiple init() functions in
// the same package is undefined.
// Inside of init() call mars.OnAppStart() for each function you wish to register.
//
// Example:
//
// // from: yourapp/app/controllers/somefile.go
// func InitDB() {
// // do DB connection stuff here
// }
//
// func FillCache() {
// // fill a cache from DB
// // this depends on InitDB having been run
// }
//
// // from: yourapp/app/init.go
// func init() {
// // set up filters...
//
// // register startup functions
// mars.OnAppStart(InitDB)
// mars.OnAppStart(FillCache)
// }
//
// This can be useful when you need to establish connections to databases or third-party services,
// setup app components, compile assets, or any thing you need to do between starting Mars and accepting connections.
//
func OnAppStart(f func()) {
startupHooks = append(startupHooks, f)
}
// OnAppShutdown register a function to be run at app shutdown.
//
// The order you register the functions will be the order they are run.
// You can think of it as a FIFO queue.
// This process will happen after the HTTP servers have stopped listening.
//
// Ideally, your application should have only one call to init() in the file init.go.
// The reason being that the call order of multiple init() functions in
// the same package is undefined.
// Inside of init() call mars.OnAppShutdown() for each function you wish to register.
//
// See also OnAppStart
func OnAppShutdown(f func()) {
shutdownHooks = append(shutdownHooks, f)
}