diff --git a/app/Controllers/AdminController.php b/app/Controllers/AdminController.php index aff1235..bd1aeef 100644 --- a/app/Controllers/AdminController.php +++ b/app/Controllers/AdminController.php @@ -132,26 +132,65 @@ class AdminController extends Controller private function startSync(array $source, string $syncType): int { - // Create appropriate sync service based on source type - switch ($source['name']) { - case 'steam': - $syncService = new SteamSyncService($this->pdo, $source); - break; - case 'jellyfin': - $syncService = new JellyfinSyncService($this->pdo, $source); - break; - case 'stash': - $syncService = new StashSyncService($this->pdo, $source); - break; - case 'adult': - $syncService = new AdultSyncService($this->pdo, $source); - break; - case 'xbvr': - $syncService = new XbvrSyncService($this->pdo, $source); - break; + // Create sync log entry first + $syncLogId = $this->createSyncLog($source, $syncType); + + // Start sync in background process + $this->startBackgroundSync($source['id'], $syncType, $syncLogId); + + return $syncLogId; + } + + private function createSyncLog(array $source, string $syncType): int + { + $data = [ + 'source_id' => $source['id'], + 'sync_type' => $syncType, + 'status' => 'started', + 'total_items' => 0, + 'processed_items' => 0, + 'new_items' => 0, + 'updated_items' => 0, + 'deleted_items' => 0, + 'started_at' => date('Y-m-d H:i:s') + ]; + + $columns = array_keys($data); + $placeholders = array_map(fn($col) => ":$col", $columns); + $sql = "INSERT INTO sync_logs (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")"; + + $stmt = $this->pdo->prepare($sql); + $stmt->execute($data); + + return (int) $this->pdo->lastInsertId(); + } + + private function startBackgroundSync(int $sourceId, string $syncType, int $syncLogId): void + { + $scriptPath = __DIR__ . '/../../sync-runner.php'; + $command = sprintf( + 'php %s %d %s %d > /dev/null 2>&1 &', + escapeshellarg($scriptPath), + $sourceId, + escapeshellarg($syncType), + $syncLogId + ); + + // Execute the command in background + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { + // Windows + pclose(popen('start /B ' . $command, 'r')); + } else { + // Unix-like systems + exec($command); } - // Start sync (this would typically be queued in production) - return $syncService->startSync($syncType); + // */ + // Update sync log to indicate it's running (this will be updated again by the script) + $syncLogModel = new SyncLog($this->pdo); + $syncLogModel->update($syncLogId, [ + 'status' => 'running', + 'message' => 'Sync process starting in background' + ]); } } diff --git a/resources/views/layouts/app.twig b/resources/views/layouts/app.twig index e60e3c4..7bb3412 100644 --- a/resources/views/layouts/app.twig +++ b/resources/views/layouts/app.twig @@ -6,14 +6,26 @@