Create & Init Project...
This commit is contained in:
55
app/job/main/app-wall/dao/offer/BUILD
Normal file
55
app/job/main/app-wall/dao/offer/BUILD
Normal file
@ -0,0 +1,55 @@
|
||||
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",
|
||||
"redis_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
rundir = ".",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//app/job/main/app-wall/conf:go_default_library",
|
||||
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"dao.go",
|
||||
"redis.go",
|
||||
],
|
||||
importpath = "go-common/app/job/main/app-wall/dao/offer",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/job/main/app-wall/conf:go_default_library",
|
||||
"//library/cache/redis:go_default_library",
|
||||
"//library/ecode:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/http/blademaster:go_default_library",
|
||||
"//vendor/github.com/pkg/errors: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"],
|
||||
)
|
70
app/job/main/app-wall/dao/offer/dao.go
Normal file
70
app/job/main/app-wall/dao/offer/dao.go
Normal file
@ -0,0 +1,70 @@
|
||||
package offer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
"go-common/app/job/main/app-wall/conf"
|
||||
"go-common/library/cache/redis"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
_active = "/x/wall/offer/active2"
|
||||
)
|
||||
|
||||
// Dao dao
|
||||
type Dao struct {
|
||||
c *conf.Config
|
||||
redis *redis.Pool
|
||||
client *bm.Client
|
||||
active string
|
||||
}
|
||||
|
||||
// New init mysql db
|
||||
func New(c *conf.Config) (dao *Dao) {
|
||||
dao = &Dao{
|
||||
c: c,
|
||||
redis: redis.NewPool(c.Redis.Feed.Config),
|
||||
client: bm.NewClient(c.HTTPClient),
|
||||
active: c.Host.APP + _active,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Close close the resource.
|
||||
func (dao *Dao) Close() (err error) {
|
||||
return dao.redis.Close()
|
||||
}
|
||||
|
||||
// Ping dao ping
|
||||
func (dao *Dao) Ping(c context.Context) (err error) {
|
||||
return dao.PingRedis(c)
|
||||
}
|
||||
|
||||
func (d *Dao) Active(c context.Context, os, imei, androidid, mac, ip string) (err error) {
|
||||
params := url.Values{}
|
||||
params.Set("os", os)
|
||||
params.Set("imei", imei)
|
||||
params.Set("androidid", androidid)
|
||||
params.Set("mac", mac)
|
||||
var res struct {
|
||||
Code int `json:"code"`
|
||||
}
|
||||
if err = d.client.Post(c, d.active, ip, params, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if res.Code != ecode.OK.Code() {
|
||||
err = errors.Wrap(ecode.Int(res.Code), d.active+"?"+params.Encode())
|
||||
if res.Code == ecode.RequestErr.Code() {
|
||||
log.Error("%+v", err)
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
36
app/job/main/app-wall/dao/offer/dao_test.go
Normal file
36
app/job/main/app-wall/dao/offer/dao_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package offer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
"go-common/app/job/main/app-wall/conf"
|
||||
)
|
||||
|
||||
var (
|
||||
d *Dao
|
||||
)
|
||||
|
||||
func ctx() context.Context {
|
||||
return context.Background()
|
||||
}
|
||||
|
||||
func init() {
|
||||
dir, _ := filepath.Abs("../../cmd/app-wall-job-test.toml")
|
||||
flag.Set("conf", dir)
|
||||
conf.Init()
|
||||
d = New(conf.Conf)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
func TestActive(t *testing.T) {
|
||||
Convey("Active", t, func() {
|
||||
err := d.Active(ctx(), "", "", "", "", "")
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
56
app/job/main/app-wall/dao/offer/redis.go
Normal file
56
app/job/main/app-wall/dao/offer/redis.go
Normal file
@ -0,0 +1,56 @@
|
||||
package offer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"go-common/library/cache/redis"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
_failList = "act_list"
|
||||
)
|
||||
|
||||
func keyRetry() string {
|
||||
return _failList
|
||||
}
|
||||
|
||||
// PushFail rpush fail item to redis
|
||||
func (d *Dao) PushFail(c context.Context, a interface{}) (err error) {
|
||||
var bs []byte
|
||||
conn := d.redis.Get(c)
|
||||
key := keyRetry()
|
||||
defer conn.Close()
|
||||
if bs, err = json.Marshal(a); err != nil {
|
||||
err = errors.Wrapf(err, "%v", a)
|
||||
return
|
||||
}
|
||||
if _, err = conn.Do("RPUSH", key, bs); err != nil {
|
||||
err = errors.Wrapf(err, "conn.Do(RPUSH,%s,%s)", key, bs)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// PopFail lpop fail item from redis
|
||||
func (d *Dao) PopFail(c context.Context) (bs []byte, err error) {
|
||||
conn := d.redis.Get(c)
|
||||
key := keyRetry()
|
||||
if bs, err = redis.Bytes(conn.Do("LPOP", key)); err != nil {
|
||||
if err == redis.ErrNil {
|
||||
err = nil
|
||||
} else {
|
||||
err = errors.Wrapf(err, "redis.Bytes(conn.Do(LPOP, %s))", key)
|
||||
}
|
||||
}
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Dao) PingRedis(c context.Context) (err error) {
|
||||
var conn = d.redis.Get(c)
|
||||
_, err = conn.Do("SET", "PING", "PONG")
|
||||
conn.Close()
|
||||
return
|
||||
}
|
14
app/job/main/app-wall/dao/offer/redis_test.go
Normal file
14
app/job/main/app-wall/dao/offer/redis_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package offer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestPushFail(t *testing.T) {
|
||||
Convey("PushFail", t, func() {
|
||||
err := d.PushFail(ctx(), "")
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user