Clean up deployment artifact tokens

This commit is contained in:
Chirag Aggarwal
2026-05-23 18:36:35 +05:30
parent 6e0eb6ccd9
commit 4f3cf5dbff
4 changed files with 57 additions and 6 deletions
+4
View File
@@ -13,6 +13,10 @@ $traceFunctionId = System::getEnv('_APP_TRACE_FUNCTION_ID', '');
$traceEnabled = $traceProjectId !== '' || $traceFunctionId !== '';
Span::addExporter(new Exporter\Pretty(), function (Span $span) use ($traceEnabled, $traceProjectId, $traceFunctionId): bool {
if ($span->getAction() === 'worker.v1-builds' && $span->get('build.type') === BUILD_TYPE_ORCHESTRATOR_EVENT) {
return $span->getError() !== null;
}
if (\str_starts_with($span->getAction(), 'listener.')) {
return $span->getError() !== null;
}
@@ -13,6 +13,7 @@ use OpenRuntimes\Orchestrator\Client as OrchestratorClient;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Validator\UID;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
@@ -112,6 +113,15 @@ class Update extends Action
]));
}
try {
$dbForProject->getAuthorization()->skip(fn () => $dbForProject->deleteDocuments('resourceTokens', [
Query::equal('resourceType', [TOKENS_RESOURCE_TYPE_DEPLOYMENT_ARTIFACTS]),
Query::equal('resourceInternalId', [$function->getSequence() . ':' . $deployment->getSequence()]),
]));
} catch (\Throwable) {
// Best-effort cleanup; deployment status is already 'canceled'.
}
try {
if (System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator') {
$client = new OrchestratorClient(
@@ -115,8 +115,6 @@ class Builds extends Action
Executor $executor,
array $plan
): void {
Console::log('Build action started');
$payload = $message->getPayload();
if (empty($payload)) {
@@ -132,6 +130,12 @@ class Builds extends Action
$log->addTag('projectId', $project->getId());
$log->addTag('type', $type);
Span::add('project.id', $project->getId());
Span::add('resource.id', $resource->getId());
Span::add('resource.type', $resource->getCollection());
Span::add('deployment.id', $deployment->getId());
Span::add('build.type', $type);
switch ($type) {
case BUILD_TYPE_ORCHESTRATOR_EVENT:
$this->applyOrchestratorEvent(
@@ -214,10 +218,6 @@ class Builds extends Action
array $platform,
int $timeout
): void {
Span::add('project.id', $project->getId());
Span::add('resource.id', $resource->getId());
Span::add('resource.type', $resource->getCollection());
Span::add('deployment.id', $deployment->getId());
Span::add('build.timeout', $timeout);
Console::info('Deployment action started');
@@ -1628,6 +1628,15 @@ class Builds extends Action
'buildLogs' => $logs,
]));
try {
$dbForProject->getAuthorization()->skip(fn () => $dbForProject->deleteDocuments('resourceTokens', [
Query::equal('resourceType', [TOKENS_RESOURCE_TYPE_DEPLOYMENT_ARTIFACTS]),
Query::equal('resourceInternalId', [$resource->getSequence() . ':' . $deployment->getSequence()]),
]));
} catch (\Throwable $error) {
Console::warning('Failed deleting deployment artifact tokens: ' . $error->getMessage());
}
if ($deployment->getSequence() === $resource->getAttribute('latestDeploymentInternalId', '')) {
$resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')]));
}
@@ -1760,6 +1769,15 @@ class Builds extends Action
$resource = $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document(['latestDeploymentStatus' => $deployment->getAttribute('status', '')]));
}
try {
$dbForProject->getAuthorization()->skip(fn () => $dbForProject->deleteDocuments('resourceTokens', [
Query::equal('resourceType', [TOKENS_RESOURCE_TYPE_DEPLOYMENT_ARTIFACTS]),
Query::equal('resourceInternalId', [$resource->getSequence() . ':' . $deployment->getSequence()]),
]));
} catch (\Throwable $error) {
Console::warning('Failed deleting deployment artifact tokens: ' . $error->getMessage());
}
$queueForRealtime
->setPayload($deployment->getArrayCopy())
->trigger();
@@ -1921,6 +1939,15 @@ class Builds extends Action
'buildDuration' => $buildDuration,
]));
try {
$dbForProject->getAuthorization()->skip(fn () => $dbForProject->deleteDocuments('resourceTokens', [
Query::equal('resourceType', [TOKENS_RESOURCE_TYPE_DEPLOYMENT_ARTIFACTS]),
Query::equal('resourceInternalId', [$resource->getSequence() . ':' . $deployment->getSequence()]),
]));
} catch (\Throwable $error) {
Console::warning('Failed deleting deployment artifact tokens: ' . $error->getMessage());
}
$queueForRealtime
->setPayload($deployment->getArrayCopy())
->trigger();
@@ -13,6 +13,7 @@ use OpenRuntimes\Orchestrator\Client as OrchestratorClient;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Validator\UID;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
@@ -110,6 +111,15 @@ class Update extends Action
]));
}
try {
$dbForProject->getAuthorization()->skip(fn () => $dbForProject->deleteDocuments('resourceTokens', [
Query::equal('resourceType', [TOKENS_RESOURCE_TYPE_DEPLOYMENT_ARTIFACTS]),
Query::equal('resourceInternalId', [$site->getSequence() . ':' . $deployment->getSequence()]),
]));
} catch (\Throwable) {
// Best-effort cleanup; deployment status is already 'canceled'.
}
try {
if (System::getEnv('_APP_BUILDS_BACKEND', 'executor') === 'orchestrator') {
$client = new OrchestratorClient(