Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update - fix bug:不支持sharding模式的mongo #192

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func main() {

lcf := zap.NewDevelopmentConfig()
lcf.Level.SetLevel(zapcore.Level(*level))
lcf.Development = false
logger, err := lcf.Build(zap.AddCallerSkip(1))
logger, err := lcf.Build(zap.AddCaller(), zap.AddCallerSkip(1))
// logger, err := zap.NewDevelopment()
if err != nil {
slog.Fatalln("new log err:", err.Error())
}
Expand Down
Empty file modified build.sh
100644 → 100755
Empty file.
12 changes: 12 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
client "github.com/coreos/etcd/clientv3"

"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
)

var (
Expand All @@ -22,6 +23,7 @@ type Client struct {
}

func NewClient(cfg *conf.Conf) (c *Client, err error) {
log.Infof("NewClient cfg:%+v", cfg)
cli, err := client.New(cfg.Etcd.Copy())
if err != nil {
return
Expand All @@ -36,12 +38,14 @@ func NewClient(cfg *conf.Conf) (c *Client, err error) {
}

func (c *Client) Put(key, val string, opts ...client.OpOption) (*client.PutResponse, error) {
log.Infof("Put key:%s", key)
ctx, cancel := NewEtcdTimeoutContext(c)
defer cancel()
return c.Client.Put(ctx, key, val, opts...)
}

func (c *Client) PutWithModRev(key, val string, rev int64) (*client.PutResponse, error) {
log.Infof("PutWithModRev key:%s, rev:%d", key, rev)
if rev == 0 {
return c.Put(key, val)
}
Expand All @@ -65,40 +69,47 @@ func (c *Client) PutWithModRev(key, val string, rev int64) (*client.PutResponse,
}

func (c *Client) Get(key string, opts ...client.OpOption) (*client.GetResponse, error) {
log.Infof("Get key:%s", key)
ctx, cancel := NewEtcdTimeoutContext(c)
defer cancel()
return c.Client.Get(ctx, key, opts...)
}

func (c *Client) Delete(key string, opts ...client.OpOption) (*client.DeleteResponse, error) {
log.Infof("Delete key:%s", key)
ctx, cancel := NewEtcdTimeoutContext(c)
defer cancel()
return c.Client.Delete(ctx, key, opts...)
}

func (c *Client) Watch(key string, opts ...client.OpOption) client.WatchChan {
log.Infof("Wath key:%s", key)
return c.Client.Watch(context.Background(), key, opts...)
}

func (c *Client) Grant(ttl int64) (*client.LeaseGrantResponse, error) {
log.Infof("Grant ttl:%d", ttl)
ctx, cancel := NewEtcdTimeoutContext(c)
defer cancel()
return c.Client.Grant(ctx, ttl)
}

func (c *Client) Revoke(id client.LeaseID) (*client.LeaseRevokeResponse, error) {
log.Infof("Revoke id:%d", id)
ctx, cancel := context.WithTimeout(context.Background(), c.reqTimeout)
defer cancel()
return c.Client.Revoke(ctx, id)
}

func (c *Client) KeepAliveOnce(id client.LeaseID) (*client.LeaseKeepAliveResponse, error) {
log.Infof("KeepAliveOnce id:%d", id)
ctx, cancel := NewEtcdTimeoutContext(c)
defer cancel()
return c.Client.KeepAliveOnce(ctx, id)
}

func (c *Client) GetLock(key string, id client.LeaseID) (bool, error) {
log.Infof("GetLock key:%s, id:%d", key, id)
key = conf.Config.Lock + key
ctx, cancel := NewEtcdTimeoutContext(c)
resp, err := DefalutClient.Txn(ctx).
Expand All @@ -115,6 +126,7 @@ func (c *Client) GetLock(key string, id client.LeaseID) (bool, error) {
}

func (c *Client) DelLock(key string) error {
log.Infof("DelLock key:%s", key)
_, err := c.Delete(conf.Config.Lock + key)
return err
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/shunfei/cronsun

go 1.16

require (
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ github.com/cockroachdb/cmux v0.0.0-20170110192607-30d10be49292 h1:dzj1/xcivGjNPw
github.com/cockroachdb/cmux v0.0.0-20170110192607-30d10be49292/go.mod h1:qRiX68mZX1lGBkTWyp3CLcenw9I94W2dLeRvMzcn9N4=
github.com/coreos/bbolt v1.3.0 h1:HIgH5xUWXT914HCI671AxuTTqjj64UOFr7pHn48LUTI=
github.com/coreos/bbolt v1.3.0/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.9+incompatible h1:/pWnp1yEff0z+vBEOBFLZZ22Ux5xoVozEe7X0VFyRNo=
github.com/coreos/etcd v3.3.9+incompatible h1:iKSVPXGNGqroBx4+RmUXv8emeU7y+ucRZSzTYgzLZwM=
github.com/coreos/etcd v3.3.9+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.2.0 h1:3Jm3tLmsgAYcjC+4Up7hJrFBPr+n7rAqYeSw/SZazuY=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
Expand Down
5 changes: 3 additions & 2 deletions job_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const (
Coll_JobLog = "job_log"
Coll_JobLatestLog = "job_latest_log"
Coll_Daily_Stat = "daily_stat"
Coll_Stat = "stat"
)

Expand Down Expand Up @@ -139,7 +140,7 @@ func CreateJobLog(j *Job, t time.Time, rs string, success bool) {
inc["failed"] = 1
}

err := mgoDB.Upsert(Coll_Stat, bson.M{"name": "job-day", "date": time.Now().Format("2006-01-02")}, bson.M{"$inc": inc})
err := mgoDB.Upsert(Coll_Daily_Stat, bson.M{"name": "job-day", "date": time.Now().Format("2006-01-02")}, bson.M{"$inc": inc})
if err != nil {
log.Errorf("increase stat.job %s", err.Error())
}
Expand All @@ -163,7 +164,7 @@ func JobLogStat() (s *StatExecuted, err error) {

func JobLogDailyStat(begin, end time.Time) (ls []*StatExecuted, err error) {
const oneDay = time.Hour * 24
err = mgoDB.WithC(Coll_Stat, func(c *mgo.Collection) error {
err = mgoDB.WithC(Coll_Daily_Stat, func(c *mgo.Collection) error {
dateList := make([]string, 0, 8)

cur := begin
Expand Down