Skip to content

Commit

Permalink
adding basic gocron-gorm-lock functionality. (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelarte committed Nov 14, 2023
1 parent c72a31e commit 4af7149
Show file tree
Hide file tree
Showing 7 changed files with 621 additions and 0 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
# gocron-gorm-lock
A gocron locker implementation using gorm

## install

```
go get github.com/go-co-op/gocron-gorm-lock
```

## usage

Here is an example usage that would be deployed in multiple instances

```go
package main

import (
"fmt"

"github.com/go-co-op/gocron"
gormlock "github.com/go-co-op/gocron-gorm-lock"
"gorm.io/gorm"
"time"
)

func main() {
var db * gorm.DB // gorm db connection
var worker string // name of this instance to be used to know which instance run the job
db.AutoMigrate(&CronJobLock{}) // We need the table to store the job execution
locker, err := gormlock.NewGormLocker(db, worker)
if err != nil {
// handle the error
}

s := gocron.NewScheduler(time.UTC)
s.WithDistributedLocker(locker)

_, err = s.Every("1s").Name("unique_name").Do(func() {
// task to do
fmt.Println("call 1s")
})
if err != nil {
// handle the error
}

s.StartBlocking()
}
```

## Prerequisites

- 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{})`
- In order to uniquely identify the job, the locker uses the unique combination of the job name + timestamp (by default with precision to seconds).

## FAQ

- Q: The locker uses the unique combination of the job name + timestamp with seconds precision, how can I change that?
- A: It's possible to set how to create the job identifier, here is an example to set an hour precision:
```go
locker, err := gormlock.NewGormLocker(db, "local",
gormlock.WithJobIdentifier(
func(ctx context.Context, key string) string {
return time.Now().Truncate(60 * time.Minute).
Format("2006-01-02 15:04:05.000")
},
),
)
```
56 changes: 56 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
module github.com/go-co-op/gocron-gorm-lock

go 1.20

require (
github.com/go-co-op/gocron v1.31.0
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.22.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.22.0
gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.2
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/containerd/containerd v1.7.3 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.5+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 4af7149

Please sign in to comment.