From d5f6ecadda33d5156430314bea95e1578b60d680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 1 Sep 2022 10:30:24 +0800 Subject: [PATCH] - add lock id --- const.go | 2 +- jobs_gorm_lock_id.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 jobs_gorm_lock_id.go diff --git a/const.go b/const.go index 7b125d6..4031162 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,5 @@ package gojobs -const Version = "1.0.70" +const Version = "1.0.71" const SpecifyIpNull = "0.0.0.0" diff --git a/jobs_gorm_lock_id.go b/jobs_gorm_lock_id.go new file mode 100644 index 0000000..380dd74 --- /dev/null +++ b/jobs_gorm_lock_id.go @@ -0,0 +1,23 @@ +package gojobs + +import ( + "context" + "fmt" + "go.dtapp.net/gojobs/jobs_gorm_model" + "time" +) + +// LockId 上锁 +func (j *JobsGorm) LockId(ctx context.Context, info jobs_gorm_model.Task) (string, error) { + return j.lockClient.Lock(ctx, fmt.Sprintf("%s%s%v%s%v", j.config.lockKeyPrefix, j.config.lockKeySeparator, info.Type, j.config.lockKeySeparator, info.Id), fmt.Sprintf("已在%s@%s机器上锁成功", j.config.insideIp, j.config.outsideIp), time.Duration(info.Frequency)*3*time.Second) +} + +// UnlockId Lock 解锁 +func (j *JobsGorm) UnlockId(ctx context.Context, info jobs_gorm_model.Task) error { + return j.lockClient.Unlock(ctx, fmt.Sprintf("%s%s%v%s%v", j.config.lockKeyPrefix, j.config.lockKeySeparator, info.Type, j.config.lockKeySeparator, info.Id)) +} + +// LockForeverId 永远上锁 +func (j *JobsGorm) LockForeverId(ctx context.Context, info jobs_gorm_model.Task) (string, error) { + return j.lockClient.LockForever(ctx, fmt.Sprintf("%s%s%v%s%v", j.config.lockKeyPrefix, j.config.lockKeySeparator, info.Type, j.config.lockKeySeparator, info.Id), fmt.Sprintf("已在%s@%s机器永远上锁成功", j.config.insideIp, j.config.outsideIp)) +}