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

adding basic gocron-gorm-lock functionality. #1

Merged
merged 8 commits into from
Nov 14, 2023
Merged
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
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