Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RedisCluster support #262

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions redbeat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def get_redis(app=None):
if not hasattr(app, 'redbeat_redis') or app.redbeat_redis is None:
redis_options = conf.redbeat_redis_options
retry_period = redis_options.get('retry_period')
if conf.redis_url.startswith('redis-sentinel') and 'sentinels' in redis_options:
if redis_options.get('cluster', False):
from redis.cluster import RedisCluster

connection = RedisCluster.from_url(conf.redis_url, **redis_options)
elif conf.redis_url.startswith('redis-sentinel') and 'sentinels' in redis_options:
from redis.sentinel import Sentinel

sentinel = Sentinel(
Expand Down Expand Up @@ -205,7 +209,7 @@ def __init__(
**clsargs,
):
super().__init__(
name=name,
name=str(name),
task=task,
schedule=schedule,
args=args,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
from copy import deepcopy
from datetime import datetime, timedelta
from unittest import mock
from unittest.mock import Mock, patch

import pytz
Expand Down Expand Up @@ -200,6 +201,24 @@ def test_sentinel_scheduler(self):
assert 'Sentinel' not in str(redis_client.connection_pool)


class ClusterRedBeatCase(AppCase):
config_dict = {
'BROKER_URL': 'redis://',
'REDBEAT_REDIS_OPTIONS': {
'cluster': True,
},
}

def setup(self):
self.app.conf.update(self.config_dict)

def test_sentinel_scheduler(self):
# Fake redis doesn't really support redis cluster, but let's just make sure it was used.
with mock.patch('redis.RedisCluster.from_url') as from_url:
get_redis(app=self.app)
self.assertTrue(from_url.called)


class SentinelRedBeatCase(AppCase):
config_dict = {
'REDBEAT_KEY_PREFIX': 'rb-tests:',
Expand Down