From 83f06dc5d2b6fdf1bddead1eb5aaf3ec62dc39c7 Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Wed, 12 Jun 2024 11:42:16 +0200 Subject: [PATCH] fix: Add error handling for syncing in tasks.py (#2663) 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 --- backend/modules/sync/tasks.py | 24 ++++++++++++++++------- backend/modules/sync/utils/googleutils.py | 5 ++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/backend/modules/sync/tasks.py b/backend/modules/sync/tasks.py index 0140de7cfc6a..9420c9432cee 100644 --- a/backend/modules/sync/tasks.py +++ b/backend/modules/sync/tasks.py @@ -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 diff --git a/backend/modules/sync/utils/googleutils.py b/backend/modules/sync/utils/googleutils.py index e1ce89910fc8..6b4e1d6433f0 100644 --- a/backend/modules/sync/utils/googleutils.py +++ b/backend/modules/sync/utils/googleutils.py @@ -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 @@ -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), @@ -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,