feat: add lock

Browse Source
main
git 2024-05-24 22:57:16 +08:00
parent e5f43aabf8
commit 9e0756f984
Signed by: git
GPG Key ID: 3F65EFFA44207ADD
2 changed files with 63 additions and 2 deletions

View File

@ -11,7 +11,6 @@ import (
redisbackend "github.com/RichardKnop/machinery/v2/backends/redis" redisbackend "github.com/RichardKnop/machinery/v2/backends/redis"
"github.com/RichardKnop/machinery/v2/config" "github.com/RichardKnop/machinery/v2/config"
"github.com/RichardKnop/machinery/v2/example/tracers" "github.com/RichardKnop/machinery/v2/example/tracers"
eagerlock "github.com/RichardKnop/machinery/v2/locks/eager"
"github.com/RichardKnop/machinery/v2/log" "github.com/RichardKnop/machinery/v2/log"
"github.com/RichardKnop/machinery/v2/tasks" "github.com/RichardKnop/machinery/v2/tasks"
"github.com/google/uuid" "github.com/google/uuid"
@ -21,6 +20,7 @@ import (
etcdbroker "github.com/ifooth/machinery-plugins/brokers/etcd" etcdbroker "github.com/ifooth/machinery-plugins/brokers/etcd"
exampletasks "github.com/ifooth/machinery-plugins/examples/tasks" exampletasks "github.com/ifooth/machinery-plugins/examples/tasks"
etcdlock "github.com/ifooth/machinery-plugins/locks/etcd"
) )
var ( var (
@ -80,7 +80,10 @@ func startServer() (*machinery.Server, error) {
} }
// lock := redislock.New(cnf, []string{"localhost:6379"}, 3, 2) // lock := redislock.New(cnf, []string{"localhost:6379"}, 3, 2)
lock := eagerlock.New() lock, err := etcdlock.New(cnf, "http://127.0.0.1:2379")
if err != nil {
return nil, err
}
backend := redisbackend.NewGR(cnf, []string{"localhost:6379"}, 3) backend := redisbackend.NewGR(cnf, []string{"localhost:6379"}, 3)
server := machinery.NewServer(cnf, broker, backend, lock) server := machinery.NewServer(cnf, broker, backend, lock)
@ -199,5 +202,11 @@ func send() error {
log.INFO.Println("Single task:", asyncResult.Signature.UUID) log.INFO.Println("Single task:", asyncResult.Signature.UUID)
if err := server.RegisterPeriodicTask("* * * * *", "hello", &addTask0); err != nil {
return err
}
time.Sleep(time.Second * 120)
return nil return nil
} }

52
locks/etcd/etcd.go Normal file
View File

@ -0,0 +1,52 @@
package etcd
import (
"errors"
"time"
"github.com/RichardKnop/machinery/v2/config"
"github.com/RichardKnop/machinery/v2/locks/iface"
"github.com/RichardKnop/machinery/v2/log"
clientv3 "go.etcd.io/etcd/client/v3"
)
var (
ErrRedisLockFailed = errors.New("redis lock: failed to acquire lock")
)
type etcdLock struct {
globalConf *config.Config
conf clientv3.Config
cli *clientv3.Client
retries int
interval time.Duration
}
func New(cnf *config.Config, endpoint string) (iface.Lock, error) {
etcdConf := clientv3.Config{Endpoints: []string{endpoint}}
cli, err := clientv3.New(etcdConf)
if err != nil {
return nil, err
}
lock := etcdLock{
globalConf: cnf,
conf: etcdConf,
cli: cli,
}
return &lock, nil
}
func (r etcdLock) LockWithRetries(key string, unixTsToExpireNs int64) error {
err := errors.New("LockWithRetries not implemented")
log.ERROR.Print(err)
return err
}
func (r etcdLock) Lock(key string, unixTsToExpireNs int64) error {
err := errors.New("Lock not implemented")
log.ERROR.Print(err)
return err
}