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,53 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["dao_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/sms/conf:go_default_library",
"//app/service/main/sms/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"databus.go",
"mysql.go",
],
importpath = "go-common/app/service/main/sms/dao",
tags = ["automanaged"],
deps = [
"//app/service/main/sms/conf:go_default_library",
"//app/service/main/sms/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,40 @@
package dao
import (
"context"
"go-common/app/service/main/sms/conf"
xsql "go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
"go-common/library/queue/databus"
)
// Dao struct info of Dao.
type Dao struct {
c *conf.Config
db *xsql.DB
client *bm.Client
databus *databus.Databus
}
// New new a Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: xsql.NewMySQL(c.MySQL),
client: bm.NewClient(c.HTTPClient),
databus: databus.New(c.Databus),
}
return
}
// Ping ping health of db.
func (d *Dao) Ping(ctx context.Context) (err error) {
return d.db.Ping(ctx)
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
d.db.Close()
d.databus.Close()
}

View File

@@ -0,0 +1,67 @@
package dao
import (
"context"
"flag"
"os"
"testing"
"go-common/app/service/main/sms/conf"
"go-common/app/service/main/sms/model"
"github.com/smartystreets/goconvey/convey"
)
var d *Dao
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") == "uat" {
flag.Set("app_id", "main.account.sms-service")
flag.Set("conf_token", "35805e3d0bd411e889af5a488f30b450")
flag.Set("tree_id", "6325")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestDaoTemplateByStatus(t *testing.T) {
convey.Convey("TemplateByStatus", t, func(ctx convey.C) {
var status = model.TemplateStatusApprovel
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.TemplateByStatus(context.Background(), status)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoPubSingle(t *testing.T) {
convey.Convey("PubSingle", t, func(ctx convey.C) {
err := d.PubSingle(context.Background(), &model.ModelSend{})
ctx.Convey("Then err should be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDao_PubBatch(t *testing.T) {
convey.Convey("PubBatch", t, func(ctx convey.C) {
err := d.PubBatch(context.Background(), &model.ModelSend{})
ctx.Convey("Then err should be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,43 @@
package dao
import (
"context"
"time"
"go-common/app/service/main/sms/model"
"go-common/library/log"
)
const _retry = 3
// PubSingle pub single sms to databus
func (d *Dao) PubSingle(ctx context.Context, l *model.ModelSend) (err error) {
for i := 0; i < _retry; i++ {
if err = d.databus.Send(ctx, l.Mobile, l); err == nil {
break
}
time.Sleep(10 * time.Millisecond)
}
if err != nil {
log.Error("PubSingle(%+v) error(%v)", l, err)
return
}
log.Info("PubSingle(%+v) success.", l)
return
}
// PubBatch pub batch sms to databus
func (d *Dao) PubBatch(ctx context.Context, l *model.ModelSend) (err error) {
for i := 0; i < _retry; i++ {
if err = d.databus.Send(ctx, l.Code, l); err == nil {
break
}
time.Sleep(10 * time.Millisecond)
}
if err != nil {
log.Error("PubBatch(%+v) error(%v)", l, err)
return
}
log.Info("PubBatch(%+v) success.", l)
return
}

View File

@@ -0,0 +1,74 @@
package dao
import (
"context"
"database/sql"
"time"
"go-common/app/service/main/sms/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_templateByStatusSQL = "SELECT id,code,stype,template,status,submitter,approver FROM sms_template_new WHERE status=?"
_taskSQL = "SELECT id,type,business_id,template_code,`desc`,file_path,file_name,send_time,status,ctime,mtime FROM sms_tasks WHERE status=? AND send_time<=? LIMIT 1 FOR UPDATE"
_upadteTaskStatusSQL = "UPDATE sms_tasks SET status=? WHERE id=?"
)
// TemplateByStatus select template by status
func (d *Dao) TemplateByStatus(ctx context.Context, status int) (res []*model.ModelTemplate, err error) {
var rows *xsql.Rows
if rows, err = d.db.Query(ctx, _templateByStatusSQL, status); err != nil {
log.Error("d.TemplateByStatus.Query(%d) error(%v)", status, err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.ModelTemplate)
if err = rows.Scan(&r.ID, &r.Code, &r.Stype, &r.Template, &r.Status, &r.Submitter, &r.Approver); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
res = append(res, r)
}
err = rows.Err()
return
}
// BeginTx begin transaction.
func (d *Dao) BeginTx(c context.Context) (*xsql.Tx, error) {
return d.db.Begin(c)
}
// TxTask gets prepared task.
func (d *Dao) TxTask(tx *xsql.Tx) (t *model.ModelTask, err error) {
t = new(model.ModelTask)
if err = tx.QueryRow(_taskSQL, model.TaskStatusPrepared, time.Now()).Scan(&t.ID, &t.Type, &t.BusinessID,
&t.TemplateCode, &t.Desc, &t.FilePath, &t.FileName, &t.SendTime, &t.Status, &t.Ctime, &t.Mtime); err != nil {
t = nil
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("d.TxTask() QueryRow() error(%v)", err)
}
return
}
// TxUpdateTaskStatus updates task status by tx.
func (d *Dao) TxUpdateTaskStatus(tx *xsql.Tx, taskID int64, status int32) (err error) {
if _, err = tx.Exec(_upadteTaskStatusSQL, status, taskID); err != nil {
log.Error("d.TxUpdateTaskStatus() Exec(%s,%d) error(%v)", taskID, status, err)
}
return
}
// UpdateTaskStatus updates task status.
func (d *Dao) UpdateTaskStatus(ctx context.Context, taskID int64, status int32) (err error) {
if _, err = d.db.Exec(ctx, _upadteTaskStatusSQL, status, taskID); err != nil {
log.Error("d.UpdateTaskStatus() Exec(%s,%d) error(%v)", taskID, status, err)
}
return
}