go get github.com/go-co-op/gocron-redis-lock/v2
Here is an example usage that would be deployed in multiple instances
package main
import (
"fmt"
"time"
"github.com/go-co-op/gocron/v2"
"github.com/redis/go-redis/v9"
redislock "github.com/go-co-op/gocron-redis-lock/v2"
)
func main() {
redisOptions := &redis.Options{
Addr: "localhost:6379",
}
redisClient := redis.NewClient(redisOptions)
locker, err := redislock.NewRedisLocker(redisClient, redislock.WithTries(1))
if err != nil {
// handle the error
}
s, err := gocron.NewScheduler(gocron.WithDistributedLocker(locker))
if err != nil {
// handle the error
}
_, err = s.NewJob(gocron.DurationJob(500*time.Millisecond), gocron.NewTask(func() {
// task to do
}, 1))
if err != nil {
// handle the error
}
s.Start()
}
The redis UniversalClient can also be used
package main
import (
"fmt"
"time"
"github.com/redis/go-redis/v9"
redislock "github.com/go-co-op/gocron-redis-lock/v2"
)
func main() {
redisOptions := &redis.UniversalOptions{
Addrs: []string{"localhost:6379"},
}
redisClient := redis.NewUniversalClient(redisOptions)
locker, err := redislock.NewRedisLocker(redisClient, redislock.WithTries(1))
if err != nil {
// handle the error
}
}