-
Notifications
You must be signed in to change notification settings - Fork 11
/
hook.go
46 lines (37 loc) · 1.26 KB
/
hook.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
package glacier
import (
"fmt"
"reflect"
"github.com/mylxsw/glacier/infra"
)
// PreBind 设置预绑定实例,这里会确保在容器中第一次进行对象实例化之前完成实例绑定
func (impl *framework) PreBind(fn func(binder infra.Binder)) infra.Glacier {
impl.preBinder = fn
return impl
}
// Init set a hook func executed before server initialize
// Usually, we use this method to initialize the log configuration
func (impl *framework) Init(f func(c infra.FlagContext) error) infra.Glacier {
impl.init = f
return impl
}
// OnServerReady call a function on server ready
func (impl *framework) OnServerReady(ffs ...interface{}) {
impl.lock.Lock()
defer impl.lock.Unlock()
if impl.status == Started {
panic(fmt.Errorf("[glacier] can not call OnServerReady since server has been started"))
}
for _, f := range ffs {
fn := newNamedFunc(f)
if reflect.TypeOf(f).Kind() != reflect.Func {
panic(fmt.Errorf("[glacier] argument for OnServerReady [%s] must be a callable function", fn.name))
}
impl.onServerReadyHooks = append(impl.onServerReadyHooks, fn)
}
}
// BeforeServerStop set a hook func executed before server stop
func (impl *framework) BeforeServerStop(f func(cc infra.Resolver) error) infra.Glacier {
impl.beforeServerStop = f
return impl
}