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,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"notify_test.go",
"pendant_test.go",
"service_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/usersuit/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"medal.go",
"notify.go",
"pendant.go",
"service.go",
"vip.go",
],
importpath = "go-common/app/job/main/usersuit/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/usersuit/conf:go_default_library",
"//app/job/main/usersuit/dao/medal:go_default_library",
"//app/job/main/usersuit/dao/pendant:go_default_library",
"//app/job/main/usersuit/model:go_default_library",
"//app/service/main/vip/model:go_default_library",
"//library/log:go_default_library",
"//library/queue/databus:go_default_library",
"//vendor/github.com/robfig/cron: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,47 @@
package service
import (
"context"
"runtime/debug"
"go-common/app/job/main/usersuit/model"
"go-common/library/log"
)
var (
_upTaskMedal = map[int64]int64{1: 4, 2: 3, 3: 1, 4: 7, 5: 6, 6: 5, 7: 10, 8: 9, 9: 8}
)
func (s *Service) cronUpNameplate() {
defer func() {
if x := recover(); x != nil {
log.Error("lrucleanproc panic %v : %s", x, debug.Stack())
go s.cronUpNameplate()
}
}()
var (
err error
res *model.UpInfo
ctx = context.TODO()
)
if res, err = s.medalDao.UpInfoData(ctx); err != nil {
log.Error("s.medalDao.UpInfoData err(%+v)", err)
return
}
for _, item := range res.Data {
var (
nid int64
ok bool
)
if nid, ok = _upTaskMedal[item.ID]; !ok {
continue
}
for _, mid := range item.Mids {
log.Info("s.medalDao.AddMedalOwner(%d, %d)", mid, nid)
if err = s.medalDao.Grant(ctx, mid, nid); err != nil {
log.Error("s.medalDao.AddMedalOwner(%d, %d) err(%+v)", mid, nid, err)
}
}
}
}

View File

@@ -0,0 +1,18 @@
package service
import (
"context"
"strconv"
"go-common/app/job/main/usersuit/model"
"go-common/library/log"
)
func (s *Service) accNotify(c context.Context, uid int64, Action string) (err error) {
msg := &model.AccountNotify{UID: uid, Type: "update", Action: Action}
if err = s.accountNotifyPub.Send(c, strconv.FormatInt(msg.UID, 10), msg); err != nil {
log.Error("mid(%d) s.accountNotifyPub.Send(%+v,%s) error(%v)", msg.UID, msg, Action, err)
}
log.Info("mid(%d) s.accountNotifyPub.Send(%+v,%s)", msg.UID, msg, Action)
return
}

View File

@@ -0,0 +1,15 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestService_accNotify(t *testing.T) {
Convey("should return err be nil", t, func() {
err := s.accNotify(context.TODO(), 1, "updateMedal")
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,46 @@
package service
import (
"context"
"time"
"go-common/app/job/main/usersuit/model"
"go-common/library/log"
)
var (
t = time.NewTimer(time.Minute * 5)
)
// startexpireproc start
func (s *Service) startexpireproc() {
for range t.C {
s.expiredEquip(context.TODO())
t.Reset(time.Minute * 10)
}
}
// expiredEquip operator equipment info
func (s *Service) expiredEquip(c context.Context) (err error) {
var (
mids []int64
expires = time.Now().Unix()
affected int64
)
if mids, err = s.pendantDao.ExpireEquipPendant(c, expires); err != nil || len(mids) == 0 {
log.Error("s.pendantDao.ExpireEquipPendant(%d) error(%+v)", expires, err)
return
}
for _, mid := range mids {
if affected, err = s.pendantDao.UpEquipMID(c, mid); err != nil || affected == 0 {
log.Error("s.pendantDao.UpEquipMID(%d) error(%+v)", mid, err)
continue
}
s.pendantDao.DelEquipCache(c, mid)
tid := mid
s.addNotify(func() {
s.accNotify(context.TODO(), tid, model.AccountNotifyUpdatePendant)
})
}
return
}

View File

@@ -0,0 +1,15 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestServiceExpiredEquip(t *testing.T) {
Convey("should return err be nil", t, func() {
err := s.expiredEquip(context.TODO())
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,100 @@
package service
import (
"context"
"sync"
"go-common/app/job/main/usersuit/conf"
medalDao "go-common/app/job/main/usersuit/dao/medal"
pendantDao "go-common/app/job/main/usersuit/dao/pendant"
"go-common/library/log"
"go-common/library/queue/databus"
"github.com/robfig/cron"
)
// Service struct of service.
type Service struct {
pendantDao *pendantDao.Dao
medalDao *medalDao.Dao
// conf
c *conf.Config
accountNotifyPub *databus.Databus
vipBinLogSub *databus.Databus
notifych chan func()
// wait group
wg sync.WaitGroup
}
// New create service instance and return.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
pendantDao: pendantDao.New(c),
medalDao: medalDao.New(c),
accountNotifyPub: databus.New(c.Databus.AccountNotify),
vipBinLogSub: databus.New(c.Databus.VipBinLog),
notifych: make(chan func(), 10240),
}
// this is function
go s.startexpireproc()
s.wg.Add(1)
go s.notifyproc()
s.wg.Add(1)
go s.vipconsumerproc()
t := cron.New()
if len(s.c.Properties.MedalCron) != 0 {
t.AddFunc(s.c.Properties.MedalCron, s.cronUpNameplate)
}
t.Start()
return
}
func (s *Service) addNotify(f func()) {
select {
case s.notifych <- f:
default:
log.Warn("addNotify chan full")
}
}
// notifyproc nofity clear cache
func (s *Service) notifyproc() {
defer s.wg.Done()
for {
f := <-s.notifych
f()
}
}
// Close dao.
func (s *Service) Close() {
if s.pendantDao != nil {
s.pendantDao.Close()
s.medalDao.Close()
}
s.wg.Wait()
}
// Ping check server ok.
func (s *Service) Ping(c context.Context) (err error) {
if err = s.pendantDao.Ping(c); err != nil {
return
}
return
}
// PDTStatStep PDT Stat step
type PDTStatStep struct {
Start, End, Step int64
}
// PDTGHisStep PDT G his
type PDTGHisStep struct {
Start, End, Step int64
}
// PDTOHisStep Order his
type PDTOHisStep struct {
Start, End, Step int64
}

View File

@@ -0,0 +1,21 @@
package service
import (
"flag"
"path/filepath"
"time"
"go-common/app/job/main/usersuit/conf"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../cmd/convey-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
time.Sleep(time.Second)
}

View File

@@ -0,0 +1,105 @@
package service
import (
"context"
"encoding/json"
"time"
"go-common/app/job/main/usersuit/model"
vipmdl "go-common/app/service/main/vip/model"
"go-common/library/log"
)
const (
_vipGid = 31
_vipUserInfoTable = "vip_user_info"
)
func (s *Service) vipconsumerproc() {
defer s.wg.Done()
var (
msgs = s.vipBinLogSub.Messages()
err error
c = context.TODO()
)
for {
msg, ok := <-msgs
if !ok {
log.Error("s.vipBinLogSub.Message closed")
return
}
msg.Commit()
m := &model.Message{}
if err = json.Unmarshal(msg.Value, m); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", string(msg.Value), err)
continue
}
switch m.Table {
case _vipUserInfoTable:
if m.Action == "update" {
s.dealUserPendantEquip(c, m.New, m.Old)
}
default:
log.Warn("vipBinLogConsumer unknown message action(%s)", m.Table)
}
if err != nil {
log.Error("vipBinLogMessage key(%s) value(%s) partition(%d) offset(%d) commit error(%v)", msg.Key, msg.Value, msg.Partition, msg.Offset, err)
continue
}
log.Info("vipBinLogMessage key(%s) value(%s) partition(%d) offset(%d) commit", msg.Key, msg.Value, msg.Partition, msg.Offset)
}
}
func (s *Service) dealUserPendantEquip(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
mr := &model.VipInfoMessage{}
if err = json.Unmarshal(nwMsg, mr); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
var (
gid int64
pe *model.PendantEquip
)
if pe, err = s.pendantDao.PendantEquipMID(c, mr.Mid); err != nil {
log.Error("mid(%d) s.pendantDao.PendantEquipMID error(%v)", mr.Mid, err)
return
}
if pe == nil || pe.Pid == 0 || pe.Expires == 0 {
log.Warn("mid(%d) no equip pendant(%d) expires(%d)", mr.Mid, pe.Pid, pe.Expires)
return
}
if gid, err = s.pendantDao.PendantEquipGidPid(c, pe.Pid); err != nil {
log.Error("mid(%d) pid(%d) s.pendantDao.PendantEquipGidPid error(%v)", mr.Mid, pe.Pid, err)
return
}
if gid != _vipGid {
log.Warn("mid(%d) no equip the vip gid(%d) of pid(%d)", mr.Mid, gid, pe.Pid)
return
}
if mr.VipStatus == vipmdl.VipStatusNotOverTime {
var ts time.Time
if ts, err = time.ParseInLocation(model.TimeFormatSec, mr.VipOverdueTime, time.Local); err != nil {
log.Error("time.ParseInLocation(%s) error(%v)", mr.VipOverdueTime, err)
return
}
if ts.Unix() <= pe.Expires {
log.Warn("mid(%d) pendant equip_time(%d) than vipoverdue_time(%d)", mr.Mid, pe.Expires, ts.Unix())
return
}
if _, err = s.pendantDao.UpEquipExpires(c, mr.Mid, ts.Unix()); err != nil {
log.Error("s.pendantDao.UpEquipExpires(%d,%d) error(%+v)", mr.Mid, ts.Unix(), err)
return
}
} else {
if _, err = s.pendantDao.UpEquipMID(c, mr.Mid); err != nil {
log.Error("s.pendantDao.UpEquipMID(%d) error(%+v)", mr.Mid, err)
return
}
log.Warn("mid(%d) vip status is overtime", mr.Mid)
}
s.pendantDao.DelEquipCache(c, mr.Mid)
s.addNotify(func() {
s.accNotify(context.TODO(), mr.Mid, model.AccountNotifyUpdatePendant)
})
return
}