-
-
Notifications
You must be signed in to change notification settings - Fork 625
Merge sync microservice into main backend #1101
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
Closed
Nakshatra480
wants to merge
17
commits into
AOSSIE-Org:main
from
Nakshatra480:feat/unify-backend-architecture
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c6d8c9b
Add file watcher utilities
29480ab
Add watcher schemas for API responses
b5bd471
Add watcher API routes
6ccebd8
Update backend config for unified architecture
045eb37
Integrate watcher into backend lifespan
eb8cd2e
Update API utility to call integrated watcher
e1ce7f8
Update frontend to use unified backend
7e61c4a
Remove sync-microservice from setup script
0f3836e
refactor(watcher): optimize utils and lock shared state
2ce80fb
fix(watcher): prevent blocking and handle errors in routes
9f5f07c
fix(backend): correct logger init and graceful shutdown
d718268
fix(watcher): resolve critical threading issues
b4b440a
fix(watcher): eliminate race conditions in folder access
8869e3b
fix(watcher): prevent race in wait_for_watcher thread access
c37a340
fix(watcher): serialize lifecycle ops and fix is_watcher_running race
dab37a1
fix(watcher): skip syncing folders scheduled for deletion
f8cd4f1
fix(watcher): deduplicate deleted folder IDs before deletion
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import asyncio | ||
| from fastapi import APIRouter, HTTPException | ||
| from app.utils.watcher import ( | ||
| watcher_util_start_folder_watcher, | ||
| watcher_util_stop_folder_watcher, | ||
| watcher_util_restart_folder_watcher, | ||
| watcher_util_is_watcher_running, | ||
| watcher_util_get_watcher_info, | ||
| ) | ||
| from app.schemas.watcher import ( | ||
| WatcherStatusResponse, | ||
| WatcherControlResponse, | ||
| ) | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.get("/status", response_model=WatcherStatusResponse) | ||
| async def get_watcher_status(): | ||
| """Get folder watcher status.""" | ||
| try: | ||
| watcher_info = await asyncio.to_thread(watcher_util_get_watcher_info) | ||
| return WatcherStatusResponse(**watcher_info) | ||
| except Exception as e: | ||
| raise HTTPException( | ||
| status_code=500, detail=f"Error getting watcher status: {str(e)}" | ||
| ) | ||
|
|
||
|
|
||
| @router.post("/restart", response_model=WatcherControlResponse) | ||
| async def restart_watcher(): | ||
| """Restart the folder watcher with fresh data from database.""" | ||
| try: | ||
| success = await asyncio.to_thread(watcher_util_restart_folder_watcher) | ||
| watcher_info = await asyncio.to_thread(watcher_util_get_watcher_info) | ||
| if success: | ||
| return WatcherControlResponse( | ||
| success=True, | ||
| message="Folder watcher restarted successfully", | ||
| watcher_info=WatcherStatusResponse(**watcher_info), | ||
| ) | ||
| else: | ||
| return WatcherControlResponse( | ||
| success=False, | ||
| message="Failed to restart folder watcher", | ||
| watcher_info=WatcherStatusResponse(**watcher_info), | ||
| ) | ||
| except Exception as e: | ||
| raise HTTPException( | ||
| status_code=500, detail=f"Error restarting watcher: {str(e)}" | ||
| ) | ||
|
|
||
|
|
||
| @router.post("/stop", response_model=WatcherControlResponse) | ||
| async def stop_watcher(): | ||
| """Stop the folder watcher.""" | ||
| try: | ||
| await asyncio.to_thread(watcher_util_stop_folder_watcher) | ||
| watcher_info = await asyncio.to_thread(watcher_util_get_watcher_info) | ||
| return WatcherControlResponse( | ||
| success=True, | ||
| message="Folder watcher stopped", | ||
| watcher_info=WatcherStatusResponse(**watcher_info), | ||
| ) | ||
| except Exception as e: | ||
| raise HTTPException(status_code=500, detail=f"Error stopping watcher: {str(e)}") | ||
|
|
||
|
|
||
| @router.post("/start", response_model=WatcherControlResponse) | ||
| async def start_watcher(): | ||
| """Start the folder watcher.""" | ||
| try: | ||
| is_running = await asyncio.to_thread(watcher_util_is_watcher_running) | ||
| if is_running: | ||
| watcher_info = await asyncio.to_thread(watcher_util_get_watcher_info) | ||
| return WatcherControlResponse( | ||
| success=False, | ||
| message="Watcher is already running", | ||
| watcher_info=WatcherStatusResponse(**watcher_info), | ||
| ) | ||
|
|
||
| success = await asyncio.to_thread(watcher_util_start_folder_watcher) | ||
| watcher_info = await asyncio.to_thread(watcher_util_get_watcher_info) | ||
| if success: | ||
| return WatcherControlResponse( | ||
| success=True, | ||
| message="Folder watcher started successfully", | ||
| watcher_info=WatcherStatusResponse(**watcher_info), | ||
| ) | ||
| else: | ||
| return WatcherControlResponse( | ||
| success=False, | ||
| message="Failed to start folder watcher", | ||
| watcher_info=WatcherStatusResponse(**watcher_info), | ||
| ) | ||
| except Exception as e: | ||
| raise HTTPException(status_code=500, detail=f"Error starting watcher: {str(e)}") | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from pydantic import BaseModel | ||
| from typing import List, Optional | ||
|
|
||
|
|
||
| class WatchedFolder(BaseModel): | ||
| """Schema for a watched folder.""" | ||
|
|
||
| id: str | ||
| path: str | ||
|
|
||
|
|
||
| class WatcherStatusResponse(BaseModel): | ||
| """Watcher status endpoint response schema.""" | ||
|
|
||
| is_running: bool | ||
| folders_count: int | ||
| thread_alive: bool | ||
| thread_id: Optional[int] | ||
| watched_folders: List[WatchedFolder] | ||
|
|
||
|
|
||
| class WatcherControlResponse(BaseModel): | ||
| """Schema for watcher control operations (start/stop/restart).""" | ||
|
|
||
| success: bool | ||
| message: str | ||
| watcher_info: WatcherStatusResponse | ||
|
|
||
|
|
||
| class WatcherErrorResponse(BaseModel): | ||
| """Schema for watcher error responses.""" | ||
|
|
||
| detail: str |
This file contains hidden or 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,33 +1,23 @@ | ||
| import requests | ||
| from app.config.settings import SYNC_MICROSERVICE_URL | ||
| import logging | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def API_util_restart_sync_microservice_watcher(): | ||
| """ | ||
| Send a POST request to restart the sync microservice watcher. | ||
| Restart the folder watcher (now integrated into the backend). | ||
|
|
||
| Returns: | ||
| bool: True if request was successful, False otherwise | ||
| bool: True if restart was successful, False otherwise | ||
| """ | ||
| try: | ||
| url = f"{SYNC_MICROSERVICE_URL}/watcher/restart" | ||
| response = requests.post(url, timeout=30) | ||
|
|
||
| if response.status_code == 200: | ||
| logger.info("Successfully restarted sync microservice watcher") | ||
| from app.utils.watcher import watcher_util_restart_folder_watcher | ||
| success = watcher_util_restart_folder_watcher() | ||
| if success: | ||
| logger.info("Successfully restarted folder watcher") | ||
| return True | ||
| else: | ||
| logger.warning( | ||
| f"Failed to restart sync microservice watcher. Status code: {response.status_code}" | ||
| ) | ||
| logger.warning("Failed to restart folder watcher") | ||
| return False | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| logger.error(f"Error communicating with sync microservice: {e}") | ||
| return False | ||
| except Exception as e: | ||
| logger.error(f"Unexpected error restarting sync microservice watcher: {e}") | ||
| logger.error(f"Unexpected error restarting folder watcher: {e}") | ||
| return False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.