diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index fdd885effa..d430d0eb67 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -16,6 +16,7 @@ use Utopia\Database\Exception\Conflict; use Utopia\Database\Exception\Restricted; use Utopia\Database\Exception\Structure; use Utopia\Database\Helpers\ID; +use Utopia\Logger\Log; use Utopia\Migration\Destination; use Utopia\Migration\Destinations\Appwrite as DestinationAppwrite; use Utopia\Migration\Exception as MigrationException; @@ -36,8 +37,6 @@ class Migrations extends Action protected Document $project; - protected $logError; - public static function getName(): string { return 'migrations'; @@ -53,14 +52,14 @@ class Migrations extends Action ->inject('message') ->inject('dbForProject') ->inject('dbForConsole') - ->inject('logError') - ->callback(fn (Message $message, Database $dbForProject, Database $dbForConsole, callable $logError) => $this->action($message, $dbForProject, $dbForConsole, $logError)); + ->inject('log') + ->callback(fn (Message $message, Database $dbForProject, Database $dbForConsole, Log $log) => $this->action($message, $dbForProject, $dbForConsole, $log)); } /** * @throws Exception */ - public function action(Message $message, Database $dbForProject, Database $dbForConsole, callable $logError): void + public function action(Message $message, Database $dbForProject, Database $dbForConsole, Log $log): void { $payload = $message->getPayload() ?? []; @@ -79,7 +78,6 @@ class Migrations extends Action $this->dbForProject = $dbForProject; $this->dbForConsole = $dbForConsole; $this->project = $project; - $this->logError = $logError; /** * Handle Event execution. @@ -88,7 +86,10 @@ class Migrations extends Action return; } - $this->processMigration($migration); + $log->addTag('migrationId', $migration->getId()); + $log->addTag('projectId', $project->getId()); + + $this->processMigration($migration, $log); } /** @@ -258,7 +259,7 @@ class Migrations extends Action * @throws \Utopia\Database\Exception * @throws Exception */ - protected function processMigration(Document $migration): void + protected function processMigration(Document $migration, Log $log): void { $project = $this->project; $projectDocument = $this->dbForConsole->getDocument('projects', $project->getId()); @@ -267,6 +268,8 @@ class Migrations extends Action $transfer = $source = $destination = null; try { + $migration = $this->dbForProject->getDocument('migrations', $migration->getId()); + if ( $migration->getAttribute('source') === SourceAppwrite::getName() && empty($migration->getAttribute('credentials', [])) @@ -284,6 +287,8 @@ class Migrations extends Action $migration->setAttribute('status', 'processing'); $this->updateMigrationDocument($migration, $projectDocument); + $log->addTag('type', $migration->getAttribute('source')); + $source = $this->processSource($migration); $destination = $this->processDestination($migration, $tempAPIKey->getAttribute('secret')); @@ -321,6 +326,7 @@ class Migrations extends Action $errorMessages = []; foreach ($sourceErrors as $error) { + /** @var $sourceErrors $error */ $message = "Error occurred while fetching '{$error->getResourceName()}:{$error->getResourceId()}' from source with message: '{$error->getMessage()}'"; if ($error->getPrevious()) { $message .= " Message: ".$error->getPrevious()->getMessage() . " File: ".$error->getPrevious()->getFile() . " Line: ".$error->getPrevious()->getLine(); @@ -340,6 +346,7 @@ class Migrations extends Action } $migration->setAttribute('errors', $errorMessages); + $log->addExtra('migrationErrors', json_encode($errorMessages)); $this->updateMigrationDocument($migration, $projectDocument); return; @@ -354,6 +361,7 @@ class Migrations extends Action if (! $migration->isEmpty()) { $migration->setAttribute('status', 'failed'); $migration->setAttribute('stage', 'finished'); + $migration->setAttribute('errors', [$th->getMessage()]); return; } @@ -373,6 +381,7 @@ class Migrations extends Action } $migration->setAttribute('errors', $errorMessages); + $log->addTag('migrationErrors', json_encode($errorMessages)); } } finally { if (! $tempAPIKey->isEmpty()) { @@ -387,23 +396,7 @@ class Migrations extends Action $destination->error(); $source->error(); - foreach ($source->getErrors() as $error) { - call_user_func($this->logError, $error, 'appwrite-worker', 'appwrite-queue-' . self::getName(), [ - 'migrationId' => $migration->getId() ?? '', - 'source' => $migration->getAttribute('source') ?? '', - 'resourceName' => $error->getResourceName(), - 'resourceGroup' => $error->getResourceGroup() - ]); - } - - foreach ($destination->getErrors() as $error) { - call_user_func($this->logError, $error, 'appwrite-worker', 'appwrite-queue-' . self::getName(), [ - 'migrationId' => $migration->getId() ?? '', - 'source' => $migration->getAttribute('source') ?? '', - 'resourceName' => $error->getResourceName(), - 'resourceGroup' => $error->getResourceGroup() - ]); - } + throw new Exception('Migration failed'); } if ($migration->getAttribute('status', '') === 'completed') {