diff --git a/src/Appwrite/Compute/Job.php b/src/Appwrite/Compute/Job.php index 1fcc4e5a12..ac8d2ce84e 100644 --- a/src/Appwrite/Compute/Job.php +++ b/src/Appwrite/Compute/Job.php @@ -6,6 +6,7 @@ use Ahc\Jwt\JWT; use Appwrite\Deployment\Token; use OpenRuntimes\Orchestrator\Enum\CallbackEvent; use OpenRuntimes\Orchestrator\Model\Artifact\DownloadArtifact; +use OpenRuntimes\Orchestrator\Model\Artifact\StatArtifact; use OpenRuntimes\Orchestrator\Model\Artifact\UnarchiveArtifact; use OpenRuntimes\Orchestrator\Model\Callback; use OpenRuntimes\Orchestrator\Model\Volume; @@ -73,6 +74,11 @@ final class Job $sourceArtifacts = [ new DownloadArtifact(id: 'source', in: $source['url'], out: 'source.tar.gz'), new UnarchiveArtifact(id: 'extract', in: 'source.tar.gz', out: 'source', subdir: $subdir !== '' ? $subdir : null), + // Appwrite never sees the remote source (the sidecar fetches it), + // so unlike the uploaded-tarball path it can't size it. Stat the + // downloaded archive so the orchestrator reports its byte size in + // an artifact callback, which the worker records as sourceSize. + new StatArtifact(id: 'sourceSize', in: 'source.tar.gz', depends: 'source'), ]; } else { // Presigned source-download URL (GET, no request-body cap), fetched by @@ -128,7 +134,11 @@ final class Job ], 'callback' => new Callback( url: "{$endpoint}/v1/jobs/event?" . \http_build_query(['project' => $projectId]), - events: [CallbackEvent::Log, CallbackEvent::Exit], + // Artifact callbacks only carry the source-size stat, which exists + // only for remote-source builds (templates / VCS). + events: $source !== null + ? [CallbackEvent::Log, CallbackEvent::Artifact, CallbackEvent::Exit] + : [CallbackEvent::Log, CallbackEvent::Exit], key: System::getEnv('_APP_JOBS_SECRET', ''), ), ]; diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 7615935e1f..61a4e6db60 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -113,6 +113,7 @@ class Jobs extends Action $deployment = match ($event->event) { 'orchestrator.job.log' => $this->onLog($dbForProject, $deployment, $event->data), + 'orchestrator.job.artifact' => $this->onArtifact($dbForProject, $deployment, $event->data), 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $deviceForBuilds, $github), default => $deployment, }; @@ -157,6 +158,37 @@ class Jobs extends Action return $dbForProject->updateDocument('deployments', $deployment->getId(), new Document($update)); } + /** + * Record a reported artifact size. Remote-source builds (templates / VCS) + * fetch the source in the sidecar, so Appwrite can't size it the way the + * uploaded-tarball path does — the job stats the downloaded archive and the + * orchestrator reports its size here, which becomes the deployment's + * sourceSize. This arrives before the exit callback, so finalize folds it + * into totalSize. + */ + private function onArtifact(Database $dbForProject, Document $deployment, array $data): Document + { + if (($data['id'] ?? '') !== 'sourceSize') { + return $deployment; + } + + // The stat callback payload shape is still settling upstream; accept the + // size flat or nested under 'stat'. + $stat = \is_array($data['stat'] ?? null) ? $data['stat'] : $data; + $size = (int) ($stat['size'] ?? $stat['bytes'] ?? 0); + if ($size <= 0) { + return $deployment; + } + + // Recompute totalSize too: this callback can land either side of exit + // (they arrive out of order), and finalize likewise derives totalSize + // from the current sourceSize — so whichever runs last, the two agree. + return $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ + 'sourceSize' => $size, + 'totalSize' => $size + (int) $deployment->getAttribute('buildSize', 0), + ])); + } + /** * Resolve the build-command exit into a terminal outcome. The output is * already on the mounted builds volume, so a zero exit enforces the size