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 Libary Load Failure #94

Merged
merged 3 commits into from
Mar 10, 2024
Merged
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
3 changes: 3 additions & 0 deletions backend/ciso_assistant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ def set_ciso_assistant_url(_, __, event_dict):
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db/ciso-assistant.sqlite3",
"OPTIONS": {
'timeout': 120,
},
}
}
logger.info("DATABASE ENGINE: %s", DATABASES["default"]["ENGINE"])
Expand Down
40 changes: 25 additions & 15 deletions backend/library/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import time
import os
from pathlib import Path
import re
Expand All @@ -19,6 +20,7 @@
from django.db import transaction
from iam.models import Folder

from django.db.utils import OperationalError
import structlog

logger = structlog.get_logger(__name__)
Expand Down Expand Up @@ -205,7 +207,6 @@ def import_requirement_node(self, framework_object: Framework):
ReferenceControl.objects.get(urn=reference_control.lower())
)


# The couple (URN, locale) is unique. ===> Check it in the future
class FrameworkImporter:
REQUIRED_FIELDS = {"ref_id", "urn"}
Expand Down Expand Up @@ -577,27 +578,36 @@ def import_objects(self, library_object):
for risk_matrix in self._risk_matrices:
risk_matrix.import_risk_matrix(library_object)

@transaction.atomic
def _import_library(self) :
library_object = self.create_or_update_library()
self.import_objects(library_object)
library_object.dependencies.set(
Library.objects.filter(
urn__in=self._library_data.get("dependencies", [])
)
)

def import_library(self):
"""Main method to import a library."""
if (error_message := self.init()) is not None:
return error_message

self.check_and_import_dependencies()

try:
with transaction.atomic():
library_object = self.create_or_update_library()
self.import_objects(library_object)
library_object.dependencies.set(
Library.objects.filter(
urn__in=self._library_data.get("dependencies", [])
)
)
except Exception as e:
# TODO: Switch to proper logging
print(f"Library import exception: {e}")
raise

for _ in range(10) :
try:
self._import_library()
break
except OperationalError as e :
if e.args and e.args[0] == 'database is locked' :
time.sleep(1)
else :
raise e
except Exception as e :
# TODO: Switch to proper logging
print(f"Library import exception: {e}")
raise e

def import_library_view(library: dict) -> Union[str, None]:
"""
Expand Down
4 changes: 2 additions & 2 deletions backend/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def destroy(self, request, *args, pk, **kwargs):
if library is None:
return Response(data="Library not found.", status=status.HTTP_404_NOT_FOUND)

if library["reference_count"] != 0:
# "reference_count" is not always defined (is this normal ?)
if library.get("reference_count",0) != 0 :
return Response(
data="Library cannot be deleted because it has references.",
status=status.HTTP_400_BAD_REQUEST,
Expand Down Expand Up @@ -119,7 +120,6 @@ def import_library(self, request, pk=None):
import_library_view(library)
return Response({"status": "success"})
except Exception as e:
print(e)
return Response(
{
"error": "Failed to load library, please check if it has dependencies"
Expand Down
Loading