Files
MediaCollectorLibary/sync-runner.php
Lars Behrends 552bb72370 snyc...
2025-10-19 22:05:21 +02:00

168 lines
4.9 KiB
PHP

<?php
require_once __DIR__ . '/vendor/autoload.php';
// Load helper functions
require_once __DIR__ . '/app/helpers.php';
// Load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Load database configuration
$dbConfig = require __DIR__ . '/config/database.php';
\App\Database\Database::setConfig($dbConfig);
// Initialize database
try {
$pdo = \App\Database\Database::getInstance();
} catch (Exception $e) {
echo "Database connection failed: " . $e->getMessage() . "\n";
exit(1);
}
use App\Models\Source;
use App\Models\SyncLog;
use App\Services\SteamSyncService;
use App\Services\JellyfinSyncService;
use App\Services\StashSyncService;
use App\Services\XbvrSyncService;
use App\Services\AdultSyncService;
use App\Services\ExophaseSyncService;
// Get command line arguments
$args = $argv;
array_shift($args); // Remove script name
if (count($args) < 3) {
echo "Usage: php sync-runner.php <source_id> <sync_type> <sync_log_id> [--no-output]\n";
echo "Example: php sync-runner.php 1 full 123\n";
echo "Example: php sync-runner.php 2 incremental 124 --no-output\n";
exit(1);
}
$sourceId = (int) $args[0];
$syncType = $args[1];
$syncLogId = (int) $args[2];
$noOutput = in_array('--no-output', $args);
// Initialize PDO connection
try {
$pdo = \App\Database\Database::getInstance();
} catch (Exception $e) {
echo "Database connection failed: " . $e->getMessage() . "\n";
exit(1);
}
// Get source information
$sourceModel = new Source($pdo);
$source = $sourceModel->find($sourceId);
if (!$source) {
echo "Source with ID {$sourceId} not found.\n";
exit(1);
}
// Convert source array to expected format for sync services
$sourceData = [
'id' => $source['id'],
'name' => $source['name'],
'display_name' => $source['display_name'],
'api_url' => $source['api_url'],
'api_key' => $source['api_key'],
'config' => $source['config'],
'is_active' => $source['is_active'],
'last_sync_at' => $source['last_sync_at']
];
// Validate sync type
if ($source['name'] === 'jellyfin') {
$validSyncTypes = ['full', 'incremental', 'all', 'movies', 'tvshows', 'cleanup'];
if (!in_array($syncType, $validSyncTypes)) {
echo "Invalid sync type for Jellyfin source. Valid types: " . implode(', ', $validSyncTypes) . "\n";
exit(1);
}
} else {
$validSyncTypes = ['full', 'incremental'];
if (!in_array($syncType, $validSyncTypes)) {
echo "Invalid sync type. Valid types: " . implode(', ', $validSyncTypes) . "\n";
exit(1);
}
}
// Create appropriate sync service based on source type
$syncService = null;
switch ($source['name']) {
case 'steam':
$syncService = new SteamSyncService($pdo, $sourceData, $syncLogId);
break;
case 'jellyfin':
$syncService = new JellyfinSyncService($pdo, $sourceData, $syncLogId);
break;
case 'stash':
$syncService = new StashSyncService($pdo, $sourceData, $syncLogId);
break;
case 'adult':
$syncService = new AdultSyncService($pdo, $sourceData, $syncLogId);
break;
case 'xbvr':
$syncService = new XbvrSyncService($pdo, $sourceData, $syncLogId);
break;
case 'exophase':
$syncService = new ExophaseSyncService($pdo, $sourceData, $syncLogId);
break;
default:
echo "Unsupported source type: " . $source['name'] . "\n";
exit(1);
}
if (!$noOutput) {
echo "Starting {$syncType} sync for source: " . ($source['display_name'] ?? $source['name']) . "\n";
echo "Source ID: {$sourceId}\n";
echo "Sync Type: {$syncType}\n";
echo "Sync Log ID: {$syncLogId}\n";
echo "Timestamp: " . date('Y-m-d H:i:s') . "\n";
echo "----------------------------------------\n";
}
// Update sync log to running status first
$syncLogModel = new SyncLog($pdo);
$syncLogModel->update($syncLogId, [
'status' => 'running',
'message' => 'Sync process started in background'
]);
// Execute the sync
try {
$returnedSyncLogId = $syncService->startSync($syncType);
if (!$noOutput) {
echo "Sync started with log ID: {$returnedSyncLogId}\n";
echo "Monitor progress at: /admin/sync/status/{$returnedSyncLogId}\n";
echo "Sync completed successfully.\n";
}
exit(0);
} catch (Exception $e) {
if (!$noOutput) {
echo "ERROR: Sync failed - " . $e->getMessage() . "\n";
echo "File: " . $e->getFile() . ":" . $e->getLine() . "\n";
}
// Update sync log as failed (use the returned ID or fall back to original)
$failedSyncLogId = $returnedSyncLogId ?? $syncLogId;
$syncLogModel = new SyncLog($pdo);
$syncLogModel->update($failedSyncLogId, [
'status' => 'failed',
'completed_at' => date('Y-m-d H:i:s'),
'message' => $e->getMessage(),
'errors' => json_encode([
$e->getMessage(),
"File: " . $e->getFile() . ":" . $e->getLine()
])
]);
exit(1);
}