From 109d12b181a467575a20e4d9f511a64d59a4a0f7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:07:26 +0100 Subject: [PATCH 1/8] Migrate screenshots worker to utopia-php/client Replaces utopia-php/fetch with the PSR-18 utopia-php/client already used by the jobs client. The client and request factory are built once per message instead of per screenshot, and the timeout is now passed in seconds instead of milliseconds to match the new client's API. Co-Authored-By: Claude Fable 5 --- .../Modules/Functions/Workers/Screenshots.php | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index 35cbe63a15..aeb3259645 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -8,14 +8,17 @@ use Appwrite\Event\Realtime; use Appwrite\Permission; use Appwrite\Role; use Exception; +use Utopia\Client; +use Utopia\Client\Adapter\Curl\Client as CurlAdapter; use Utopia\Compression\Compression; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; -use Utopia\Fetch\Client as FetchClient; use Utopia\Platform\Action; +use Utopia\Psr7\Method; +use Utopia\Psr7\Request\Factory as RequestFactory; use Utopia\Queue\Message; use Utopia\Span\Span; use Utopia\Storage\Device; @@ -111,7 +114,7 @@ class Screenshots extends Action throw new \Exception("Rule for deployment not found"); } - $timeout = \intval($site->getAttribute('timeout', '15')) * 1000; + $timeout = \intval($site->getAttribute('timeout', '15')); $bucket = $dbForPlatform->getDocument('buckets', 'screenshots'); @@ -161,9 +164,13 @@ class Screenshots extends Action 'deploymentStatusIgnored' => true ]); + $browserEndpoint = System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1'); + $client = (new Client(new CurlAdapter()))->withTimeout($timeout); + $factory = new RequestFactory(); + $screenshotError = null; - $screenshots = batch(\array_map(function ($key) use ($configs, $apiKey, $site, $timeout, &$screenshotError) { - return function () use ($key, $configs, $apiKey, $site, $timeout, &$screenshotError) { + $screenshots = batch(\array_map(function ($key) use ($configs, $apiKey, $site, $browserEndpoint, $client, $factory, &$screenshotError) { + return function () use ($key, $configs, $apiKey, $site, $browserEndpoint, $client, $factory, &$screenshotError) { try { $config = $configs[$key]; @@ -178,22 +185,13 @@ class Screenshots extends Action $config['sleep'] = $framework['screenshotSleep']; } - $browserEndpoint = System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1'); - $client = new FetchClient(); - $client->setTimeout($timeout); - $client->addHeader('content-type', FetchClient::CONTENT_TYPE_APPLICATION_JSON); + $response = $client->sendRequest($factory->json(Method::POST, $browserEndpoint . '/screenshots', $config)); - $fetchResponse = $client->fetch( - url: $browserEndpoint . '/screenshots', - method: 'POST', - body: $config - ); - - if ($fetchResponse->getStatusCode() >= 400) { - throw new \Exception($fetchResponse->getBody()); + if ($response->getStatusCode() >= 400) { + throw new \Exception((string)$response->getBody()); } - $screenshot = $fetchResponse->getBody(); + $screenshot = (string)$response->getBody(); return ['key' => $key, 'screenshot' => $screenshot]; } catch (\Throwable $th) { From 94f277ef75e4bba7aaac06fb30bc14afbe7e41fe Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:13:04 +0100 Subject: [PATCH 2/8] Extract browser screenshot HTTP logic into Screenshots\Client The client takes a PSR-18 ClientInterface configured with the browser service base URI, so the worker only decides what to capture (theme, headers, sleep) and the transport is swappable in tests. Co-Authored-By: Claude Fable 5 --- .../Modules/Functions/Workers/Screenshots.php | 66 ++++++++----------- src/Appwrite/Screenshots/Client.php | 44 +++++++++++++ 2 files changed, 70 insertions(+), 40 deletions(-) create mode 100644 src/Appwrite/Screenshots/Client.php diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index aeb3259645..6f09db1bec 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -7,6 +7,7 @@ use Appwrite\Event\Message\Screenshot; use Appwrite\Event\Realtime; use Appwrite\Permission; use Appwrite\Role; +use Appwrite\Screenshots\Client as ScreenshotsClient; use Exception; use Utopia\Client; use Utopia\Client\Adapter\Curl\Client as CurlAdapter; @@ -17,8 +18,6 @@ use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; use Utopia\Platform\Action; -use Utopia\Psr7\Method; -use Utopia\Psr7\Request\Factory as RequestFactory; use Utopia\Queue\Message; use Utopia\Span\Span; use Utopia\Storage\Device; @@ -128,19 +127,6 @@ class Screenshots extends Action $routerHost = "$protocol://{$rule->getAttribute('domain')}"; } - $configs = [ - 'screenshotLight' => [ - 'headers' => [ 'x-appwrite-hostname' => $rule->getAttribute('domain') ], - 'url' => $routerHost . '/?appwrite-preview=1&appwrite-theme=light', - 'theme' => 'light' - ], - 'screenshotDark' => [ - 'headers' => [ 'x-appwrite-hostname' => $rule->getAttribute('domain') ], - 'url' => $routerHost . '/?appwrite-preview=1&appwrite-theme=dark', - 'theme' => 'dark' - ], - ]; - $jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 0); $apiKey = $jwtObj->encode([ 'hostnameOverride' => true, @@ -165,33 +151,33 @@ class Screenshots extends Action ]); $browserEndpoint = System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1'); - $client = (new Client(new CurlAdapter()))->withTimeout($timeout); - $factory = new RequestFactory(); + $client = new ScreenshotsClient( + (new Client(new CurlAdapter())) + ->withBaseUri($browserEndpoint) + ->withTimeout($timeout) + ); + + $headers = [ + 'x-appwrite-hostname' => $rule->getAttribute('domain'), + 'x-appwrite-key' => API_KEY_EPHEMERAL . '_' . $apiKey, + ]; + + $framework = Config::getParam('frameworks', [])[$site->getAttribute('framework', '')] ?? null; + $sleep = $framework['screenshotSleep'] ?? 3000; + + $themes = ['screenshotLight' => 'light', 'screenshotDark' => 'dark']; $screenshotError = null; - $screenshots = batch(\array_map(function ($key) use ($configs, $apiKey, $site, $browserEndpoint, $client, $factory, &$screenshotError) { - return function () use ($key, $configs, $apiKey, $site, $browserEndpoint, $client, $factory, &$screenshotError) { + $screenshots = batch(\array_map(function ($key) use ($themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { + return function () use ($key, $themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { try { - $config = $configs[$key]; - - $config['headers'] = \array_merge($config['headers'], [ - 'x-appwrite-key' => API_KEY_EPHEMERAL . '_' . $apiKey - ]); - $config['sleep'] = 3000; - - $frameworks = Config::getParam('frameworks', []); - $framework = $frameworks[$site->getAttribute('framework', '')] ?? null; - if (!is_null($framework)) { - $config['sleep'] = $framework['screenshotSleep']; - } - - $response = $client->sendRequest($factory->json(Method::POST, $browserEndpoint . '/screenshots', $config)); - - if ($response->getStatusCode() >= 400) { - throw new \Exception((string)$response->getBody()); - } - - $screenshot = (string)$response->getBody(); + $theme = $themes[$key]; + $screenshot = $client->create( + url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme, + theme: $theme, + headers: $headers, + sleep: $sleep, + ); return ['key' => $key, 'screenshot' => $screenshot]; } catch (\Throwable $th) { @@ -199,7 +185,7 @@ class Screenshots extends Action return; } }; - }, \array_keys($configs))); + }, \array_keys($themes))); if (!\is_null($screenshotError)) { throw new \Exception($screenshotError); diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php new file mode 100644 index 0000000000..ad5b78a8f6 --- /dev/null +++ b/src/Appwrite/Screenshots/Client.php @@ -0,0 +1,44 @@ +factory = new Factory(); + } + + /** + * Capture a screenshot of a page and return the PNG bytes. + * + * @param array $headers + * @throws \Exception on an error response + * @throws ClientExceptionInterface on transport failure + */ + public function create(string $url, string $theme, array $headers = [], int $sleep = 3000): string + { + $response = $this->client->sendRequest($this->factory->json(Method::POST, 'screenshots', [ + 'url' => $url, + 'theme' => $theme, + 'headers' => $headers, + 'sleep' => $sleep, + ])); + + if ($response->getStatusCode() >= 400) { + throw new \Exception((string)$response->getBody()); + } + + return (string)$response->getBody(); + } +} From 29cdc9fe2d0ca333fa50f28967ae1d97548d6631 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:17:31 +0100 Subject: [PATCH 3/8] Provide the screenshots client as an injected resource The client is registered once in app/init/resources.php with the browser base URI and a timeout of _APP_SITES_TIMEOUT (the upper bound of any site's timeout), instead of being constructed per message with the per-site timeout. Co-Authored-By: Claude Fable 5 --- app/init/resources.php | 9 +++++++ .../Modules/Functions/Workers/Screenshots.php | 25 ++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index d559213f5e..c41aa4489f 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -18,6 +18,7 @@ use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; +use Appwrite\Screenshots\Client as ScreenshotsClient; use Executor\Executor; use OpenRuntimes\Orchestrator\Jobs; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; @@ -86,6 +87,14 @@ $container->set('jobs', function () { return new Jobs($client); }, []); +$container->set('screenshots', function () { + $client = (new Client(new CurlAdapter())) + ->withBaseUri(System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1')) + ->withTimeout((int) System::getEnv('_APP_SITES_TIMEOUT', 30)); + + return new ScreenshotsClient($client); +}, []); + $container->set('telemetry', fn () => new NoTelemetry(), []); $container->set('authorization', fn () => new Authorization(), []); diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index 6f09db1bec..4898ff7c88 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -9,8 +9,6 @@ use Appwrite\Permission; use Appwrite\Role; use Appwrite\Screenshots\Client as ScreenshotsClient; use Exception; -use Utopia\Client; -use Utopia\Client\Adapter\Curl\Client as CurlAdapter; use Utopia\Compression\Compression; use Utopia\Config\Config; use Utopia\Database\Database; @@ -49,6 +47,7 @@ class Screenshots extends Action ->inject('project') ->inject('deviceForFiles') ->inject('telemetry') + ->inject('screenshots') ->callback($this->action(...)); } @@ -59,7 +58,8 @@ class Screenshots extends Action Database $dbForProject, Document $project, Device $deviceForFiles, - Telemetry $telemetry + Telemetry $telemetry, + ScreenshotsClient $screenshots ): void { Span::add('project.id', $project->getId()); @@ -113,8 +113,6 @@ class Screenshots extends Action throw new \Exception("Rule for deployment not found"); } - $timeout = \intval($site->getAttribute('timeout', '15')); - $bucket = $dbForPlatform->getDocument('buckets', 'screenshots'); if ($bucket->isEmpty()) { @@ -150,13 +148,6 @@ class Screenshots extends Action 'deploymentStatusIgnored' => true ]); - $browserEndpoint = System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1'); - $client = new ScreenshotsClient( - (new Client(new CurlAdapter())) - ->withBaseUri($browserEndpoint) - ->withTimeout($timeout) - ); - $headers = [ 'x-appwrite-hostname' => $rule->getAttribute('domain'), 'x-appwrite-key' => API_KEY_EPHEMERAL . '_' . $apiKey, @@ -168,11 +159,11 @@ class Screenshots extends Action $themes = ['screenshotLight' => 'light', 'screenshotDark' => 'dark']; $screenshotError = null; - $screenshots = batch(\array_map(function ($key) use ($themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { - return function () use ($key, $themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { + $captures = batch(\array_map(function ($key) use ($themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) { + return function () use ($key, $themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) { try { $theme = $themes[$key]; - $screenshot = $client->create( + $screenshot = $screenshots->create( url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme, theme: $theme, headers: $headers, @@ -191,12 +182,12 @@ class Screenshots extends Action throw new \Exception($screenshotError); } - Span::add('screenshot.count', \count($screenshots)); + Span::add('screenshot.count', \count($captures)); $mimeType = "image/png"; $updates = new Document([]); - foreach ($screenshots as $data) { + foreach ($captures as $data) { $key = $data['key']; $screenshot = $data['screenshot']; From a9a7a1eba38dba0b0c3fd32aadb1b1fe1ebdeaf3 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:21:16 +0100 Subject: [PATCH 4/8] Import screenshots client without alias in the worker Co-Authored-By: Claude Fable 5 --- .../Platform/Modules/Functions/Workers/Screenshots.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index 4898ff7c88..fff1e1e77d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -7,7 +7,7 @@ use Appwrite\Event\Message\Screenshot; use Appwrite\Event\Realtime; use Appwrite\Permission; use Appwrite\Role; -use Appwrite\Screenshots\Client as ScreenshotsClient; +use Appwrite\Screenshots\Client; use Exception; use Utopia\Compression\Compression; use Utopia\Config\Config; @@ -59,7 +59,7 @@ class Screenshots extends Action Document $project, Device $deviceForFiles, Telemetry $telemetry, - ScreenshotsClient $screenshots + Client $screenshots ): void { Span::add('project.id', $project->getId()); From b10a2bdcb461882d55545c8c459d2a72d7937457 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:25:03 +0100 Subject: [PATCH 5/8] Handle browser service error responses in the screenshots client The browser service returns raw image bytes on success and a 4xx JSON {"error": message} on failure (per appwrite/docker-browser 0.3.2). The client now surfaces that message through a typed Screenshots\Exception carrying the status code, and rejects non-image success responses instead of saving them as PNG files. Covered by unit tests with a stubbed PSR-18 client. Co-Authored-By: Claude Fable 5 --- src/Appwrite/Screenshots/Client.php | 20 ++++-- src/Appwrite/Screenshots/Exception.php | 7 ++ tests/unit/Screenshots/ClientTest.php | 94 ++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 src/Appwrite/Screenshots/Exception.php create mode 100644 tests/unit/Screenshots/ClientTest.php diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php index ad5b78a8f6..74fe9fb98c 100644 --- a/src/Appwrite/Screenshots/Client.php +++ b/src/Appwrite/Screenshots/Client.php @@ -22,8 +22,11 @@ class Client /** * Capture a screenshot of a page and return the PNG bytes. * + * The browser service responds with raw image bytes on success and + * `{"error": message}` with a 4xx status on failure. + * * @param array $headers - * @throws \Exception on an error response + * @throws Exception on an error response * @throws ClientExceptionInterface on transport failure */ public function create(string $url, string $theme, array $headers = [], int $sleep = 3000): string @@ -35,10 +38,19 @@ class Client 'sleep' => $sleep, ])); - if ($response->getStatusCode() >= 400) { - throw new \Exception((string)$response->getBody()); + $status = $response->getStatusCode(); + $body = (string)$response->getBody(); + + if ($status >= 400) { + $error = \json_decode($body, true)['error'] ?? null; + throw new Exception(\is_string($error) ? $error : 'Screenshot failed with status ' . $status, $status); } - return (string)$response->getBody(); + $contentType = $response->getHeaderLine('Content-Type'); + if (!\str_starts_with($contentType, 'image/')) { + throw new Exception('Expected an image response, got ' . ($contentType === '' ? 'no content type' : $contentType), $status); + } + + return $body; } } diff --git a/src/Appwrite/Screenshots/Exception.php b/src/Appwrite/Screenshots/Exception.php new file mode 100644 index 0000000000..4f6b17a39b --- /dev/null +++ b/src/Appwrite/Screenshots/Exception.php @@ -0,0 +1,7 @@ +test->setRequest($request); + return $this->response; + } + }); + } + + public function setRequest(RequestInterface $request): void + { + $this->request = $request; + } + + public function testCreate(): void + { + $response = (new Response(200, body: new Stream('png-bytes'))) + ->withHeader('Content-Type', 'image/png'); + + $screenshot = $this->client($response)->create( + url: 'http://appwrite/', + theme: 'dark', + headers: ['x-appwrite-hostname' => 'example.com'], + sleep: 500, + ); + + $this->assertSame('png-bytes', $screenshot); + $this->assertSame('POST', $this->request->getMethod()); + $this->assertSame('screenshots', (string)$this->request->getUri()); + $this->assertEquals([ + 'url' => 'http://appwrite/', + 'theme' => 'dark', + 'headers' => ['x-appwrite-hostname' => 'example.com'], + 'sleep' => 500, + ], \json_decode((string)$this->request->getBody(), true)); + } + + public function testCreateError(): void + { + $response = (new Response(400, body: new Stream('{"error":"Timeout exceeded"}'))) + ->withHeader('Content-Type', 'application/json'); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Timeout exceeded'); + $this->expectExceptionCode(400); + + $this->client($response)->create('http://appwrite/', 'light'); + } + + public function testCreateErrorWithoutBody(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Screenshot failed with status 502'); + + $this->client(new Response(502))->create('http://appwrite/', 'light'); + } + + public function testCreateUnexpectedContentType(): void + { + $response = (new Response(200, body: new Stream(''))) + ->withHeader('Content-Type', 'text/html'); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Expected an image response, got text/html'); + + $this->client($response)->create('http://appwrite/', 'light'); + } +} From d99a780494bcafb46d6d3adaafa9f94024888ceb Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:32:27 +0100 Subject: [PATCH 6/8] Flatten the screenshot capture batch Build the task list with a foreach over the theme map instead of a closure-returning closure that captured the same variables twice, and return failures from the coroutines instead of threading a by-reference error string that left null slots in the results. Co-Authored-By: Claude Fable 5 --- .../Modules/Functions/Workers/Screenshots.php | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index fff1e1e77d..27de5414ce 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -156,30 +156,29 @@ class Screenshots extends Action $framework = Config::getParam('frameworks', [])[$site->getAttribute('framework', '')] ?? null; $sleep = $framework['screenshotSleep'] ?? 3000; - $themes = ['screenshotLight' => 'light', 'screenshotDark' => 'dark']; - - $screenshotError = null; - $captures = batch(\array_map(function ($key) use ($themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) { - return function () use ($key, $themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) { + $tasks = []; + foreach (['screenshotLight' => 'light', 'screenshotDark' => 'dark'] as $key => $theme) { + // batch() doesn't propagate exceptions out of coroutines; return them instead. + $tasks[] = function () use ($key, $theme, $screenshots, $routerHost, $headers, $sleep) { try { - $theme = $themes[$key]; - $screenshot = $screenshots->create( + return ['key' => $key, 'screenshot' => $screenshots->create( url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme, theme: $theme, headers: $headers, sleep: $sleep, - ); - - return ['key' => $key, 'screenshot' => $screenshot]; + )]; } catch (\Throwable $th) { - $screenshotError = $th->getMessage(); - return; + return $th; } }; - }, \array_keys($themes))); + } - if (!\is_null($screenshotError)) { - throw new \Exception($screenshotError); + $captures = []; + foreach (batch($tasks) as $capture) { + if ($capture instanceof \Throwable) { + throw $capture; + } + $captures[] = $capture; } Span::add('screenshot.count', \count($captures)); From 5e35b4bac17ce9226e7572f6ab30b6d73f64a244 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:35:09 +0100 Subject: [PATCH 7/8] Capture themes sequentially; rely on worker coroutines for concurrency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The screenshots worker already runs with _APP_WORKERS_NUM=1 and _APP_WORKER_MAX_COROUTINES=8, so per-message batch() fan-out is redundant — concurrency across messages comes from the queue adapter. Capturing light/dark sequentially deletes the task closures and lets failures propagate as plain exceptions. Co-Authored-By: Claude Fable 5 --- .../Modules/Functions/Workers/Screenshots.php | 38 +++++-------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index 27de5414ce..3fce845324 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -23,8 +23,6 @@ use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Telemetry\Counter; -use function Swoole\Coroutine\batch; - class Screenshots extends Action { public static function getName(): string @@ -156,29 +154,16 @@ class Screenshots extends Action $framework = Config::getParam('frameworks', [])[$site->getAttribute('framework', '')] ?? null; $sleep = $framework['screenshotSleep'] ?? 3000; - $tasks = []; - foreach (['screenshotLight' => 'light', 'screenshotDark' => 'dark'] as $key => $theme) { - // batch() doesn't propagate exceptions out of coroutines; return them instead. - $tasks[] = function () use ($key, $theme, $screenshots, $routerHost, $headers, $sleep) { - try { - return ['key' => $key, 'screenshot' => $screenshots->create( - url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme, - theme: $theme, - headers: $headers, - sleep: $sleep, - )]; - } catch (\Throwable $th) { - return $th; - } - }; - } - + // Concurrency comes from the worker's coroutines (_APP_WORKER_MAX_COROUTINES), + // so the two themes are captured sequentially within a message. $captures = []; - foreach (batch($tasks) as $capture) { - if ($capture instanceof \Throwable) { - throw $capture; - } - $captures[] = $capture; + foreach (['screenshotLight' => 'light', 'screenshotDark' => 'dark'] as $key => $theme) { + $captures[$key] = $screenshots->create( + url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme, + theme: $theme, + headers: $headers, + sleep: $sleep, + ); } Span::add('screenshot.count', \count($captures)); @@ -186,10 +171,7 @@ class Screenshots extends Action $mimeType = "image/png"; $updates = new Document([]); - foreach ($captures as $data) { - $key = $data['key']; - $screenshot = $data['screenshot']; - + foreach ($captures as $key => $screenshot) { $fileId = ID::unique(); $fileName = $fileId . '.png'; $path = $deviceForFiles->getPath($fileName); From aa26b98617ab6258456ecfd917fae879b5e32e41 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:57:49 +0100 Subject: [PATCH 8/8] Drop narration comments Co-Authored-By: Claude Fable 5 --- .../Platform/Modules/Functions/Workers/Screenshots.php | 2 -- src/Appwrite/Screenshots/Client.php | 3 --- 2 files changed, 5 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index 3fce845324..b61ab8fa9d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -154,8 +154,6 @@ class Screenshots extends Action $framework = Config::getParam('frameworks', [])[$site->getAttribute('framework', '')] ?? null; $sleep = $framework['screenshotSleep'] ?? 3000; - // Concurrency comes from the worker's coroutines (_APP_WORKER_MAX_COROUTINES), - // so the two themes are captured sequentially within a message. $captures = []; foreach (['screenshotLight' => 'light', 'screenshotDark' => 'dark'] as $key => $theme) { $captures[$key] = $screenshots->create( diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php index 74fe9fb98c..096d737393 100644 --- a/src/Appwrite/Screenshots/Client.php +++ b/src/Appwrite/Screenshots/Client.php @@ -22,9 +22,6 @@ class Client /** * Capture a screenshot of a page and return the PNG bytes. * - * The browser service responds with raw image bytes on success and - * `{"error": message}` with a 4xx status on failure. - * * @param array $headers * @throws Exception on an error response * @throws ClientExceptionInterface on transport failure