Skip to content

Commit

Permalink
Add NewRedisLockerAlways, always returning a gocron.Locker (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: John Roesler <[email protected]>
  • Loading branch information
addshore and JohnRoesler authored Oct 17, 2023
1 parent c506dab commit 5e75c74
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion redislock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ func NewRedisLocker(r redis.UniversalClient, options ...redsync.Option) (gocron.
if err := r.Ping(context.Background()).Err(); err != nil {
return nil, fmt.Errorf("%s: %w", gocron.ErrFailedToConnectToRedis, err)
}
return newLocker(r, options...), nil
}

// NewRedisLockerAlways provides an implementation of the Locker interface using
// redis for storage, even if the connection fails.
func NewRedisLockerAlways(r redis.UniversalClient, options ...redsync.Option) (gocron.Locker, error) {
return newLocker(r, options...), r.Ping(context.Background()).Err()
}

func newLocker(r redis.UniversalClient, options ...redsync.Option) gocron.Locker {
pool := goredis.NewPool(r)
rs := redsync.New(pool)
return &redisLocker{rs: rs, options: options}, nil
return &redisLocker{rs: rs, options: options}
}

var _ gocron.Locker = (*redisLocker)(nil)
Expand Down

0 comments on commit 5e75c74

Please sign in to comment.