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 <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Fable 5 committed 2026-07-10 20:17:31 +01:00
1 parent 94f277ef75
commit 29cdc9fe2d
2 files changed
+17 -17

No files matched your search

+9
View File
@@ -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(), []);
@@ -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'];