Skip to content

Commit 4af7149

Browse files
authored
adding basic gocron-gorm-lock functionality. (#1)
1 parent c72a31e commit 4af7149

File tree

7 files changed

+621
-0
lines changed

7 files changed

+621
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,68 @@
11
# gocron-gorm-lock
22
A gocron locker implementation using gorm
3+
4+
## install
5+
6+
```
7+
go get github.com/go-co-op/gocron-gorm-lock
8+
```
9+
10+
## usage
11+
12+
Here is an example usage that would be deployed in multiple instances
13+
14+
```go
15+
package main
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/go-co-op/gocron"
21+
gormlock "github.com/go-co-op/gocron-gorm-lock"
22+
"gorm.io/gorm"
23+
"time"
24+
)
25+
26+
func main() {
27+
var db * gorm.DB // gorm db connection
28+
var worker string // name of this instance to be used to know which instance run the job
29+
db.AutoMigrate(&CronJobLock{}) // We need the table to store the job execution
30+
locker, err := gormlock.NewGormLocker(db, worker)
31+
if err != nil {
32+
// handle the error
33+
}
34+
35+
s := gocron.NewScheduler(time.UTC)
36+
s.WithDistributedLocker(locker)
37+
38+
_, err = s.Every("1s").Name("unique_name").Do(func() {
39+
// task to do
40+
fmt.Println("call 1s")
41+
})
42+
if err != nil {
43+
// handle the error
44+
}
45+
46+
s.StartBlocking()
47+
}
48+
```
49+
50+
## Prerequisites
51+
52+
- The table cron_job_locks needs to exist in the database. This can be achieved, as an example, using gorm automigrate functionality `db.Automigrate(&CronJobLock{})`
53+
- In order to uniquely identify the job, the locker uses the unique combination of the job name + timestamp (by default with precision to seconds).
54+
55+
## FAQ
56+
57+
- Q: The locker uses the unique combination of the job name + timestamp with seconds precision, how can I change that?
58+
- A: It's possible to set how to create the job identifier, here is an example to set an hour precision:
59+
```go
60+
locker, err := gormlock.NewGormLocker(db, "local",
61+
gormlock.WithJobIdentifier(
62+
func(ctx context.Context, key string) string {
63+
return time.Now().Truncate(60 * time.Minute).
64+
Format("2006-01-02 15:04:05.000")
65+
},
66+
),
67+
)
68+
```

go.mod

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,59 @@
11
module github.com/go-co-op/gocron-gorm-lock
22

33
go 1.20
4+
5+
require (
6+
github.com/go-co-op/gocron v1.31.0
7+
github.com/stretchr/testify v1.8.4
8+
github.com/testcontainers/testcontainers-go v0.22.0
9+
github.com/testcontainers/testcontainers-go/modules/postgres v0.22.0
10+
gorm.io/driver/postgres v1.5.2
11+
gorm.io/gorm v1.25.2
12+
)
13+
14+
require (
15+
dario.cat/mergo v1.0.0 // indirect
16+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
17+
github.com/Microsoft/go-winio v0.6.1 // indirect
18+
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
19+
github.com/containerd/containerd v1.7.3 // indirect
20+
github.com/cpuguy83/dockercfg v0.3.1 // indirect
21+
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/docker/distribution v2.8.2+incompatible // indirect
23+
github.com/docker/docker v24.0.5+incompatible // indirect
24+
github.com/docker/go-connections v0.4.0 // indirect
25+
github.com/docker/go-units v0.5.0 // indirect
26+
github.com/gogo/protobuf v1.3.2 // indirect
27+
github.com/golang/protobuf v1.5.3 // indirect
28+
github.com/google/uuid v1.3.0 // indirect
29+
github.com/jackc/pgpassfile v1.0.0 // indirect
30+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
31+
github.com/jackc/pgx/v5 v5.3.1 // indirect
32+
github.com/jinzhu/inflection v1.0.0 // indirect
33+
github.com/jinzhu/now v1.1.5 // indirect
34+
github.com/klauspost/compress v1.16.0 // indirect
35+
github.com/magiconair/properties v1.8.7 // indirect
36+
github.com/moby/patternmatcher v0.5.0 // indirect
37+
github.com/moby/sys/sequential v0.5.0 // indirect
38+
github.com/moby/term v0.5.0 // indirect
39+
github.com/morikuni/aec v1.0.0 // indirect
40+
github.com/opencontainers/go-digest v1.0.0 // indirect
41+
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
42+
github.com/opencontainers/runc v1.1.5 // indirect
43+
github.com/pkg/errors v0.9.1 // indirect
44+
github.com/pmezard/go-difflib v1.0.0 // indirect
45+
github.com/robfig/cron/v3 v3.0.1 // indirect
46+
github.com/sirupsen/logrus v1.9.0 // indirect
47+
go.uber.org/atomic v1.9.0 // indirect
48+
golang.org/x/crypto v0.8.0 // indirect
49+
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
50+
golang.org/x/mod v0.9.0 // indirect
51+
golang.org/x/net v0.9.0 // indirect
52+
golang.org/x/sys v0.8.0 // indirect
53+
golang.org/x/text v0.9.0 // indirect
54+
golang.org/x/tools v0.7.0 // indirect
55+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
56+
google.golang.org/grpc v1.57.0 // indirect
57+
google.golang.org/protobuf v1.30.0 // indirect
58+
gopkg.in/yaml.v3 v3.0.1 // indirect
59+
)

0 commit comments

Comments
 (0)