fix: Make sync operations truly non-blocking with thread pool (#309)

Signed-off-by: phernandez <paul@basicmachines.co>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Paul Hernandez
2025-09-26 10:10:58 -05:00
committed by GitHub
parent f40ab31685
commit 1091e11322
4 changed files with 78 additions and 34 deletions
+16 -9
View File
@@ -101,18 +101,18 @@ async def initialize_file_sync(
# Get active projects
active_projects = await project_repository.get_active_projects()
# First, sync all projects sequentially
for project in active_projects:
# Start sync for all projects as background tasks (non-blocking)
async def sync_project_background(project):
"""Sync a single project in the background."""
# avoid circular imports
from basic_memory.cli.commands.sync import get_sync_service
logger.info(f"Starting sync for project: {project.name}")
sync_service = await get_sync_service(project)
sync_dir = Path(project.path)
logger.info(f"Starting background sync for project: {project.name}")
try:
sync_service = await get_sync_service(project)
sync_dir = Path(project.path)
await sync_service.sync(sync_dir, project_name=project.name)
logger.info(f"Sync completed successfully for project: {project.name}")
logger.info(f"Background sync completed successfully for project: {project.name}")
# Mark project as watching for changes after successful sync
from basic_memory.services.sync_status_service import sync_status_tracker
@@ -120,12 +120,19 @@ async def initialize_file_sync(
sync_status_tracker.start_project_watch(project.name)
logger.info(f"Project {project.name} is now watching for changes")
except Exception as e: # pragma: no cover
logger.error(f"Error syncing project {project.name}: {e}")
logger.error(f"Error in background sync for project {project.name}: {e}")
# Mark sync as failed for this project
from basic_memory.services.sync_status_service import sync_status_tracker
sync_status_tracker.fail_project_sync(project.name, str(e))
# Continue with other projects even if one fails
# Create background tasks for all project syncs (non-blocking)
sync_tasks = [
asyncio.create_task(sync_project_background(project)) for project in active_projects
]
logger.info(f"Created {len(sync_tasks)} background sync tasks")
# Don't await the tasks - let them run in background while we continue
# Then start the watch service in the background
logger.info("Starting watch service for all projects")