mirror of
https://github.com/appwrite/appwrite
synced 2026-05-24 09:28:40 +00:00
Concurrency screenshots
This commit is contained in:
parent
681b6031aa
commit
2c55f5aaa6
1 changed files with 63 additions and 54 deletions
|
|
@ -41,6 +41,8 @@ use Utopia\Storage\Device\Local;
|
||||||
use Utopia\System\System;
|
use Utopia\System\System;
|
||||||
use Utopia\VCS\Adapter\Git\GitHub;
|
use Utopia\VCS\Adapter\Git\GitHub;
|
||||||
|
|
||||||
|
use function Swoole\Coroutine\batch;
|
||||||
|
|
||||||
class Builds extends Action
|
class Builds extends Action
|
||||||
{
|
{
|
||||||
public static function getName(): string
|
public static function getName(): string
|
||||||
|
|
@ -849,69 +851,76 @@ class Builds extends Action
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// TODO: @Meldiron if becomes too slow, do concurrently
|
// TODO: @Meldiron if becomes too slow, do concurrently
|
||||||
foreach ($configs as $key => $config) {
|
$screenshots = batch(\array_map(function ($key) use ($configs, $deviceForFiles, $apiKey, $resource, $client, $bucket, $project, $dbForPlatform) {
|
||||||
$config['headers'] = \array_merge($config['headers'] ?? [], [
|
return function () use ($key, $configs, $deviceForFiles, $apiKey, $resource, $client, $bucket, $project, $dbForPlatform) {
|
||||||
'x-appwrite-key' => API_KEY_DYNAMIC . '_' . $apiKey
|
$config = $configs[$key];
|
||||||
]);
|
|
||||||
|
|
||||||
$config['sleep'] = 3000;
|
$config['headers'] = \array_merge($config['headers'] ?? [], [
|
||||||
|
'x-appwrite-key' => API_KEY_DYNAMIC . '_' . $apiKey
|
||||||
|
]);
|
||||||
|
$config['sleep'] = 3000;
|
||||||
|
|
||||||
$frameworks = Config::getParam('frameworks', []);
|
$frameworks = Config::getParam('frameworks', []);
|
||||||
$framework = $frameworks[$resource->getAttribute('framework', '')] ?? null;
|
$framework = $frameworks[$resource->getAttribute('framework', '')] ?? null;
|
||||||
if (!is_null($framework)) {
|
if (!is_null($framework)) {
|
||||||
$config['sleep'] = $framework['screenshotSleep'];
|
$config['sleep'] = $framework['screenshotSleep'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$fetchResponse = $client->fetch(
|
$fetchResponse = $client->fetch(
|
||||||
url: 'http://appwrite-browser:3000/v1/screenshots',
|
url: 'http://appwrite-browser:3000/v1/screenshots',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: $config
|
body: $config
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($fetchResponse->getStatusCode() >= 400) {
|
if ($fetchResponse->getStatusCode() >= 400) {
|
||||||
throw new \Exception($fetchResponse->getBody());
|
throw new \Exception($fetchResponse->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
$screenshot = $fetchResponse->getBody();
|
$screenshot = $fetchResponse->getBody();
|
||||||
|
|
||||||
$fileId = ID::unique();
|
$fileId = ID::unique();
|
||||||
$fileName = $fileId . '.png';
|
$fileName = $fileId . '.png';
|
||||||
$path = $deviceForFiles->getPath($fileName);
|
$path = $deviceForFiles->getPath($fileName);
|
||||||
$path = str_ireplace($deviceForFiles->getRoot(), $deviceForFiles->getRoot() . DIRECTORY_SEPARATOR . $bucket->getId(), $path); // Add bucket id to path after root
|
$path = str_ireplace($deviceForFiles->getRoot(), $deviceForFiles->getRoot() . DIRECTORY_SEPARATOR . $bucket->getId(), $path); // Add bucket id to path after root
|
||||||
$success = $deviceForFiles->write($path, $screenshot, "image/png");
|
$success = $deviceForFiles->write($path, $screenshot, "image/png");
|
||||||
|
|
||||||
if (!$success) {
|
if (!$success) {
|
||||||
throw new \Exception("Screenshot failed to save");
|
throw new \Exception("Screenshot failed to save");
|
||||||
}
|
}
|
||||||
|
|
||||||
$teamId = $project->getAttribute('teamId', '');
|
$teamId = $project->getAttribute('teamId', '');
|
||||||
$file = new Document([
|
$file = new Document([
|
||||||
'$id' => $fileId,
|
'$id' => $fileId,
|
||||||
'$permissions' => [
|
'$permissions' => [
|
||||||
Permission::read(Role::team(ID::custom($teamId))),
|
Permission::read(Role::team(ID::custom($teamId))),
|
||||||
],
|
],
|
||||||
'bucketId' => $bucket->getId(),
|
'bucketId' => $bucket->getId(),
|
||||||
'bucketInternalId' => $bucket->getInternalId(),
|
'bucketInternalId' => $bucket->getInternalId(),
|
||||||
'name' => $fileName,
|
'name' => $fileName,
|
||||||
'path' => $path,
|
'path' => $path,
|
||||||
'signature' => $deviceForFiles->getFileHash($path),
|
'signature' => $deviceForFiles->getFileHash($path),
|
||||||
'mimeType' => $deviceForFiles->getFileMimeType($path),
|
'mimeType' => $deviceForFiles->getFileMimeType($path),
|
||||||
'sizeOriginal' => \strlen($screenshot),
|
'sizeOriginal' => \strlen($screenshot),
|
||||||
'sizeActual' => $deviceForFiles->getFileSize($path),
|
'sizeActual' => $deviceForFiles->getFileSize($path),
|
||||||
'algorithm' => Compression::GZIP,
|
'algorithm' => Compression::GZIP,
|
||||||
'comment' => '',
|
'comment' => '',
|
||||||
'chunksTotal' => 1,
|
'chunksTotal' => 1,
|
||||||
'chunksUploaded' => 1,
|
'chunksUploaded' => 1,
|
||||||
'openSSLVersion' => null,
|
'openSSLVersion' => null,
|
||||||
'openSSLCipher' => null,
|
'openSSLCipher' => null,
|
||||||
'openSSLTag' => null,
|
'openSSLTag' => null,
|
||||||
'openSSLIV' => null,
|
'openSSLIV' => null,
|
||||||
'search' => implode(' ', [$fileId, $fileName]),
|
'search' => implode(' ', [$fileId, $fileName]),
|
||||||
'metadata' => ['content_type' => $deviceForFiles->getFileMimeType($path)],
|
'metadata' => ['content_type' => $deviceForFiles->getFileMimeType($path)],
|
||||||
]);
|
]);
|
||||||
$file = Authorization::skip(fn () => $dbForPlatform->createDocument('bucket_' . $bucket->getInternalId(), $file));
|
$file = Authorization::skip(fn () => $dbForPlatform->createDocument('bucket_' . $bucket->getInternalId(), $file));
|
||||||
|
|
||||||
$deployment->setAttribute($key, $fileId);
|
return [ 'key' => $key, 'fileId' => $fileId ];
|
||||||
|
};
|
||||||
|
}, \array_keys($configs)));
|
||||||
|
|
||||||
|
foreach ($screenshots as $screenshot) {
|
||||||
|
$deployment->setAttribute($screenshot['key'], $screenshot['fileId']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$dbForProject->updateDocument('deployments', $deployment->getId(), $deployment);
|
$dbForProject->updateDocument('deployments', $deployment->getId(), $deployment);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue