-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 multiple richie sites: error on 1st, 2nd is not called
When configured with multiple Richie sites, don't stop on the 1st error. Closes #5
- Loading branch information
Showing
3 changed files
with
101 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 59 additions & 41 deletions
100
richie_openedx_sync/management/commands/sync_courses_to_richie.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,88 @@ | ||
""" | ||
Script for synchronize courses to Richie marketing site | ||
Script to synchronize courses to Richie marketing site | ||
""" | ||
|
||
import logging | ||
|
||
from typing import Dict | ||
from django.core.management.base import BaseCommand | ||
from six import text_type | ||
|
||
from xmodule.modulestore.django import modulestore | ||
from richie_openedx_sync.tasks import sync_course_run_information_to_richie | ||
from opaque_keys.edx.keys import CourseKey | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Synchronize courses from mongo to the Richie marketing site | ||
Command that synchronizes the Open edX courses to the Richie marketing site | ||
""" | ||
|
||
help = ( | ||
'Synchronize courses from mongo to the Richie marketing site, all courses ' | ||
'or a specific course' | ||
"Synchronize courses to the Richie marketing site, by default all courses " | ||
"or a specific course" | ||
) | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--course_id', type=str, default=None, | ||
help='Course id to synchronize, otherwise all courses would be sync') | ||
parser.add_argument( | ||
"--course_id", | ||
type=str, | ||
default=None, | ||
help="Course id to synchronize, otherwise all courses would be sync", | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
""" | ||
Execute the command | ||
Synchronize courses to the Richie marketing site, print to console its sync progress. | ||
""" | ||
course_id = kwargs['course_id'] | ||
courses, failed_courses = synchronize_courses(course_id=course_id) | ||
course_id = kwargs["course_id"] | ||
|
||
print("=" * 80) | ||
print("=" * 30 + "> Synchronization summary") | ||
print(u"Total number of courses to sync: {0}".format(len(courses))) | ||
print(u"Total number of courses which failed to sync: {0}".format(len(failed_courses))) | ||
print("List of sync failed courses ids:") | ||
print("\n".join(failed_courses)) | ||
print("=" * 80) | ||
if not course_id: | ||
module_store = modulestore() | ||
courses = module_store.get_courses() | ||
course_ids = [x.id for x in courses] | ||
else: | ||
course_key = CourseKey.from_string(course_id) | ||
course = modulestore().get_course(course_key) | ||
courses = [course] | ||
course_ids = [course_id] | ||
|
||
course_ids_count = len(course_ids) | ||
total_sync_ok_count = 0 | ||
total_sync_not_ok_count = 0 | ||
|
||
def synchronize_courses(course_id=None): | ||
""" | ||
Export all courses to target directory and return the list of courses which failed to export | ||
""" | ||
if not course_id: | ||
module_store = modulestore() | ||
courses = module_store.get_courses() | ||
course_ids = [x.id for x in courses] | ||
else: | ||
course_key = CourseKey.from_string(course_id) | ||
course = modulestore().get_course(course_key) | ||
courses = [course] | ||
course_ids = [course_id] | ||
for course_id in course_ids: | ||
log.info("-" * 80) | ||
log.info("Synchronizing to Richie course id = {0}".format(course_id)) | ||
sync_result = sync_course_run_information_to_richie( | ||
course_id=str(course_id) | ||
) | ||
ok_count = len(list(filter(lambda e: e[1], sync_result.items()))) | ||
not_ok_count = len(list(filter(lambda e: not e[1], sync_result.items()))) | ||
if ok_count > 0: | ||
log.info(" ok count: {0}".format(ok_count)) | ||
if not_ok_count > 0: | ||
log.info(" not ok count: {0}".format(not_ok_count)) | ||
|
||
failed_courses = [] | ||
richie_failed_backends = str( | ||
list( | ||
map( | ||
lambda e: e[0], | ||
(filter(lambda e: not e[1], sync_result.items())), | ||
) | ||
) | ||
) | ||
log.info(" failed backends: {0}".format(richie_failed_backends)) | ||
|
||
for course_id in course_ids: | ||
print("-" * 80) | ||
print(u"Synchronizing to Richie course id = {0}".format(course_id)) | ||
try: | ||
sync_course_run_information_to_richie(course_id=str(course_id)) | ||
except Exception as err: # pylint: disable=broad-except | ||
failed_courses.append(text_type(course_id)) | ||
print(u"=" * 30 + u"> Oops, failed to sync {0}".format(course_id)) | ||
print("Error:") | ||
print(err) | ||
total_sync_ok_count += ok_count | ||
total_sync_not_ok_count += not_ok_count | ||
|
||
return courses, failed_courses | ||
log.info("=" * 80) | ||
log.info("Synchronization summary") | ||
print("Total number of courses synchronized: {0}".format(course_ids_count)) | ||
log.info("Total number of synchronizations ok: {0}".format(total_sync_ok_count)) | ||
log.info( | ||
"Total number of synchronizations not ok (error): {0}".format(total_sync_not_ok_count) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters