Skip to content

Commit

Permalink
chore: adding mongoconnection.
Browse files Browse the repository at this point in the history
  • Loading branch information
awais786 committed May 21, 2024
1 parent bb7d0f2 commit c9d66dd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
27 changes: 20 additions & 7 deletions xmodule/contentstore/mongo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

"""
MongoDB/GridFS-level code for the contentstore.
"""
Expand All @@ -11,7 +12,7 @@
import pymongo
from bson.son import SON
from fs.osfs import OSFS
from gridfs.errors import FileExists, NoFile
from gridfs.errors import NoFile, FileExists
from mongodb_proxy import autoretry_read
from opaque_keys.edx.keys import AssetKey

Expand Down Expand Up @@ -56,7 +57,15 @@ def close_connections(self):
"""
Closes any open connections to the underlying databases
"""
pass
self.fs_files.database.client.close()

def check_connection(self):
connection = self.fs_files.database.client
try:
connection.admin.command("ping")
return True
except pymongo.errors.InvalidOperation:
return False

def _drop_database(self, database=True, collections=True, connections=True):
"""
Expand All @@ -70,8 +79,10 @@ def _drop_database(self, database=True, collections=True, connections=True):
If connections is True, then close the connection to the database as well.
"""
connection = self.fs_files.database.client
if not self.check_connection():
return

connection = self.fs_files.database.client
if database:
connection.drop_database(self.fs_files.database.name)
elif collections:
Expand Down Expand Up @@ -144,15 +155,16 @@ def find(self, location, throw_on_not_found=True, as_stream=False): # lint-amne
thumbnail_location[4]
)

if getattr(fp, 'md5', None) is None:
md5 = getattr(fp, 'md5', None)
if md5 is None:
md5 = hashlib.md5().hexdigest()

return StaticContentStream(
location, fp.displayname, fp.content_type, fp, last_modified_at=fp.uploadDate,
thumbnail_location=thumbnail_location,
import_path=getattr(fp, 'import_path', None),
length=fp.length, locked=getattr(fp, 'locked', False),
content_digest=md5
content_digest=md5,
)
else:
with self.fs.get(content_id) as fp:
Expand All @@ -167,15 +179,16 @@ def find(self, location, throw_on_not_found=True, as_stream=False): # lint-amne
thumbnail_location[4]
)

if getattr(fp, 'md5', None) is None:
md5 = getattr(fp, 'md5', None)
if md5 is None:
md5 = hashlib.md5().hexdigest()

return StaticContent(
location, fp.displayname, fp.content_type, fp.read(), last_modified_at=fp.uploadDate,
thumbnail_location=thumbnail_location,
import_path=getattr(fp, 'import_path', None),
length=fp.length, locked=getattr(fp, 'locked', False),
content_digest=md5
content_digest=md5,
)
except NoFile:
if throw_on_not_found: # lint-amnesty, pylint: disable=no-else-raise
Expand Down
15 changes: 14 additions & 1 deletion xmodule/modulestore/tests/test_mixed_modulestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,27 @@ def setUp(self):
tz_aware=True,
)
self.connection.drop_database(self.DB)
self.addCleanup(self.connection.drop_database, self.DB)
self.addCleanup(self._drop_database)
self.addCleanup(self._close_connection)

# define attrs which get set in initdb to quell pylint
self.writable_chapter_location = self.store = self.fake_location = None
self.course_locations = {}

self.user_id = ModuleStoreEnum.UserID.test

def _drop_database(self):
try:
self.connection.drop_database(self.DB)
except pymongo.errors.InvalidOperation:
pass

def _close_connection(self):
try:
self.connection.close()
except pymongo.errors.InvalidOperation:
pass

def _create_course(self, course_key, asides=None):
"""
Create a course w/ one item in the persistence store using the given course & item location.
Expand Down

0 comments on commit c9d66dd

Please sign in to comment.