mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
snyc...
This commit is contained in:
@@ -51,10 +51,20 @@ abstract class BaseSyncService
|
||||
{
|
||||
if ($this->logFileHandle) {
|
||||
$this->logProgress("=== Sync completed at " . date('Y-m-d H:i:s') . " ===");
|
||||
$this->updateSyncLog($this->currentSyncLogId, 'completed', [
|
||||
'processed_items' => $this->getProcessedCount(),
|
||||
'new_items' => $this->getNewCount(),
|
||||
'updated_items' => $this->getUpdatedCount(),
|
||||
'deleted_items' => $this->getDeletedCount(),
|
||||
'message' => $this->getCompletionMessage()
|
||||
]);
|
||||
fclose($this->logFileHandle);
|
||||
}
|
||||
}
|
||||
|
||||
protected $deletedCount = 0;
|
||||
protected $totalItems = 0;
|
||||
|
||||
public function startSync(string $syncType = 'full'): int
|
||||
{
|
||||
// Set higher limits for long-running syncs
|
||||
@@ -79,7 +89,18 @@ abstract class BaseSyncService
|
||||
try {
|
||||
$this->logProgress("Starting {$syncType} sync for source: " . ($this->source['display_name'] ?? $this->source['name']));
|
||||
|
||||
$this->executeSync($syncType);
|
||||
// Execute cleanup if requested
|
||||
if ($syncType === 'cleanup') {
|
||||
$this->executeCleanup();
|
||||
} else {
|
||||
$this->executeSync($syncType);
|
||||
|
||||
// Optionally run cleanup after regular sync
|
||||
if (in_array($syncType, ['full', 'incremental'])) {
|
||||
$this->logProgress("Running cleanup after sync...");
|
||||
$this->executeCleanup();
|
||||
}
|
||||
}
|
||||
|
||||
// Update sync log as completed
|
||||
$this->updateSyncLog($syncLogId, 'completed', [
|
||||
@@ -87,10 +108,12 @@ abstract class BaseSyncService
|
||||
'new_items' => $this->getNewCount(),
|
||||
'updated_items' => $this->getUpdatedCount(),
|
||||
'deleted_items' => $this->getDeletedCount(),
|
||||
'message' => "Successfully completed sync"
|
||||
'total_items' => $this->getTotalItems(),
|
||||
'message' => $this->getCompletionMessage()
|
||||
]);
|
||||
|
||||
$this->logProgress("Sync completed successfully");
|
||||
// Log completion to file but don't update database (already completed above)
|
||||
$this->logProgressToFile("Sync completed successfully");
|
||||
|
||||
} catch (Exception $e) {
|
||||
// Log the full error details
|
||||
@@ -119,6 +142,12 @@ abstract class BaseSyncService
|
||||
return $syncLogId;
|
||||
}
|
||||
|
||||
protected function executeCleanup(): void
|
||||
{
|
||||
// Override in subclasses to implement cleanup logic
|
||||
$this->logProgress("Cleanup not implemented for this sync service");
|
||||
}
|
||||
|
||||
private function createSyncLog(string $syncType, string $status): int
|
||||
{
|
||||
$data = [
|
||||
@@ -162,6 +191,10 @@ abstract class BaseSyncService
|
||||
$data['message'] = $stats['message'];
|
||||
}
|
||||
|
||||
if (isset($stats['total_items'])) {
|
||||
$data['total_items'] = $stats['total_items'];
|
||||
}
|
||||
|
||||
$setClause = array_map(fn($col) => "$col = :$col", array_keys($data));
|
||||
$sql = "UPDATE sync_logs SET " . implode(', ', $setClause) . " WHERE id = :id";
|
||||
$data['id'] = $syncLogId;
|
||||
@@ -189,11 +222,53 @@ abstract class BaseSyncService
|
||||
|
||||
protected function getDeletedCount(): int
|
||||
{
|
||||
return 0; // Override in subclasses
|
||||
return $this->deletedCount; // Return the deleted count
|
||||
}
|
||||
|
||||
protected function getTotalItems(): int
|
||||
{
|
||||
return $this->totalItems;
|
||||
}
|
||||
|
||||
protected function setTotalItems(int $total): void
|
||||
{
|
||||
$this->totalItems = $total;
|
||||
}
|
||||
|
||||
protected function getCompletionMessage(): string
|
||||
{
|
||||
$new = $this->getNewCount();
|
||||
$updated = $this->getUpdatedCount();
|
||||
$deleted = $this->getDeletedCount();
|
||||
|
||||
$message = "Sync completed";
|
||||
if ($new > 0 || $updated > 0 || $deleted > 0) {
|
||||
$parts = [];
|
||||
if ($new > 0) $parts[] = "{$new} new";
|
||||
if ($updated > 0) $parts[] = "{$updated} updated";
|
||||
if ($deleted > 0) $parts[] = "{$deleted} deleted";
|
||||
$message .= ": " . implode(", ", $parts);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
protected $currentSyncLogId = null;
|
||||
|
||||
protected function logProgressToFile(string $message): void
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
protected function logProgress(string $message): void
|
||||
{
|
||||
$timestamp = date('H:i:s');
|
||||
@@ -209,9 +284,23 @@ abstract class BaseSyncService
|
||||
|
||||
// Update sync log with progress message if we have a current sync log
|
||||
if ($this->currentSyncLogId) {
|
||||
$this->updateSyncLog($this->currentSyncLogId, 'running', [
|
||||
'message' => $message
|
||||
]);
|
||||
$updateData = [
|
||||
'message' => $message,
|
||||
'processed_items' => $this->getProcessedCount(),
|
||||
'new_items' => $this->getNewCount(),
|
||||
'updated_items' => $this->getUpdatedCount(),
|
||||
'deleted_items' => $this->getDeletedCount()
|
||||
];
|
||||
|
||||
// Only update total_items if it's greater than 0 (to avoid overwriting with 0)
|
||||
if ($this->getTotalItems() > 0) {
|
||||
$updateData['total_items'] = $this->getTotalItems();
|
||||
}
|
||||
|
||||
// Don't update status for completion messages - status should remain as set by completion logic
|
||||
$newStatus = 'running';
|
||||
|
||||
$this->updateSyncLog($this->currentSyncLogId, $newStatus, $updateData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user