Skip to content

Commit

Permalink
changing to second precision
Browse files Browse the repository at this point in the history
  • Loading branch information
lv90no committed Nov 12, 2023
1 parent 6eaef8b commit 09627e3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func main() {
## 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 miliseconds).
- 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 miliseconds precision, how can I change that?
- 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",
Expand Down
2 changes: 1 addition & 1 deletion gormlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gorm.io/gorm"
)

var defaultPrecision = time.Millisecond
var defaultPrecision = time.Second

func NewGormLocker(db *gorm.DB, worker string, options ...LockOption) (gocron.Locker, error) {
gl := &gormLocker{db: db, worker: worker}
Expand Down
30 changes: 11 additions & 19 deletions gormlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,30 @@ func TestEnableDistributedLocking(t *testing.T) {
resultChan := make(chan int, 10)
f := func(schedulerInstance int) {
resultChan <- schedulerInstance
println(time.Now().Truncate(defaultPrecision).Format("2006-01-02 15:04:05.000"))
}

s1 := gocron.NewScheduler(time.UTC)
l1, err := NewGormLocker(db, "s1")
require.NoError(t, err)
s1 := gocron.NewScheduler(time.UTC)
s1.WithDistributedLocker(l1)
_, err = s1.Every("500ms").Do(f, 1)
_, err = s1.Every("1s").Do(f, 1)
require.NoError(t, err)

s2 := gocron.NewScheduler(time.UTC)
l2, err := NewGormLocker(db, "s2")
require.NoError(t, err)
s2 := gocron.NewScheduler(time.UTC)
s2.WithDistributedLocker(l2)
_, err = s2.Every("500ms").Do(f, 2)
require.NoError(t, err)

s3 := gocron.NewScheduler(time.UTC)
l3, err := NewGormLocker(db, "s3")
require.NoError(t, err)
s3.WithDistributedLocker(l3)
_, err = s3.Every("500ms").Do(f, 3)
_, err = s2.Every("1s").Do(f, 2)
require.NoError(t, err)

s1.StartAsync()
s2.StartAsync()
s3.StartAsync()

time.Sleep(1700 * time.Millisecond)
time.Sleep(3500 * time.Millisecond)

s1.Stop()
s2.Stop()
s3.Stop()
close(resultChan)

var results []int
Expand Down Expand Up @@ -118,18 +110,18 @@ func TestEnableDistributedLocking_DifferentJob(t *testing.T) {
l1, err := NewGormLocker(db, "s1")
require.NoError(t, err)
s1.WithDistributedLocker(l1)
_, err = s1.Every("500ms").Name("f").Do(f, 1)
_, err = s1.Every("1s").Name("f").Do(f, 1)
require.NoError(t, err)
_, err = s1.Every("500ms").Name("f2").Do(f2, 1)
_, err = s1.Every("1s").Name("f2").Do(f2, 1)
require.NoError(t, err)

s2 := gocron.NewScheduler(time.UTC)
l2, err := NewGormLocker(db, "s2")
require.NoError(t, err)
s2.WithDistributedLocker(l2)
_, err = s2.Every("500ms").Name("f").Do(f, 2)
_, err = s2.Every("1s").Name("f").Do(f, 2)
require.NoError(t, err)
_, err = s2.Every("500ms").Name("f2").Do(f2, 2)
_, err = s2.Every("1s").Name("f2").Do(f2, 2)
require.NoError(t, err)

s3 := gocron.NewScheduler(time.UTC)
Expand All @@ -145,7 +137,7 @@ func TestEnableDistributedLocking_DifferentJob(t *testing.T) {
s2.StartAsync()
s3.StartAsync()

time.Sleep(1700 * time.Millisecond)
time.Sleep(3500 * time.Millisecond)

s1.Stop()
s2.Stop()
Expand Down

0 comments on commit 09627e3

Please sign in to comment.