app/system/helper.go

146 lines
3.3 KiB
Go
Raw Permalink Normal View History

2021-12-16 04:11:33 +00:00
package system
import (
"fmt"
"kumoly.io/kumoly/app/util"
)
type BaseService struct{}
func (srv BaseService) GetName() string { return "base" }
2021-12-16 11:11:46 +00:00
func (srv BaseService) GetDependencies() []string { return []string{} }
2021-12-16 04:11:33 +00:00
func (srv BaseService) IsService() bool { return false }
func (srv BaseService) Init() error { return nil }
func (srv BaseService) Load() error { return nil }
func (srv BaseService) Main() error { return nil }
func (srv BaseService) Del() {}
func (srv BaseService) Health() error { return nil }
type EasyBaseService struct {
Name string
Dependencies []string
IsServ bool
}
func (srv EasyBaseService) GetName() string { return srv.Name }
func (srv EasyBaseService) GetDependencies() []string { return srv.Dependencies }
func (srv EasyBaseService) IsService() bool { return srv.IsServ }
func (srv EasyBaseService) Init() error { return nil }
func (srv EasyBaseService) Load() error { return nil }
func (srv EasyBaseService) Main() error { return nil }
func (srv EasyBaseService) Del() {}
func (srv EasyBaseService) Health() error { return nil }
type Inject struct {
Name string
Dependencies []string
IsServ bool
InitFunc func() error
LoadFunc func() error
MainFunc func() error
DelFunc func()
HealthFunc func() error
}
func (srv Inject) GetName() string { return srv.Name }
func (srv Inject) GetDependencies() []string { return srv.Dependencies }
func (srv Inject) IsService() bool { return srv.IsServ }
func (srv Inject) Init() error {
if srv.InitFunc == nil {
return nil
}
return srv.InitFunc()
}
func (srv Inject) Load() error {
if srv.LoadFunc == nil {
return nil
}
return srv.LoadFunc()
}
func (srv Inject) Main() error {
if srv.MainFunc == nil {
return nil
}
return srv.MainFunc()
}
func (srv Inject) Del() {
if srv.HealthFunc == nil {
return
}
srv.DelFunc()
}
func (srv Inject) Health() error {
if srv.HealthFunc == nil {
return nil
}
return srv.HealthFunc()
}
func removeNode(s []*node, i int) []*node {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func removeEdge(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
type node struct {
srv *Service
name string
edge []string
}
func topo(nodes []*node) []*node {
// check
names := map[string]bool{}
for _, v := range nodes {
names[v.name] = true
}
for _, n := range nodes {
for _, e := range n.edge {
if !names[e] {
err := fmt.Errorf("dependency not found: %v", e)
l.Error().Err(err).Msg("")
panic(err)
}
}
}
// start
ret := []*node{}
for i := 0; i <= len(nodes); i++ {
if len(nodes) == 0 {
break
}
if i == len(nodes) {
i = 0
}
if nodes[i].edge == nil || len(nodes[i].edge) == 0 {
n := nodes[i].name
ret = append(ret, nodes[i])
nodes = removeNode(nodes, i)
for j := 0; j < len(nodes); j++ {
for k := 0; k < len(nodes[j].edge); k++ {
if nodes[j].edge[k] == n {
nodes[j].edge = removeEdge(nodes[j].edge, k)
}
}
}
i--
}
}
return ret
}
// Inject a system.Inject and uses caller as name
func (sys *System) Inject(i *Inject) {
if i.Name == "" {
i.Name = util.Caller(2)
}
sys.Append(i)
}