From f830918c7cce2be697f8e8b1f0c9b04a555583a3 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 25 Feb 2025 16:19:30 +0530 Subject: [PATCH] Add logic to detect rendering strategy and fallback file --- docker-compose.yml | 2 +- .../Modules/Functions/Workers/Builds.php | 27 ++++++++++++++++++- src/Executor/Executor.php | 25 +++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 6dec6e3918..f03e4097b0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -952,7 +952,7 @@ services: hostname: exc1 <<: *x-logging stop_signal: SIGINT - image: openruntimes/executor:0.7.4 + image: openruntimes-executor-2 restart: unless-stopped networks: - appwrite diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 0a4907fdb7..e55375e12f 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -24,6 +24,9 @@ use Utopia\Database\Exception\Structure; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; +use Utopia\Detector\Detection\Rendering\SSG; +use Utopia\Detector\Detection\Rendering\SSR; +use Utopia\Detector\Detector\Rendering; use Utopia\Logger\Log; use Utopia\Platform\Action; use Utopia\Queue\Message; @@ -590,7 +593,7 @@ class Builds extends Action cpus: $cpus, memory: $memory, timeout: $timeout, - remove: true, + remove: false, entrypoint: $deployment->getAttribute('entrypoint', 'package.json'), // TODO: change this later so that sites don't need to have an entrypoint destination: APP_STORAGE_BUILDS . "/app-{$project->getId()}", variables: $vars, @@ -691,6 +694,28 @@ class Builds extends Action throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / 1048576, 2) . ' MBs.'); } + $listFilesCommand = "cd /usr/local/build/" . $resource->getAttribute('outputDirectory') . " && find . -name 'node_modules' -prune -o -type f -print"; + $response = $executor->executeCommand( + deploymentId: $deployment->getId(), + projectId: $project->getId(), + command: $listFilesCommand, + timeout: 60 + ); + + $files = array_map('trim', array_filter(explode("\n", $response['output']))); + + $detector = new Rendering($files, $resource->getAttribute('framework')); + $detector + ->addOption(new SSR()) + ->addOption(new SSG()); + $detectedRenderingStrategy = $detector->detect(); + + $renderingStrategy = $detectedRenderingStrategy->getName(); + $fallbackFile = $detectedRenderingStrategy->getFallbackFile(); + + $resource->setAttribute('adapter', $renderingStrategy); + $resource->setAttribute('fallbackFile', $fallbackFile); + /** Update the build document */ $build->setAttribute('startTime', DateTime::format((new \DateTime())->setTimestamp(floor($response['startTime'])))); $build->setAttribute('endTime', $endTime); diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 0c6b5a14ff..a105503088 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -249,6 +249,31 @@ class Executor return $response['body']; } + public function executeCommand( + string $deploymentId, + string $projectId, + string $command, + int $timeout + ) { + $runtimeId = "$projectId-$deploymentId-build"; + $route = "/runtimes/$runtimeId/commands"; + + $params = [ + 'command' => $command, + 'timeout' => $timeout + ]; + + $response = $this->call(self::METHOD_POST, $route, [ 'x-opr-runtime-id' => $runtimeId ], $params, true, $timeout); + + $status = $response['headers']['status-code']; + if ($status >= 400) { + $message = \is_string($response['body']) ? $response['body'] : $response['body']['message']; + throw new \Exception($message, $status); + } + + return $response['body']; + } + /** * Call *