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

fix disabling of distributed lock (#2) #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 14 additions & 5 deletions redbeat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def get_redis(app=None):
class RedBeatConfig(object):
def __init__(self, app=None):
self.app = app_or_default(app)
self.key_prefix = self.either_or('redbeat_key_prefix', 'redbeat:')
self.key_prefix = self.key_has_value_or('redbeat_key_prefix', 'redbeat:')
self.schedule_key = self.key_prefix + ':schedule'
self.statics_key = self.key_prefix + ':statics'
self.lock_key = self.either_or('redbeat_lock_key', self.key_prefix + ':lock')
self.lock_timeout = self.either_or('redbeat_lock_timeout', None)
self.redis_url = self.either_or('redbeat_redis_url', app.conf['BROKER_URL'])
self.lock_key = self.key_present_or('redbeat_lock_key', self.key_prefix + ':lock')
self.lock_timeout = self.key_has_value_or('redbeat_lock_timeout', None)
self.redis_url = self.key_has_value_or('redbeat_redis_url', app.conf['BROKER_URL'])

@property
def schedule(self):
Expand All @@ -159,15 +159,24 @@ def schedule(self, value):
else:
self.app.conf.CELERYBEAT_SCHEDULE = value

def either_or(self, name, default=None):
def warn_if_wrong_format(self, name):
if CELERY_4_OR_GREATER and name == name.upper():
warnings.warn(
'Celery v4 installed, but detected Celery v3 '
'configuration %s (use %s instead).' % (name, name.lower()),
UserWarning
)
def key_has_value_or(self, name, default=None):
self.warn_if_wrong_format(name)
return self.app.conf.first(name, name.upper()) or default

def key_present_or(self, name, default=None):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be tests for this function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely. I'll add one.

self.warn_if_wrong_format(name)
if name.upper() in map(lambda k: k.upper(), self.app.conf.keys()):
return self.app.conf.first(name, name.upper())
else:
return default


class RedBeatSchedulerEntry(ScheduleEntry):
_meta = None
Expand Down
26 changes: 22 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ def test_lock_timeout(self):
def test_key_prefix_default(self):
self.assertEqual(self.conf.key_prefix, 'redbeat:')

def test_lock_key_default(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check my understanding, is this the same test as in https://github.com/sibson/redbeat/pull/109/files#diff-423890b7e3c2aa4791f9e203b7f194cbR45? If so I don't think we need to remove it, being explicit here is great.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this tests the same thing and was added to be explicit.

self.assertTrue("REDBEAT_LOCK_KEY" not in self.app.conf.keys())
self.assertTrue("redbeat_lock_key" not in self.app.conf.keys())
self.conf = RedBeatConfig(self.app)
self.assertEqual(self.conf.lock_key, 'redbeat::lock')

def test_disable_lock_key_4(self):
self.app.conf.redbeat_lock_key = None
self.assertTrue("redbeat_lock_key" in self.app.conf.keys())
self.conf = RedBeatConfig(self.app)
self.assertEqual(self.conf.lock_key, None)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a @pytest.mark.skipif(CELERY_4_OR_GREATER, reason="requires Celery < 4.x") here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Will add the skipif

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've dropped celery 3 support so this maybe simpler to fix.

def test_disable_lock_key_3(self):
self.app.conf.REDBEAT_LOCK_KEY = None
self.assertTrue("REDBEAT_LOCK_KEY" in self.app.conf.keys())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? It seems to be testing the assignment we did in the previous line or am I missing some behind the scenes magic?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes. I'll remove this.

self.conf = RedBeatConfig(self.app)
self.assertEqual(self.conf.lock_key, None)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a test_custom_lock_key() or is that covered elsewhere somehow?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. I'll add a test for a custom lock key.

def test_other_keys(self):
self.assertEqual(self.conf.schedule_key, self.conf.key_prefix + ':schedule')
self.assertEqual(self.conf.statics_key, self.conf.key_prefix + ':statics')
Expand All @@ -45,14 +63,14 @@ def test_schedule(self):

@pytest.mark.skipif(CELERY_4_OR_GREATER, reason="requires Celery < 4.x")
@mock.patch('warnings.warn')
def test_either_or_3(self, warn_mock):
broker_url = self.conf.either_or('BROKER_URL')
def test_key_has_value_or_3(self, warn_mock):
broker_url = self.conf.key_has_value_or('BROKER_URL')
self.assertFalse(warn_mock.called)
self.assertEqual(broker_url, self.app.conf.BROKER_URL)

@pytest.mark.skipif(not CELERY_4_OR_GREATER, reason="requires Celery >= 4.x")
@mock.patch('warnings.warn')
def test_either_or_4(self, warn_mock):
broker_url = self.conf.either_or('BROKER_URL')
def test_key_has_value_or_4(self, warn_mock):
broker_url = self.conf.key_has_value_or('BROKER_URL')
self.assertTrue(warn_mock.called)
self.assertEqual(broker_url, self.app.conf.broker_url)