diff --git a/README.md b/README.md index a84d31d..9195437 100644 --- a/README.md +++ b/README.md @@ -15,46 +15,40 @@ A distributed cron framework. go get github.com/gochore/dcron ``` -## Example +## Quick Start -First of all, you should implement a distributed atomic operation: +First, implement a distributed atomic operation that only requires support for one method: `SetIfNotExists`. +You can implement it in any way you prefer, such as using Redis `SetNX`. ```go -type Atomic interface { - SetIfNotExists(key, value string) bool -} -``` - -You can implement it any way you like, for example, via Redis `SetNX`: +import "github.com/redis/go-redis/v9" -```go type RedisAtomic struct { client *redis.Client } -func (m *RedisAtomic) SetIfNotExists(key, value string) bool { - ret := m.client.SetNX(key, value, time.Hour) +func (m *RedisAtomic) SetIfNotExists(ctx context.Context, key, value string) bool { + ret := m.client.SetNX(ctx, key, value, time.Hour) return ret.Err() == nil && ret.Val() } ``` -Now we can create a cron with that: +Now you can create a cron with that: ```go - ra := &RedisAtomic{ - // init redis client +func main() { + atomic := &RedisAtomic{ + client: redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + }), } - cron := dcron.NewCron(dcron.WithKey("TestCron"), dcron.WithAtomic(ra)) + cron := dcron.NewCron(dcron.WithKey("TestCron"), dcron.WithAtomic(atomic)) +} ``` -There are many ways to create jobs: -- use `dcron.NewJob`; -- use `dcron.NewJobWithAutoKey`; -- implement interface `dcron.Job`. +Then, create a job and add it to the cron. ```go - -func main { job1 := dcron.NewJob("Job1", "*/15 * * * * *", func(ctx context.Context) error { if task, ok := dcron.TaskFromContext(ctx); ok { log.Println("run:", task.Job.Spec(), task.Key) @@ -62,70 +56,26 @@ func main { // do something return nil }) - job2 := dcron.NewJobWithAutoKey("*/20 * * * * *", Job2) - job3 := Job3{} -} - -func Job2(ctx context.Context) error { - if task, ok := dcron.TaskFromContext(ctx); ok { - log.Println("run:", task.Job.Spec(), task.Key) - } - // do something - return nil -} - -type Job3 struct { -} - -func (j Job3) Key() string { - return "Job3" -} - -func (j Job3) Spec() string { - return "*/30 * * * * *" -} - -func (j Job3) Run(ctx context.Context) error { - if task, ok := dcron.TaskFromContext(ctx); ok { - log.Println("run:", task.Job.Spec(), task.Key) + if err := cron.AddJobs(job1); err != nil { + log.Fatal(err) } - // do something - return nil -} - -func (j Job3) Options() []dcron.JobOption { - return nil -} ``` -Finally, add the jobs to the cron, and start it: +Finally, start the cron: ```go - if err := cron.AddJobs(job1, job2, job3); err != nil { - panic(err) - } - cron.Start() log.Println("cron started") time.Sleep(time.Minute) <-cron.Stop().Done() - log.Println("cron stopped") ``` -You will see logging: - -```text -2020/03/11 15:28:04 cron started -2020/03/11 15:28:15 run: */15 * * * * * dcron:TestCron.Job1@1583911695 -2020/03/11 15:28:20 run: */20 * * * * * dcron:TestCron.Job2@1583911700 -2020/03/11 15:28:30 run: */30 * * * * * dcron:TestCron.Job3@1583911710 -2020/03/11 15:28:30 run: */15 * * * * * dcron:TestCron.Job1@1583911710 -2020/03/11 15:28:40 run: */20 * * * * * dcron:TestCron.Job2@1583911720 -2020/03/11 15:28:45 run: */15 * * * * * dcron:TestCron.Job1@1583911725 -2020/03/11 15:29:00 run: */30 * * * * * dcron:TestCron.Job3@1583911740 -2020/03/11 15:29:00 run: */15 * * * * * dcron:TestCron.Job1@1583911740 -2020/03/11 15:29:00 run: */20 * * * * * dcron:TestCron.Job2@1583911740 -2020/03/11 15:29:04 cron stopped -``` +If you start the program multiple times, you will notice that the cron will run the job once every 15 seconds on only one of the processes. -There is the complete example: [_example/main.go](https://github.com/gochore/dcron/tree/_example/main.go). +| process 1 | process 2 | process 3 | +|------------------------------------------------------------------------|------------------------------------------------------------------------|------------------------------------------------------------------------| +| 2023/10/13 11:39:45 cron started | 2023/10/13 11:39:47 cron started | 2023/10/13 11:39:48 cron started | +| | | 2023/10/13 11:40:00 run: */15 * * * * * dcron:TestCron.Job1@1697168400 | +| | 2023/10/13 11:40:15 run: */15 * * * * * dcron:TestCron.Job1@1697168415 | | +| | | 2023/10/13 11:40:30 run: */15 * * * * * dcron:TestCron.Job1@1697168430 | +| 2023/10/13 11:40:45 run: */15 * * * * * dcron:TestCron.Job1@1697168445 | | | diff --git a/_example/go.mod b/_example/go.mod deleted file mode 100644 index 10dbc48..0000000 --- a/_example/go.mod +++ /dev/null @@ -1,8 +0,0 @@ -module example - -go 1.14 - -require ( - github.com/go-redis/redis/v7 v7.2.0 - github.com/gochore/dcron v1.0.0 -) diff --git a/_example/go.sum b/_example/go.sum deleted file mode 100644 index 0d884cf..0000000 --- a/_example/go.sum +++ /dev/null @@ -1,54 +0,0 @@ -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/go-redis/redis/v7 v7.2.0 h1:CrCexy/jYWZjW0AyVoHlcJUeZN19VWlbepTh1Vq6dJs= -github.com/go-redis/redis/v7 v7.2.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= -github.com/gochore/dcron v1.0.0 h1:hq/7QacSmTqHh6U9Jl0vFV3tO3lY2vT7JCPzU4GHv7o= -github.com/gochore/dcron v1.0.0/go.mod h1:zAQ90N7x0Xtc97/EhCJV+gAHOpMji5jLMJl/tLa/FRU= -github.com/golang/mock v1.4.1 h1:ocYkMQY5RrXTYgXl7ICpV0IXwlEQGwKIsery4gyXa1U= -github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= -github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= -github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME= -github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= -github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= -github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= -golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY= -golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= -gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= -rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/_example/main.go b/_example/main.go deleted file mode 100644 index c4b72db..0000000 --- a/_example/main.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "context" - "log" - "time" - - "github.com/go-redis/redis/v7" - "github.com/gochore/dcron" -) - -type RedisAtomic struct { - client *redis.Client -} - -func (m *RedisAtomic) SetIfNotExists(key, value string) bool { - ret := m.client.SetNX(key, value, time.Hour) - return ret.Err() == nil && ret.Val() -} - -func main() { - ra := &RedisAtomic{ - // init redis client - } - cron := dcron.NewCron(dcron.WithKey("TestCron"), dcron.WithAtomic(ra)) - - job1 := dcron.NewJob("Job1", "*/15 * * * * *", func(ctx context.Context) error { - if task, ok := dcron.TaskFromContext(ctx); ok { - log.Println("run:", task.Job.Spec(), task.Key) - } - // do something - return nil - }) - job2 := dcron.NewJobWithAutoKey("*/20 * * * * *", Job2) - job3 := Job3{} - - if err := cron.AddJobs(job1, job2, job3); err != nil { - panic(err) - } - - cron.Start() - log.Println("cron started") - time.Sleep(time.Minute) - <-cron.Stop().Done() - log.Println("cron stopped") -} - -func Job2(ctx context.Context) error { - if task, ok := dcron.TaskFromContext(ctx); ok { - log.Println("run:", task.Job.Spec(), task.Key) - } - // do something - return nil -} - -type Job3 struct { -} - -func (j Job3) Key() string { - return "Job3" -} - -func (j Job3) Spec() string { - return "*/30 * * * * *" -} - -func (j Job3) Run(ctx context.Context) error { - if task, ok := dcron.TaskFromContext(ctx); ok { - log.Println("run:", task.Job.Spec(), task.Key) - } - // do something - return nil -} - -func (j Job3) Options() []dcron.JobOption { - return nil -} diff --git a/atomic.go b/atomic.go index 51eb414..db18401 100644 --- a/atomic.go +++ b/atomic.go @@ -1,6 +1,10 @@ package dcron -//go:generate mockgen -source=atomic.go -destination mock_dcron/atomic.go +import "context" + +//go:generate go get go.uber.org/mock/mockgen +//go:generate go run go.uber.org/mock/mockgen -source=atomic.go -destination mock_dcron/atomic.go +//go:generate go mod tidy // Atomic provides distributed atomic operation for dcron, // it can be implemented easily via Redis/SQL and so on. @@ -9,5 +13,5 @@ type Atomic interface { // or does nothing and return false. // Note that the key/value should be kept for at least one minute. // For example, `SetNX(key, value, time.Minute)` via redis. - SetIfNotExists(key, value string) bool + SetIfNotExists(ctx context.Context, key, value string) bool } diff --git a/cron_test.go b/cron_test.go index 4b7fb89..39d122c 100644 --- a/cron_test.go +++ b/cron_test.go @@ -6,10 +6,10 @@ import ( "testing" "time" - "github.com/golang/mock/gomock" - "github.com/robfig/cron/v3" - "github.com/gochore/dcron/mock_dcron" + + "github.com/robfig/cron/v3" + "go.uber.org/mock/gomock" ) func Test_Cron(t *testing.T) { @@ -20,7 +20,7 @@ func Test_Cron(t *testing.T) { c := NewCron(WithKey("test_cron"), WithAtomic(atomic)) atomic.EXPECT(). - SetIfNotExists(gomock.Any(), c.Hostname()). + SetIfNotExists(gomock.Any(), gomock.Any(), c.Hostname()). Return(true). Times(2) @@ -304,8 +304,8 @@ func Test_JobWithGroup(t *testing.T) { c := NewCron(WithKey("test_cron"), WithAtomic(atomic)) atomic.EXPECT(). - SetIfNotExists(gomock.Any(), c.Hostname()). - DoAndReturn(func(key, value interface{}) bool { + SetIfNotExists(gomock.Any(), gomock.Any(), c.Hostname()). + DoAndReturn(func(ctx context.Context, key, value string) bool { time.Sleep(time.Duration(rand.Int63n(int64(time.Millisecond)))) return true }). diff --git a/entry_getter.go b/entry_getter.go index 5ec0f15..2b5330d 100644 --- a/entry_getter.go +++ b/entry_getter.go @@ -2,7 +2,9 @@ package dcron import "github.com/robfig/cron/v3" -//go:generate mockgen -source=entry_getter.go -destination mock_dcron/entry_getter.go +//go:generate go get go.uber.org/mock/mockgen +//go:generate go run go.uber.org/mock/mockgen -source=entry_getter.go -destination mock_dcron/entry_getter.go +//go:generate go mod tidy type entryGetter interface { Entry(id cron.EntryID) cron.Entry diff --git a/go.mod b/go.mod index 73e9732..a86546f 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/gochore/dcron go 1.21.3 require ( - github.com/golang/mock v1.6.0 github.com/robfig/cron/v3 v3.0.1 + go.uber.org/mock v0.3.0 ) diff --git a/go.sum b/go.sum index 56ff122..58e65b3 100644 --- a/go.sum +++ b/go.sum @@ -1,27 +1,4 @@ -github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= -github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo= +go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= diff --git a/inner_job.go b/inner_job.go index 4a2a2a2..57c3217 100644 --- a/inner_job.go +++ b/inner_job.go @@ -77,7 +77,7 @@ func (j *innerJob) Run() { if !task.Skipped { checkAtomic := func() bool { - return j.noMutex || j.cron.atomic == nil || j.cron.atomic.SetIfNotExists(task.Key, c.hostname) + return j.noMutex || j.cron.atomic == nil || j.cron.atomic.SetIfNotExists(ctx, task.Key, c.hostname) } needExec := false if j.group != nil { diff --git a/inner_job_test.go b/inner_job_test.go index 46ae673..6029722 100644 --- a/inner_job_test.go +++ b/inner_job_test.go @@ -9,8 +9,8 @@ import ( "github.com/gochore/dcron/mock_dcron" - "github.com/golang/mock/gomock" "github.com/robfig/cron/v3" + "go.uber.org/mock/gomock" ) func Test_innerJob_Key(t *testing.T) { @@ -78,8 +78,8 @@ func Test_innerJob_Run(t *testing.T) { MinTimes(1) atomic.EXPECT(). - SetIfNotExists(gomock.Any(), gomock.Any()). - DoAndReturn(func(key, value string) bool { + SetIfNotExists(gomock.Any(), gomock.Any(), gomock.Any()). + DoAndReturn(func(ctx context.Context, key, value string) bool { return value != "always_miss" }). MinTimes(1) diff --git a/mock_dcron/atomic.go b/mock_dcron/atomic.go index 3630426..19c7480 100644 --- a/mock_dcron/atomic.go +++ b/mock_dcron/atomic.go @@ -1,47 +1,53 @@ // Code generated by MockGen. DO NOT EDIT. // Source: atomic.go - +// +// Generated by this command: +// +// mockgen -source=atomic.go -destination mock_dcron/atomic.go +// // Package mock_dcron is a generated GoMock package. package mock_dcron import ( - gomock "github.com/golang/mock/gomock" + context "context" reflect "reflect" + + gomock "go.uber.org/mock/gomock" ) -// MockAtomic is a mock of Atomic interface +// MockAtomic is a mock of Atomic interface. type MockAtomic struct { ctrl *gomock.Controller recorder *MockAtomicMockRecorder } -// MockAtomicMockRecorder is the mock recorder for MockAtomic +// MockAtomicMockRecorder is the mock recorder for MockAtomic. type MockAtomicMockRecorder struct { mock *MockAtomic } -// NewMockAtomic creates a new mock instance +// NewMockAtomic creates a new mock instance. func NewMockAtomic(ctrl *gomock.Controller) *MockAtomic { mock := &MockAtomic{ctrl: ctrl} mock.recorder = &MockAtomicMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockAtomic) EXPECT() *MockAtomicMockRecorder { return m.recorder } -// SetIfNotExists mocks base method -func (m *MockAtomic) SetIfNotExists(key, value string) bool { +// SetIfNotExists mocks base method. +func (m *MockAtomic) SetIfNotExists(ctx context.Context, key, value string) bool { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetIfNotExists", key, value) + ret := m.ctrl.Call(m, "SetIfNotExists", ctx, key, value) ret0, _ := ret[0].(bool) return ret0 } -// SetIfNotExists indicates an expected call of SetIfNotExists -func (mr *MockAtomicMockRecorder) SetIfNotExists(key, value interface{}) *gomock.Call { +// SetIfNotExists indicates an expected call of SetIfNotExists. +func (mr *MockAtomicMockRecorder) SetIfNotExists(ctx, key, value any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetIfNotExists", reflect.TypeOf((*MockAtomic)(nil).SetIfNotExists), key, value) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetIfNotExists", reflect.TypeOf((*MockAtomic)(nil).SetIfNotExists), ctx, key, value) } diff --git a/mock_dcron/entry_getter.go b/mock_dcron/entry_getter.go index 2afd204..faba83d 100644 --- a/mock_dcron/entry_getter.go +++ b/mock_dcron/entry_getter.go @@ -1,39 +1,44 @@ // Code generated by MockGen. DO NOT EDIT. // Source: entry_getter.go - +// +// Generated by this command: +// +// mockgen -source=entry_getter.go -destination mock_dcron/entry_getter.go +// // Package mock_dcron is a generated GoMock package. package mock_dcron import ( - gomock "github.com/golang/mock/gomock" - cron "github.com/robfig/cron/v3" reflect "reflect" + + cron "github.com/robfig/cron/v3" + gomock "go.uber.org/mock/gomock" ) -// MockentryGetter is a mock of entryGetter interface +// MockentryGetter is a mock of entryGetter interface. type MockentryGetter struct { ctrl *gomock.Controller recorder *MockentryGetterMockRecorder } -// MockentryGetterMockRecorder is the mock recorder for MockentryGetter +// MockentryGetterMockRecorder is the mock recorder for MockentryGetter. type MockentryGetterMockRecorder struct { mock *MockentryGetter } -// NewMockentryGetter creates a new mock instance +// NewMockentryGetter creates a new mock instance. func NewMockentryGetter(ctrl *gomock.Controller) *MockentryGetter { mock := &MockentryGetter{ctrl: ctrl} mock.recorder = &MockentryGetterMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockentryGetter) EXPECT() *MockentryGetterMockRecorder { return m.recorder } -// Entry mocks base method +// Entry mocks base method. func (m *MockentryGetter) Entry(id cron.EntryID) cron.Entry { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Entry", id) @@ -41,8 +46,8 @@ func (m *MockentryGetter) Entry(id cron.EntryID) cron.Entry { return ret0 } -// Entry indicates an expected call of Entry -func (mr *MockentryGetterMockRecorder) Entry(id interface{}) *gomock.Call { +// Entry indicates an expected call of Entry. +func (mr *MockentryGetterMockRecorder) Entry(id any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Entry", reflect.TypeOf((*MockentryGetter)(nil).Entry), id) }