7 // The list of startup hooks
9 // The startup hooks structure
11 order int // The order
12 f func() // The function to call
15 RevelHooks []RevelHook
19 // The local instance of the list
20 startupHooks RevelHooks
22 // The instance of the list
23 shutdownHooks RevelHooks
26 // Called to run the hooks
27 func (r RevelHooks) Run() {
28 serverLogger.Infof("There is %d hooks need to run ...", len(r))
30 for i, hook := range r {
31 utilLog.Infof("Run the %d hook ...", i+1)
36 // Adds a new function hook, using the order
37 func (r RevelHooks) Add(fn func(), order ...int) RevelHooks {
42 return append(r, RevelHook{order: o, f: fn})
46 func (slice RevelHooks) Len() int {
51 func (slice RevelHooks) Less(i, j int) bool {
52 return slice[i].order < slice[j].order
56 func (slice RevelHooks) Swap(i, j int) {
57 slice[i], slice[j] = slice[j], slice[i]
60 // OnAppStart registers a function to be run at app startup.
62 // The order you register the functions will be the order they are run.
63 // You can think of it as a FIFO queue.
64 // This process will happen after the config file is read
65 // and before the server is listening for connections.
67 // Ideally, your application should have only one call to init() in the file init.go.
68 // The reason being that the call order of multiple init() functions in
69 // the same package is undefined.
70 // Inside of init() call revel.OnAppStart() for each function you wish to register.
74 // // from: yourapp/app/controllers/somefile.go
76 // // do DB connection stuff here
80 // // fill a cache from DB
81 // // this depends on InitDB having been run
84 // // from: yourapp/app/init.go
86 // // set up filters...
88 // // register startup functions
89 // revel.OnAppStart(InitDB)
90 // revel.OnAppStart(FillCache)
93 // This can be useful when you need to establish connections to databases or third-party services,
94 // setup app components, compile assets, or any thing you need to do between starting Revel and accepting connections.
96 func OnAppStart(f func(), order ...int) {
97 startupHooks = startupHooks.Add(f, order...)
100 // Called to add a new stop hook
101 func OnAppStop(f func(), order ...int) {
102 shutdownHooks = shutdownHooks.Add(f, order...)