mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
tada
This commit is contained in:
165
sync-runner.php
Normal file
165
sync-runner.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?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'];
|
||||
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);
|
||||
break;
|
||||
case 'jellyfin':
|
||||
$syncService = new JellyfinSyncService($pdo, $sourceData);
|
||||
break;
|
||||
case 'stash':
|
||||
$syncService = new StashSyncService($pdo, $sourceData);
|
||||
break;
|
||||
case 'adult':
|
||||
$syncService = new AdultSyncService($pdo, $sourceData);
|
||||
break;
|
||||
case 'xbvr':
|
||||
$syncService = new XbvrSyncService($pdo, $sourceData);
|
||||
break;
|
||||
case 'exophase':
|
||||
$syncService = new ExophaseSyncService($pdo, $sourceData);
|
||||
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 {
|
||||
$syncLogId = $syncService->startSync($syncType);
|
||||
|
||||
if (!$noOutput) {
|
||||
echo "Sync started with log ID: {$syncLogId}\n";
|
||||
echo "Monitor progress at: /admin/sync/status/{$syncLogId}\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
|
||||
$syncLogModel->update($syncLogId, [
|
||||
'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);
|
||||
}
|
||||
Reference in New Issue
Block a user