Capture themes sequentially; rely on worker coroutines for concurrency

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 <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Fable 5 committed 2026-07-10 20:35:09 +01:00
1 parent d99a780494
commit 5e35b4bac1
1 file changed
+10 -28
@@ -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);