Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["plugin_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-resource/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["plugin.go"],
importpath = "go-common/app/interface/main/app-resource/service/plugin",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-resource/conf:go_default_library",
"//app/interface/main/app-resource/dao/plugin:go_default_library",
"//app/interface/main/app-resource/model/plugin:go_default_library",
"//library/log:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,67 @@
package plugin
import (
"context"
"sort"
"time"
"go-common/app/interface/main/app-resource/conf"
pgdao "go-common/app/interface/main/app-resource/dao/plugin"
"go-common/app/interface/main/app-resource/model/plugin"
"go-common/library/log"
)
type Service struct {
pgDao *pgdao.Dao
tick time.Duration
pluginCache map[string][]*plugin.Plugin
}
func New(c *conf.Config) (s *Service) {
s = &Service{
pgDao: pgdao.New(c),
tick: time.Duration(c.Tick),
pluginCache: map[string][]*plugin.Plugin{},
}
s.load()
go s.loadproc()
return
}
func (s *Service) Plugin(build, baseCode, seed int, name string) (pg *plugin.Plugin) {
if build == 0 || seed == 0 || name == "" {
return
}
if ps, ok := s.pluginCache[name]; ok {
for _, p := range ps {
if ((p.Policy == 1 && baseCode == p.BaseCode) || p.Policy == 2 && baseCode >= p.BaseCode) && seed%100 <= p.Coverage && build >= p.MinBuild && ((p.MaxBuild == 0) || (p.MaxBuild != 0 && build <= p.MaxBuild)) {
pg = p
break
}
}
}
return
}
// load cache data
func (s *Service) load() {
psm, err := s.pgDao.All(context.TODO())
if err != nil {
log.Error("s.pgDao.All() error(%v)", err)
return
}
pgCache := make(map[string][]*plugin.Plugin, len(psm))
for name, ps := range psm {
sort.Sort(plugin.Plugins(ps))
pgCache[name] = ps
}
s.pluginCache = pgCache
}
// cacheproc load cache data
func (s *Service) loadproc() {
for {
time.Sleep(s.tick)
s.load()
}
}

View File

@@ -0,0 +1,37 @@
package plugin
import (
"flag"
"path/filepath"
"testing"
"time"
"go-common/app/interface/main/app-resource/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func WithService(f func(s *Service)) func() {
return func() {
f(s)
}
}
func init() {
dir, _ := filepath.Abs("../../cmd/app-resource-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
time.Sleep(time.Second)
}
func TestPlugin(t *testing.T) {
Convey("get Plugin data", t, WithService(func(s *Service) {
res := s.Plugin(1, 1, 1, "")
So(res, ShouldNotBeEmpty)
}))
}