Merge pull request #10053 from appwrite/fix-build-duration

Fix: build duration calculation
This commit is contained in:
Matej Bačo 2025-06-25 13:41:24 +02:00 committed by GitHub
commit 213cbf055a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 14 deletions

View file

@ -209,6 +209,9 @@ class Builds extends Action
Executor $executor, Executor $executor,
array $plan array $plan
): void { ): void {
$startTime = DateTime::now();
$durationStart = \microtime(true);
$resourceKey = match ($resource->getCollection()) { $resourceKey = match ($resource->getCollection()) {
'functions' => 'functionId', 'functions' => 'functionId',
'sites' => 'siteId', 'sites' => 'siteId',
@ -260,9 +263,6 @@ class Builds extends Action
->setParam($resourceKey, $resource->getId()) ->setParam($resourceKey, $resource->getId())
->setParam('deploymentId', $deployment->getId()); ->setParam('deploymentId', $deployment->getId());
$startTime = DateTime::now();
$durationStart = \microtime(true);
if ($deployment->getAttribute('status') === 'canceled') { if ($deployment->getAttribute('status') === 'canceled') {
Console::info('Build has been canceled'); Console::info('Build has been canceled');
return; return;
@ -810,9 +810,6 @@ class Builds extends Action
throw $err; throw $err;
} }
$endTime = DateTime::now();
$durationEnd = \microtime(true);
$buildSizeLimit = (int)System::getEnv('_APP_COMPUTE_BUILD_SIZE_LIMIT', '2000000000'); $buildSizeLimit = (int)System::getEnv('_APP_COMPUTE_BUILD_SIZE_LIMIT', '2000000000');
if (isset($plan['buildSize'])) { if (isset($plan['buildSize'])) {
$buildSizeLimit = $plan['buildSize'] * 1000 * 1000; $buildSizeLimit = $plan['buildSize'] * 1000 * 1000;
@ -821,10 +818,6 @@ class Builds extends Action
throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / (1000 * 1000), 2) . ' MBs.'); throw new \Exception('Build size should be less than ' . number_format($buildSizeLimit / (1000 * 1000), 2) . ' MBs.');
} }
/** Update the build document */
$deployment->setAttribute('buildStartedAt', DateTime::format((new \DateTime())->setTimestamp(floor($response['startTime']))));
$deployment->setAttribute('buildEndedAt', $endTime);
$deployment->setAttribute('buildDuration', \intval(\ceil($durationEnd - $durationStart)));
$deployment->setAttribute('buildPath', $response['path']); $deployment->setAttribute('buildPath', $response['path']);
$deployment->setAttribute('buildSize', $response['size']); $deployment->setAttribute('buildSize', $response['size']);
$deployment->setAttribute('totalSize', $deployment->getAttribute('buildSize', 0) + $deployment->getAttribute('sourceSize', 0)); $deployment->setAttribute('totalSize', $deployment->getAttribute('buildSize', 0) + $deployment->getAttribute('sourceSize', 0));
@ -1191,6 +1184,15 @@ class Builds extends Action
} }
} }
$endTime = DateTime::now();
$durationEnd = \microtime(true);
$deployment->setAttribute('buildEndedAt', $endTime);
$deployment->setAttribute('buildDuration', \intval(\ceil($durationEnd - $durationStart)));
$deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment);
$queueForRealtime
->setPayload($deployment->getArrayCopy())
->trigger();
if ($dbForProject->getDocument('deployments', $deploymentId)->getAttribute('status') === 'canceled') { if ($dbForProject->getDocument('deployments', $deploymentId)->getAttribute('status') === 'canceled') {
Console::info('Build has been canceled'); Console::info('Build has been canceled');
return; return;
@ -1234,7 +1236,7 @@ class Builds extends Action
// Combine with previous logs if deployment got past build process // Combine with previous logs if deployment got past build process
$previousLogs = ''; $previousLogs = '';
if (!empty($deployment->getAttribute('buildEndedAt', ''))) { if (!is_null($deployment->getAttribute('buildSize', null))) {
$previousLogs = $deployment->getAttribute('buildLogs', ''); $previousLogs = $deployment->getAttribute('buildLogs', '');
if (!empty($previousLogs)) { if (!empty($previousLogs)) {
$message = $previousLogs . "\n" . $message; $message = $previousLogs . "\n" . $message;

View file

@ -607,10 +607,8 @@ class RealtimeConsoleClientTest extends Scope
$this->assertContains("projects.{$projectId}", $response['data']['channels']); $this->assertContains("projects.{$projectId}", $response['data']['channels']);
$this->assertArrayHasKey('buildLogs', $response['data']['payload']); $this->assertArrayHasKey('buildLogs', $response['data']['payload']);
if (!empty($response['data']['payload']['buildEndedAt'])) { if (!empty($response['data']['payload']['buildSize'])) {
$this->assertNotEmpty($response['data']['payload']['buildEndedAt']);
$this->assertNotEmpty($response['data']['payload']['buildStartedAt']); $this->assertNotEmpty($response['data']['payload']['buildStartedAt']);
$this->assertNotEmpty($response['data']['payload']['buildDuration']);
$this->assertNotEmpty($response['data']['payload']['buildPath']); $this->assertNotEmpty($response['data']['payload']['buildPath']);
$this->assertNotEmpty($response['data']['payload']['buildSize']); $this->assertNotEmpty($response['data']['payload']['buildSize']);
$this->assertNotEmpty($response['data']['payload']['totalSize']); $this->assertNotEmpty($response['data']['payload']['totalSize']);
@ -634,6 +632,13 @@ class RealtimeConsoleClientTest extends Scope
$this->assertContains("projects.{$projectId}", $response['data']['channels']); $this->assertContains("projects.{$projectId}", $response['data']['channels']);
$this->assertEquals("ready", $response['data']['payload']['status']); $this->assertEquals("ready", $response['data']['payload']['status']);
$response = json_decode($client->receive(), true);
$this->assertContains("functions.{$functionId}.deployments.{$deploymentId}.update", $response['data']['events']);
$this->assertContains('console', $response['data']['channels']);
$this->assertContains("projects.{$projectId}", $response['data']['channels']);
$this->assertNotEmpty($response['data']['payload']['buildDuration']);
$this->assertNotEmpty($response['data']['payload']['buildEndedAt']);
$client->close(); $client->close();
} }
} }