A file system synchronization microservice for PictoPy that monitors folder changes and keeps the database in sync with the filesystem.
- 🔍 Real-time File Monitoring: Watches all folders registered in PictoPy database
- 🗄️ Database Integration: Connects to main PictoPy database to get folder information
- 📊 Health Monitoring: Provides health check endpoints for monitoring
- ⚡ Async Processing: Built with FastAPI and async/await for high performance
- 🔄 Hot Reload: Automatically starts watching folders on startup
- Python 3.8+
- PictoPy backend database should be set up and accessible
- Virtual environment (recommended)
-
Navigate to the sync microservice directory:
cd sync-microservice -
Create and activate virtual environment:
python -m venv .sync-env source .sync-env/bin/activate # On Windows: .sync-env\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Start the service:
fastapi dev --port 52124
GET /- Service informationGET /health- Health check with database and watcher statusGET /folders- List all folders being watchedGET /watcher/status- Current watcher status
Health Check:
{
"status": "healthy",
"database": "connected",
"watcher": "running"
}Folders List:
{
"total_folders": 3,
"folders": [
{ "id": "uuid-1", "path": "/path/to/folder1" },
{ "id": "uuid-2", "path": "/path/to/folder2" }
]
}sync-microservice/
├── app/
│ ├── config/
│ │ └── settings.py # Configuration settings
│ ├── database/
│ │ ├── __init__.py
│ │ └── folders.py # Database operations for folders
│ └── utils/
│ ├── __init__.py
│ └── file_watcher.py # File watching implementation
├── main.py # FastAPI application
└── requirements.txt # Dependencies
The service connects to the main PictoPy database. Update app/config/settings.py if needed:
DATABASE_PATH = "../backend/app/database/PictoPy.db"The service automatically:
-
On Startup:
- Connects to PictoPy database
- Retrieves all folder paths and IDs
- Starts watching existing folders
- Reports status
-
During Operation:
- Monitors file changes (add, modify, delete)
- Logs all detected changes
- Maps changes to specific folder IDs
- Prepares for future database sync operations
-
On Shutdown:
- Gracefully stops file watcher
- Cleans up resources
- Database Operations: Add new functions to
app/database/folders.py - File Processing: Extend
app/utils/file_watcher.py - API Endpoints: Add routes to
main.py
The file watcher currently logs changes but doesn't process them. Future enhancements:
- Image file detection and database updates
- Thumbnail generation for new images
- AI processing trigger for new images
- Database cleanup for deleted files
- Integration with main PictoPy backend APIs
Use the health endpoint to monitor service status:
curl http://localhost:52124/health-
Database Connection Failed
- Ensure PictoPy backend database exists
- Check database path in settings.py
- Verify file permissions
-
No Folders to Watch
- Add folders to PictoPy backend first
- Check database has folder entries
- Verify folder paths exist in filesystem
-
Watcher Not Starting
- Check folder permissions
- Ensure folders exist on filesystem
- Review logs for specific errors
The service provides detailed console output for:
- Startup process
- Database connections
- Folder discovery
- File change events
- Error conditions
This microservice is designed to work alongside the main PictoPy backend:
- Port 52123: Main PictoPy backend
- Port 52124: Sync microservice (this service)
- Shared Database: Both services use the same SQLite database
The sync service operates independently but relies on the main backend's database structure.