Skip to content

Commit

Permalink
fix: Add error handling for syncing in tasks.py (#2663)
Browse files Browse the repository at this point in the history
This pull request adds error handling for the syncing process in the
tasks.py file. Previously, if an error occurred during the syncing
process, the program would crash. With this change, any exceptions that
occur during syncing will be caught and logged, allowing the program to
continue running. This improves the stability and reliability of the
syncing functionality.

Co-authored-by: Stan Girard <stan@quivr.app>
  • Loading branch information
StanGirard and Stan Girard committed Jun 12, 2024
1 parent 6f95a2d commit 83f06dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
24 changes: 17 additions & 7 deletions backend/modules/sync/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ async def _process_sync_active():
active = await sync_active_service.get_syncs_active_in_interval()

for sync in active:
details_user_sync = sync_user_service.get_sync_user_by_id(sync.syncs_user_id)
if details_user_sync["provider"].lower() == "google":
await google_sync_utils.sync(sync_active_id=sync.id, user_id=sync.user_id)
elif details_user_sync["provider"].lower() == "azure":
await azure_sync_utils.sync(sync_active_id=sync.id, user_id=sync.user_id)
else:
logger.info("Provider not supported: %s", details_user_sync["provider"])
try:
details_user_sync = sync_user_service.get_sync_user_by_id(
sync.syncs_user_id
)
if details_user_sync["provider"].lower() == "google":
await google_sync_utils.sync(
sync_active_id=sync.id, user_id=sync.user_id
)
elif details_user_sync["provider"].lower() == "azure":
await azure_sync_utils.sync(
sync_active_id=sync.id, user_id=sync.user_id
)
else:
logger.info("Provider not supported: %s", details_user_sync["provider"])
except Exception as e:
logger.error(f"Error syncing: {e}")
continue
5 changes: 2 additions & 3 deletions backend/modules/sync/utils/googleutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from google.auth.transport.requests import Request as GoogleRequest
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from logger import get_logger
from modules.brain.repository.brains_vectors import BrainsVectors
from modules.knowledge.repository.storage import Storage
Expand Down Expand Up @@ -125,9 +124,9 @@ async def _upload_files(
# Check if the file already exists in the storage
if check_file_exists(brain_id, file_name):
logger.debug("馃敟 File already exists in the storage: %s", file_name)

self.storage.remove_file(brain_id + "/" + file_name)
BrainsVectors().delete_file_from_brain(brain_id, file_name)


to_upload_file = UploadFile(
file=BytesIO(file_data),
Expand Down Expand Up @@ -166,7 +165,7 @@ async def _upload_files(
)

downloaded_files.append(file_name)
except HttpError as error:
except Exception as error:
logger.error(
"An error occurred while downloading Google Drive files: %s",
error,
Expand Down

0 comments on commit 83f06dc

Please sign in to comment.