Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / revel_hooks.go
1 package revel
2
3 import (
4         "sort"
5 )
6
7 // The list of startup hooks
8 type (
9         // The startup hooks structure
10         RevelHook struct {
11                 order int    // The order
12                 f     func() // The function to call
13         }
14
15         RevelHooks []RevelHook
16 )
17
18 var (
19         // The local instance of the list
20         startupHooks RevelHooks
21
22         // The instance of the list
23         shutdownHooks RevelHooks
24 )
25
26 // Called to run the hooks
27 func (r RevelHooks) Run() {
28         serverLogger.Infof("There is %d hooks need to run ...", len(r))
29         sort.Sort(r)
30         for i, hook := range r {
31                 utilLog.Infof("Run the %d hook ...", i+1)
32                 hook.f()
33         }
34 }
35
36 // Adds a new function hook, using the order
37 func (r RevelHooks) Add(fn func(), order ...int) RevelHooks {
38         o := 1
39         if len(order) > 0 {
40                 o = order[0]
41         }
42         return append(r, RevelHook{order: o, f: fn})
43 }
44
45 // Sorting function
46 func (slice RevelHooks) Len() int {
47         return len(slice)
48 }
49
50 // Sorting function
51 func (slice RevelHooks) Less(i, j int) bool {
52         return slice[i].order < slice[j].order
53 }
54
55 // Sorting function
56 func (slice RevelHooks) Swap(i, j int) {
57         slice[i], slice[j] = slice[j], slice[i]
58 }
59
60 // OnAppStart registers a function to be run at app startup.
61 //
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.
66 //
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.
71 //
72 // Example:
73 //
74 //      // from: yourapp/app/controllers/somefile.go
75 //      func InitDB() {
76 //          // do DB connection stuff here
77 //      }
78 //
79 //      func FillCache() {
80 //          // fill a cache from DB
81 //          // this depends on InitDB having been run
82 //      }
83 //
84 //      // from: yourapp/app/init.go
85 //      func init() {
86 //          // set up filters...
87 //
88 //          // register startup functions
89 //          revel.OnAppStart(InitDB)
90 //          revel.OnAppStart(FillCache)
91 //      }
92 //
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.
95 //
96 func OnAppStart(f func(), order ...int) {
97         startupHooks = startupHooks.Add(f, order...)
98 }
99
100 // Called to add a new stop hook
101 func OnAppStop(f func(), order ...int) {
102         shutdownHooks = shutdownHooks.Add(f, order...)
103 }