From 36155d86d1675886c6c44e32da67240e54260d93 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 16 Jul 2026 11:40:50 +0100 Subject: [PATCH] Declare the build artifact path from its compression instead of discovering it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The artifact's filename is a pure function of the compression the job is told to use (none → code.tar, squashfs → code.sqfs, default gzip → code.tar.gz), so the payload now passes OPEN_RUNTIMES_BUILD_COMPRESSION (_APP_COMPUTE_BUILD_COMPRESSION, default gzip) and buildPath is declared at submission again — outputDirectory() plus the deterministic name. Post-build discovery via Device::getFiles() is dropped: its return shape differs per device (Local returns a path list, the S3 family returns the raw ListObjectsV2 response), so on S3-backed builds storage every completed build would have counted response metadata and finalized as "must produce exactly one output artifact". A declared path keeps the worker to a plain exists/size check that behaves identically on every device, and the strategy override (e.g. Cloud's S3 artifacts) can reuse artifact() to name its upload. Co-Authored-By: Claude Fable 5 --- .../Deployment/Backend/Orchestrator.php | 27 ++++++++ .../Modules/Functions/Workers/Jobs.php | 23 ++----- .../Modules/Functions/Workers/JobsTest.php | 65 ------------------- 3 files changed, 31 insertions(+), 84 deletions(-) delete mode 100644 tests/unit/Platform/Modules/Functions/Workers/JobsTest.php diff --git a/src/Appwrite/Deployment/Backend/Orchestrator.php b/src/Appwrite/Deployment/Backend/Orchestrator.php index e62ac5a2ac..a003b036a0 100644 --- a/src/Appwrite/Deployment/Backend/Orchestrator.php +++ b/src/Appwrite/Deployment/Backend/Orchestrator.php @@ -84,6 +84,7 @@ readonly class Orchestrator extends Backend $deployment = $this->dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'status' => 'waiting', + 'buildPath' => static::buildPath($this->project->getId(), $deployment->getId()), ])); $this->jobs->create(...static::payload($this->project, $resource, $deployment, $this->platform, $source)); @@ -165,6 +166,7 @@ readonly class Orchestrator extends Backend $command = $deployment->getAttribute('buildCommands', ''); $env = self::variables($project, $function, $deployment, $runtime, $cpus, $memory, $endpoint, $timeout) + [ 'OPEN_RUNTIMES_BUILD_INPUT_DIR' => '/mnt/code/source', + 'OPEN_RUNTIMES_BUILD_COMPRESSION' => static::compression(), ] + $output['environment']; return [ @@ -218,6 +220,31 @@ readonly class Orchestrator extends Backend return APP_STORAGE_BUILDS . "/app-{$projectId}/{$deploymentId}"; } + /** + * The build output path on the builds volume, declared at submission. + */ + public static function buildPath(string $projectId, string $deploymentId): string + { + return static::outputDirectory($projectId, $deploymentId) . '/' . static::artifact(); + } + + /** + * The artifact filename build.sh produces for the configured compression. + */ + public static function artifact(): string + { + return match (static::compression()) { + 'none' => 'code.tar', + 'squashfs' => 'code.sqfs', + default => 'code.tar.gz', + }; + } + + protected static function compression(): string + { + return System::getEnv('_APP_COMPUTE_BUILD_COMPRESSION', 'gzip'); + } + /** * Deterministic build-cache key, shared across a function's deployments so * package-manager caches (npm/yarn/pnpm) survive between builds. diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 2f90e11f2e..a70960588a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\Functions\Workers; -use Appwrite\Deployment\Backend\Orchestrator; use Appwrite\Event\Event; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Message\Jobs as JobsMessage; @@ -266,9 +265,9 @@ class Jobs extends Action Device $deviceForBuilds, VcsFactory $vcsFactory, ): Document { - $path = $this->findBuildPath($deviceForBuilds, $project->getId(), $deployment); - if ($path === '') { - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build must produce exactly one output artifact.', $usage, $publisherForUsage, $vcsFactory); + $path = (string) $deployment->getAttribute('buildPath', ''); + if ($path === '' || ! $deviceForBuilds->exists($path)) { + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build produced no output artifact.', $usage, $publisherForUsage, $vcsFactory); } $size = $deviceForBuilds->getFileSize($path); @@ -280,19 +279,7 @@ class Jobs extends Action return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $vcsFactory); } - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsFactory, $size, $path); - } - - private function findBuildPath(Device $deviceForBuilds, string $projectId, Document $deployment): string - { - $path = (string) $deployment->getAttribute('buildPath', ''); - if ($path !== '' && $deviceForBuilds->exists($path)) { - return $path; - } - - $files = $deviceForBuilds->getFiles(Orchestrator::outputDirectory($projectId, $deployment->getId())); - - return \count($files) === 1 ? (string) $files[0] : ''; + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsFactory, $size); } /** @@ -311,7 +298,6 @@ class Jobs extends Action UsagePublisher $publisherForUsage, VcsFactory $vcsFactory, int $buildSize = 0, - string $buildPath = '', ): Document { $function = $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId')); @@ -327,7 +313,6 @@ class Jobs extends Action 'buildLogs' => $this->truncate($logs . $trailer), ]; if ($success) { - $update['buildPath'] = $buildPath; $update['buildSize'] = $buildSize; $update['totalSize'] = $buildSize + (int) $deployment->getAttribute('sourceSize', 0); } diff --git a/tests/unit/Platform/Modules/Functions/Workers/JobsTest.php b/tests/unit/Platform/Modules/Functions/Workers/JobsTest.php deleted file mode 100644 index db6fa07d16..0000000000 --- a/tests/unit/Platform/Modules/Functions/Workers/JobsTest.php +++ /dev/null @@ -1,65 +0,0 @@ -createMock(Device::class); - $device->expects($this->once())->method('exists')->with($path)->willReturn(true); - $device->expects($this->never())->method('getFiles'); - - $this->assertSame($path, $this->findBuildPath($device, new Document([ - '$id' => 'deployment', - 'buildPath' => $path, - ]))); - } - - public function testFindBuildPathDiscoversOpaqueArtifactPath(): void - { - $path = '/storage/builds/app-project/deployment/code.sqfs'; - $device = $this->createMock(Device::class); - $device->expects($this->never())->method('exists'); - $device - ->expects($this->once()) - ->method('getFiles') - ->with('/storage/builds/app-project/deployment') - ->willReturn([$path]); - - $this->assertSame($path, $this->findBuildPath($device, new Document([ - '$id' => 'deployment', - 'buildPath' => '', - ]))); - } - - public function testFindBuildPathRejectsAmbiguousOutput(): void - { - $device = $this->createMock(Device::class); - $device - ->expects($this->once()) - ->method('getFiles') - ->willReturn(['/build/code.sqfs', '/build/code.next']); - - $this->assertSame('', $this->findBuildPath($device, new Document([ - '$id' => 'deployment', - 'buildPath' => '', - ]))); - } - - private function findBuildPath(Device $device, Document $deployment): string - { - $method = new ReflectionMethod(Jobs::class, 'findBuildPath'); - - return $method->invoke(new Jobs(), $device, 'project', $deployment); - } -}