-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgorm_lock.go
93 lines (75 loc) · 1.82 KB
/
gorm_lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package gormlock
import (
"context"
"sync/atomic"
"time"
"github.com/go-co-op/gocron/v2"
"gorm.io/gorm"
)
func NewGormLocker(db *gorm.DB, worker string, options ...LockOption) (*GormLocker, error) {
if db == nil {
return nil, ErrGormCantBeNull
}
if worker == "" {
return nil, ErrWorkerIsRequired
}
gl := &GormLocker{
db: db,
worker: worker,
ttl: defaultTTL,
interval: defaultCleanInterval,
}
gl.jobIdentifier = defaultJobIdentifier(defaultPrecision)
for _, option := range options {
option(gl)
}
go func() {
ticker := time.NewTicker(gl.interval)
defer ticker.Stop()
for range ticker.C {
if gl.closed.Load() {
return
}
gl.cleanExpiredRecords()
}
}()
return gl, nil
}
var _ gocron.Locker = (*GormLocker)(nil)
type GormLocker struct {
db *gorm.DB
worker string
ttl time.Duration
interval time.Duration
jobIdentifier func(ctx context.Context, key string) string
closed atomic.Bool
}
func (g *GormLocker) cleanExpiredRecords() {
g.db.Where("updated_at < ? and status = ?", time.Now().Add(-g.ttl), StatusFinished).Delete(&CronJobLock{})
}
func (g *GormLocker) Close() {
g.closed.Store(true)
}
func (g *GormLocker) Lock(ctx context.Context, key string) (gocron.Lock, error) {
ji := g.jobIdentifier(ctx, key)
cjb := &CronJobLock{
JobName: key,
JobIdentifier: ji,
Worker: g.worker,
Status: StatusRunning,
}
tx := g.db.WithContext(ctx).Create(cjb)
if tx.Error != nil {
return nil, tx.Error
}
return &gormLock{db: g.db, id: cjb.GetID()}, nil
}
var _ gocron.Lock = (*gormLock)(nil)
type gormLock struct {
db *gorm.DB
// id the id that lock a particular job
id int
}
func (g *gormLock) Unlock(_ context.Context) error {
return g.db.Model(&CronJobLock{ID: g.id}).Updates(&CronJobLock{Status: StatusFinished}).Error
}