Skip to content
Snippets Groups Projects
Unverified Commit 3b528f44 authored by adeelehsan's avatar adeelehsan Committed by GitHub
Browse files

Merge pull request #21950 from edx/aehsan/prod-735/logged_improverd_cache_miss_hit

Logged improved for cache
parents aa25b4a3 c2600c44
No related merge requests found
...@@ -57,6 +57,7 @@ from openedx.core.djangoapps.content.course_overviews.tests.factories import Cou ...@@ -57,6 +57,7 @@ from openedx.core.djangoapps.content.course_overviews.tests.factories import Cou
from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory from openedx.core.djangoapps.site_configuration.tests.factories import SiteFactory
from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms from openedx.core.djangolib.testing.utils import CacheIsolationTestCase, skip_unless_lms
from student.tests.factories import CourseEnrollmentFactory, UserFactory from student.tests.factories import CourseEnrollmentFactory, UserFactory
from openedx.core.djangoapps.site_configuration.tests.test_util import with_site_configuration_context
UTILS_MODULE = 'openedx.core.djangoapps.catalog.utils' UTILS_MODULE = 'openedx.core.djangoapps.catalog.utils'
User = get_user_model() # pylint: disable=invalid-name User = get_user_model() # pylint: disable=invalid-name
...@@ -83,11 +84,12 @@ class TestGetPrograms(CacheIsolationTestCase): ...@@ -83,11 +84,12 @@ class TestGetPrograms(CacheIsolationTestCase):
# When called before UUIDs are cached, the function should return an # When called before UUIDs are cached, the function should return an
# empty list and log a warning. # empty list and log a warning.
self.assertEqual(get_programs(site=self.site), []) with with_site_configuration_context(domain=self.site.name, configuration={'COURSE_CATALOG_API_URL': 'foo'}):
mock_warning.assert_called_once_with( self.assertEqual(get_programs(site=self.site), [])
u'Failed to get program UUIDs from the cache for site {}.'.format(self.site.domain) mock_warning.assert_called_once_with(
) u'Failed to get program UUIDs from the cache for site {}.'.format(self.site.domain)
mock_warning.reset_mock() )
mock_warning.reset_mock()
# Cache UUIDs for all 3 programs. # Cache UUIDs for all 3 programs.
cache.set( cache.set(
...@@ -158,21 +160,22 @@ class TestGetPrograms(CacheIsolationTestCase): ...@@ -158,21 +160,22 @@ class TestGetPrograms(CacheIsolationTestCase):
mock_cache.get.return_value = [program['uuid'] for program in programs] mock_cache.get.return_value = [program['uuid'] for program in programs]
mock_cache.get_many.side_effect = fake_get_many mock_cache.get_many.side_effect = fake_get_many
actual_programs = get_programs(site=self.site) with with_site_configuration_context(domain=self.site.name, configuration={'COURSE_CATALOG_API_URL': 'foo'}):
actual_programs = get_programs(site=self.site)
# All 3 cached programs should be returned. An info message should be # All 3 cached programs should be returned. An info message should be
# logged about the one that was initially missing, but the code should # logged about the one that was initially missing, but the code should
# be able to stitch together all the details. # be able to stitch together all the details.
self.assertEqual( self.assertEqual(
set(program['uuid'] for program in actual_programs), set(program['uuid'] for program in actual_programs),
set(program['uuid'] for program in all_programs.values()) set(program['uuid'] for program in all_programs.values())
) )
self.assertFalse(mock_warning.called) self.assertFalse(mock_warning.called)
mock_info.assert_called_with('Failed to get details for 1 programs. Retrying.') mock_info.assert_called_with('Failed to get details for 1 programs. Retrying.')
for program in actual_programs: for program in actual_programs:
key = PROGRAM_CACHE_KEY_TPL.format(uuid=program['uuid']) key = PROGRAM_CACHE_KEY_TPL.format(uuid=program['uuid'])
self.assertEqual(program, all_programs[key]) self.assertEqual(program, all_programs[key])
def test_get_one(self, mock_warning, _mock_info): def test_get_one(self, mock_warning, _mock_info):
expected_program = ProgramFactory() expected_program = ProgramFactory()
......
...@@ -119,9 +119,14 @@ def get_programs(site=None, uuid=None, uuids=None, course=None, organization=Non ...@@ -119,9 +119,14 @@ def get_programs(site=None, uuid=None, uuids=None, course=None, organization=Non
# without programs. After this is changed, log any cache misses here. # without programs. After this is changed, log any cache misses here.
return [] return []
elif site: elif site:
uuids = cache.get(SITE_PROGRAM_UUIDS_CACHE_KEY_TPL.format(domain=site.domain), []) site_config = getattr(site, 'configuration', None)
if not uuids: catalog_url = site_config.get_value('COURSE_CATALOG_API_URL') if site_config else None
logger.warning(u'Failed to get program UUIDs from the cache for site {}.'.format(site.domain)) if site_config and catalog_url:
uuids = cache.get(SITE_PROGRAM_UUIDS_CACHE_KEY_TPL.format(domain=site.domain), [])
if not uuids:
logger.warning(u'Failed to get program UUIDs from the cache for site {}.'.format(site.domain))
else:
uuids = []
elif organization: elif organization:
uuids = get_programs_for_organization(organization) uuids = get_programs_for_organization(organization)
if not uuids: if not uuids:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment