fix: seed admin user on startup and run sync as background task

- Seed/update admin user from ADMIN_USERNAME/ADMIN_PASSWORD settings on
  startup (admin_users table was empty, login was impossible)
- POST /api/admin/sync now returns immediately and runs the equipment
  sync via BackgroundTasks (full sync took minutes and hit nginx 504)
- run_sync accepts optional sync_log_id to reuse a pre-created log entry
- Adapt admin router test to background sync behavior
This commit is contained in:
Agent Zero
2026-09-24 21:41:35 +02:00
parent 709190719f
commit e66bcc7058
4 changed files with 77 additions and 18 deletions
+16 -8
View File
@@ -23,10 +23,10 @@ class SyncService:
self.db = db
self.rentman = rentman or RentmanService()
async def run_sync(self) -> dict[str, Any]:
async def run_sync(self, sync_log_id: int | None = None) -> dict[str, Any]:
"""Execute an incremental equipment sync.
1. Create sync_log entry (status=running)
1. Create sync_log entry (status=running) or reuse sync_log_id
2. Paginate GET /equipment from Rentman
3. Compare updateHash with DB, only process changed items
4. Download images only for changed items
@@ -35,12 +35,20 @@ class SyncService:
7. Update sync_log (status=completed or failed)
Returns dict with sync_id, items_processed, status.
"""
log_entry = SyncLog(
sync_type="equipment",
status="running",
started_at=datetime.utcnow(),
)
self.db.add(log_entry)
if sync_log_id is not None:
log_entry = await self.db.get(SyncLog, sync_log_id)
if not log_entry:
log_entry = None
if sync_log_id is None or log_entry is None:
log_entry = SyncLog(
sync_type="equipment",
status="running",
started_at=datetime.utcnow(),
)
self.db.add(log_entry)
else:
log_entry.status = "running"
log_entry.started_at = datetime.utcnow()
await self.db.commit()
await self.db.refresh(log_entry)
sync_id = log_entry.id