Update generate_course_blocks management command to enqueue tasks

This commit is contained in:
Nimisha Asthagiri
2017-01-21 16:49:37 -05:00
parent 43c5cb2af8
commit c04f4401dc
5 changed files with 255 additions and 195 deletions

View File

@@ -29,17 +29,37 @@ def update_course_in_cache(course_id):
"""
Updates the course blocks (in the database) for the specified course.
"""
_call_and_retry_if_needed(course_id, api.update_course_in_cache, update_course_in_cache)
@task(
default_retry_delay=settings.BLOCK_STRUCTURES_SETTINGS['BLOCK_STRUCTURES_TASK_DEFAULT_RETRY_DELAY'],
max_retries=settings.BLOCK_STRUCTURES_SETTINGS['BLOCK_STRUCTURES_TASK_MAX_RETRIES'],
)
def get_course_in_cache(course_id):
"""
Gets the course blocks for the specified course, updating the cache if needed.
"""
_call_and_retry_if_needed(course_id, api.get_course_in_cache, get_course_in_cache)
def _call_and_retry_if_needed(course_id, api_method, task_method):
"""
Calls the given api_method with the given course_id, retrying task_method upon failure.
"""
try:
course_key = CourseKey.from_string(course_id)
api.update_course_in_cache(course_key)
api_method(course_key)
except NO_RETRY_TASKS as exc:
# Known unrecoverable errors
raise
except RETRY_TASKS as exc:
log.exception("update_course_in_cache encounted expected error, retrying.")
raise update_course_in_cache.retry(args=[course_id], exc=exc)
log.exception("%s encountered expected error, retrying.", task_method.__name__)
raise task_method.retry(args=[course_id], exc=exc)
except Exception as exc: # pylint: disable=broad-except
log.exception("update_course_in_cache encounted unknown error. Retry #{}".format(
update_course_in_cache.request.retries,
))
raise update_course_in_cache.retry(args=[course_id], exc=exc)
log.exception(
"%s encountered unknown error. Retry #%d",
task_method.__name__,
task_method.request.retries,
)
raise task_method.retry(args=[course_id], exc=exc)

View File

@@ -0,0 +1,48 @@
"""
Useful utilities for management commands.
"""
from django.core.management.base import CommandError
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
def get_mutually_exclusive_required_option(options, option_1, option_2):
"""
Validates that exactly one of the 2 given options is specified.
Returns the name of the found option.
"""
validate_mutually_exclusive_option(options, option_1, option_2)
if not options.get(option_1) and not options.get(option_2):
raise CommandError('Either --{} or --{} must be specified.'.format(option_1, option_2))
return option_1 if options.get(option_1) else option_2
def validate_mutually_exclusive_option(options, option_1, option_2):
"""
Validates that both of the 2 given options are not specified.
"""
if options.get(option_1) and options.get(option_2):
raise CommandError('Both --{} and --{} cannot be specified.'.format(option_1, option_2))
def validate_dependent_option(options, dependent_option, depending_on_option):
"""
Validates that option_1 is specified if dependent_option is specified.
"""
if options.get(dependent_option) and not options.get(depending_on_option):
raise CommandError('Option --{} requires option --{}.'.format(dependent_option, depending_on_option))
def parse_course_keys(course_key_strings):
"""
Parses and returns a list of CourseKey objects from the given
list of course key strings.
"""
try:
return [CourseKey.from_string(course_key_string) for course_key_string in course_key_strings]
except InvalidKeyError as error:
raise CommandError('Invalid key specified: {}'.format(error.message))