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,54 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"service_test.go",
"wallet_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/live/wallet/conf:go_default_library",
"//app/job/live/wallet/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"service.go",
"wallet.go",
],
importpath = "go-common/app/job/live/wallet/service",
tags = ["automanaged"],
deps = [
"//app/job/live/wallet/conf:go_default_library",
"//app/job/live/wallet/dao:go_default_library",
"//app/job/live/wallet/model:go_default_library",
"//library/log:go_default_library",
"//library/queue/databus: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,101 @@
package Service
import (
"context"
"encoding/json"
"strings"
"sync"
"time"
"go-common/app/job/live/wallet/conf"
"go-common/app/job/live/wallet/dao"
"go-common/app/job/live/wallet/model"
"go-common/library/log"
"go-common/library/queue/databus"
)
// Service struct
type Service struct {
c *conf.Config
dao *dao.Dao
userSub *databus.Databus
waiter *sync.WaitGroup
userUpMo int64
}
// New init
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: dao.New(c),
userSub: databus.New(c.UserSub),
waiter: new(sync.WaitGroup),
}
s.waiter.Add(1)
go s.userCanalConsumeproc()
go s.checkUserCanalConsumeproc()
return s
}
// Ping Service
func (s *Service) Ping(c context.Context) (err error) {
return s.dao.Ping(c)
}
// Close Service
func (s *Service) Close() {
defer s.waiter.Wait()
s.userSub.Close()
s.dao.Close()
}
// Wait goroutinue to close
func (s *Service) Wait() {
s.waiter.Wait()
}
// expCanalConsumeproc consumer archive
func (s *Service) userCanalConsumeproc() {
var (
msgs = s.userSub.Messages()
err error
)
defer s.waiter.Done()
for {
msg, ok := <-msgs
if !ok {
log.Info("userCanal databus Consumer exit")
return
}
s.userUpMo++
msg.Commit()
m := &model.Message{}
//log.Info("canal message %s", msg.Value)
if err = json.Unmarshal(msg.Value, m); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", msg.Value, err)
continue
}
if !strings.HasPrefix(m.Table, "user_") || (m.Action != "update" && m.Action != "insert") {
continue
}
s.mergeData(m.New, m.Old, m.Action)
}
}
// checkConsumeproc check consumer stat
func (s *Service) checkUserCanalConsumeproc() {
if s.c.Env != "pro" {
return
}
var userMo int64
for {
time.Sleep(1 * time.Minute)
if s.userUpMo-userMo == 0 {
msg := "live-wallet-job userCanal did not consume within a minute"
//s.dao.SendSMS(msg)
log.Warn(msg)
}
userMo = s.userUpMo
}
}

View File

@@ -0,0 +1,41 @@
package Service
import (
"context"
"go-common/app/job/live/wallet/conf"
"os"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestMain(m *testing.M) {
conf.ConfPath = "../cmd/live-wallet-test.toml"
once.Do(startService)
os.Exit(m.Run())
}
func TestPing(t *testing.T) {
Convey("Ping", t, func() {
once.Do(startService)
time.Sleep(time.Second)
s.Ping(context.TODO())
})
}
func TestClose(t *testing.T) {
Convey("Close", t, func() {
once.Do(startService)
time.Sleep(time.Second)
s.Close()
})
}
func TestWait(t *testing.T) {
Convey("Wait", t, func() {
once.Do(startService)
time.Sleep(time.Second)
s.Wait()
})
}

View File

@@ -0,0 +1,24 @@
package Service
import (
"context"
"encoding/json"
"go-common/app/job/live/wallet/model"
"go-common/library/log"
)
func (s *Service) mergeData(nwMsg []byte, oldMsg []byte, action string) {
if action == "update" {
// do noting
} else if action == "insert" {
userNew := &model.User{}
if err := json.Unmarshal(nwMsg, userNew); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
return
}
log.Info("new user %d", userNew.Uid)
s.dao.InitWallet(context.TODO(), userNew)
//s.dao.InitWallet(context.TODO(), userNew.Uid, userNew.Gold, userNew.IapGold, userNew.Silver)
s.dao.DelWalletCache(context.TODO(), userNew.Uid)
}
}

View File

@@ -0,0 +1,39 @@
package Service
import (
"fmt"
"sync"
"testing"
"time"
"encoding/json"
"go-common/app/job/live/wallet/conf"
"go-common/app/job/live/wallet/model"
. "github.com/smartystreets/goconvey/convey"
)
var (
once sync.Once
s *Service
)
func startService() {
if err := conf.Init(); err != nil {
panic(fmt.Sprintf("conf.Init() error(%v)", err))
}
s = New(conf.Conf)
}
func TestMergeData(t *testing.T) {
Convey("Cache update", t, func() {
once.Do(startService)
time.Sleep(time.Second)
m := &model.Message{}
user := &model.User{}
m.New, _ = json.Marshal(user)
m.Old, _ = json.Marshal(user)
s.mergeData(m.New, m.Old, "update")
})
}