mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
...
This commit is contained in:
@@ -12,6 +12,9 @@ abstract class BaseSyncService
|
||||
protected SyncLog $syncLog;
|
||||
protected int $sourceId;
|
||||
|
||||
protected $logFileHandle;
|
||||
protected $logFilePath;
|
||||
|
||||
public function __construct(\PDO $pdo, array $source)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
@@ -22,17 +25,50 @@ abstract class BaseSyncService
|
||||
}
|
||||
|
||||
$this->sourceId = (int) $source['id'];
|
||||
|
||||
// Create log file for this sync operation
|
||||
$this->initializeLogFile();
|
||||
}
|
||||
|
||||
private function initializeLogFile(): void
|
||||
{
|
||||
$timestamp = date('Y-m-d_H-i-s');
|
||||
$sourceName = strtolower($this->source['name'] ?? 'unknown');
|
||||
$this->logFilePath = "logs/{$sourceName}_sync_{$timestamp}.log";
|
||||
|
||||
// Create logs directory if it doesn't exist
|
||||
$logDir = dirname($this->logFilePath);
|
||||
if (!is_dir($logDir)) {
|
||||
mkdir($logDir, 0755, true);
|
||||
}
|
||||
|
||||
$this->logFileHandle = fopen($this->logFilePath, 'w');
|
||||
if ($this->logFileHandle) {
|
||||
$this->logProgress("=== Starting {$sourceName} sync at " . date('Y-m-d H:i:s') . " ===");
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->logFileHandle) {
|
||||
$this->logProgress("=== Sync completed at " . date('Y-m-d H:i:s') . " ===");
|
||||
fclose($this->logFileHandle);
|
||||
}
|
||||
}
|
||||
|
||||
public function startSync(string $syncType = 'full'): int
|
||||
{
|
||||
// Create sync log entry
|
||||
$this->syncLog = new SyncLog($this->pdo);
|
||||
$syncLogId = $this->createSyncLog($syncType, 'started');
|
||||
// Set higher limits for long-running syncs
|
||||
ini_set('max_execution_time', 3600); // 1 hour
|
||||
ini_set('memory_limit', '512M');
|
||||
|
||||
$this->syncLog->id = $syncLogId;
|
||||
// Create sync log entry
|
||||
$syncLogId = $this->createSyncLog($syncType, 'started');
|
||||
$this->currentSyncLogId = $syncLogId;
|
||||
|
||||
try {
|
||||
$this->logProgress("Starting {$syncType} sync for source: " . ($this->source['display_name'] ?? $this->source['name']));
|
||||
|
||||
$this->executeSync($syncType);
|
||||
|
||||
// Update sync log as completed
|
||||
@@ -40,14 +76,31 @@ abstract class BaseSyncService
|
||||
'processed_items' => $this->getProcessedCount(),
|
||||
'new_items' => $this->getNewCount(),
|
||||
'updated_items' => $this->getUpdatedCount(),
|
||||
'deleted_items' => $this->getDeletedCount()
|
||||
'deleted_items' => $this->getDeletedCount(),
|
||||
'message' => "Successfully completed sync"
|
||||
]);
|
||||
|
||||
$this->logProgress("Sync completed successfully");
|
||||
|
||||
} catch (Exception $e) {
|
||||
// Update sync log as failed
|
||||
// Log the full error details
|
||||
$errorMessage = $e->getMessage();
|
||||
$errorFile = $e->getFile();
|
||||
$errorLine = $e->getLine();
|
||||
$errorTrace = $e->getTraceAsString();
|
||||
|
||||
$this->logProgress("CRITICAL ERROR - Sync failed: {$errorMessage}");
|
||||
$this->logProgress("Error location: {$errorFile}:{$errorLine}");
|
||||
$this->logProgress("Stack trace: {$errorTrace}");
|
||||
|
||||
// Update sync log as failed with full error details
|
||||
$this->updateSyncLog($syncLogId, 'failed', [
|
||||
'message' => $e->getMessage(),
|
||||
'errors' => [$e->getMessage()]
|
||||
'message' => $errorMessage,
|
||||
'errors' => [
|
||||
$errorMessage,
|
||||
"File: {$errorFile}:{$errorLine}",
|
||||
"Stack: " . substr($errorTrace, 0, 1000) // Limit trace size
|
||||
]
|
||||
]);
|
||||
|
||||
throw $e;
|
||||
@@ -80,7 +133,7 @@ abstract class BaseSyncService
|
||||
return (int) $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
private function updateSyncLog(int $syncLogId, string $status, array $stats = []): bool
|
||||
protected function updateSyncLog(int $syncLogId, string $status, array $stats = []): bool
|
||||
{
|
||||
$data = [
|
||||
'status' => $status,
|
||||
@@ -129,13 +182,31 @@ abstract class BaseSyncService
|
||||
return 0; // Override in subclasses
|
||||
}
|
||||
|
||||
protected $currentSyncLogId = null;
|
||||
|
||||
protected function logProgress(string $message): void
|
||||
{
|
||||
// Update sync log with progress message
|
||||
if ($this->syncLog) {
|
||||
$this->updateSyncLog($this->syncLog->id, 'running', [
|
||||
$timestamp = date('H:i:s');
|
||||
$logMessage = "[{$timestamp}] {$message}\n";
|
||||
|
||||
// Write to log file if available
|
||||
if ($this->logFileHandle) {
|
||||
fwrite($this->logFileHandle, $logMessage);
|
||||
}
|
||||
|
||||
// Also write to error log for immediate visibility
|
||||
error_log($message);
|
||||
|
||||
// Update sync log with progress message if we have a current sync log
|
||||
if ($this->currentSyncLogId) {
|
||||
$this->updateSyncLog($this->currentSyncLogId, 'running', [
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getLogFilePath(): string
|
||||
{
|
||||
return $this->logFilePath ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user