feat: update deployment status
This commit is contained in:
parent
2c91a24928
commit
ada65e9819
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -436,7 +436,7 @@ function execute(string $trigger, string $projectId, string $executionId, string
|
||||||
|
|
||||||
$database->updateDocument('deployments', $deployment->getId(), $deployment);
|
$database->updateDocument('deployments', $deployment->getId(), $deployment);
|
||||||
|
|
||||||
runBuildStage($buildId, $projectId);
|
runBuildStage($buildId, $deployment->getId(), $projectId);
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$execution
|
$execution
|
||||||
|
@ -659,7 +659,7 @@ function execute(string $trigger, string $projectId, string $executionId, string
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
function runBuildStage(string $buildId, string $projectID): Document
|
function runBuildStage(string $buildId, string $deploymentId, string $projectID): Document
|
||||||
{
|
{
|
||||||
global $runtimes;
|
global $runtimes;
|
||||||
global $orchestrationPool;
|
global $orchestrationPool;
|
||||||
|
@ -681,6 +681,7 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
|
|
||||||
// Check if build has already been run
|
// Check if build has already been run
|
||||||
$build = $database->getDocument('builds', $buildId);
|
$build = $database->getDocument('builds', $buildId);
|
||||||
|
$deployment = $database->getDocument('deployments', $deploymentId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// If we already have a built package ready there is no need to rebuild.
|
// If we already have a built package ready there is no need to rebuild.
|
||||||
|
@ -690,8 +691,9 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
|
|
||||||
// Update deployment Status
|
// Update deployment Status
|
||||||
$build->setAttribute('status', 'building');
|
$build->setAttribute('status', 'building');
|
||||||
|
$deployment->setAttribute('status', 'building');
|
||||||
$database->updateDocument('builds', $build->getId(), $build);
|
$database->updateDocument('builds', $buildId, $build);
|
||||||
|
$database->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
|
|
||||||
// Check if runtime is active
|
// Check if runtime is active
|
||||||
$runtime = $runtimes[$build->getAttribute('runtime', '')] ?? null;
|
$runtime = $runtimes[$build->getAttribute('runtime', '')] ?? null;
|
||||||
|
@ -702,14 +704,12 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
|
|
||||||
// Grab Deployment Files
|
// Grab Deployment Files
|
||||||
$deploymentPath = $build->getAttribute('source', '');
|
$deploymentPath = $build->getAttribute('source', '');
|
||||||
$sourceType = $build->getAttribute('sourceType', '');
|
|
||||||
|
|
||||||
$device = Storage::getDevice('builds');
|
$device = Storage::getDevice('builds');
|
||||||
|
|
||||||
$deploymentPathTarget = '/tmp/project-' . $projectID . '/' . $build->getId() . '/code.tar.gz';
|
$deploymentPathTarget = '/tmp/project-' . $projectID . '/' . $buildId . '/code.tar.gz';
|
||||||
$deploymentPathTargetDir = \pathinfo($deploymentPathTarget, PATHINFO_DIRNAME);
|
$deploymentPathTargetDir = \pathinfo($deploymentPathTarget, PATHINFO_DIRNAME);
|
||||||
|
|
||||||
$container = 'build-stage-' . $build->getId();
|
$container = 'build-stage-' . $buildId;
|
||||||
|
|
||||||
// Perform various checks
|
// Perform various checks
|
||||||
if (!\file_exists($deploymentPathTargetDir)) {
|
if (!\file_exists($deploymentPathTargetDir)) {
|
||||||
|
@ -747,13 +747,13 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
->setSwap(App::getEnv('_APP_FUNCTIONS_MEMORY_SWAP', 256));
|
->setSwap(App::getEnv('_APP_FUNCTIONS_MEMORY_SWAP', 256));
|
||||||
|
|
||||||
$vars = array_map(fn ($v) => strval($v), $vars);
|
$vars = array_map(fn ($v) => strval($v), $vars);
|
||||||
$path = '/tmp/project-' . $projectID . '/' . $build->getId() . '/builtCode';
|
$path = '/tmp/project-' . $projectID . '/' . $buildId . '/builtCode';
|
||||||
|
|
||||||
if (!\file_exists($path)) {
|
if (!\file_exists($path)) {
|
||||||
if (@\mkdir($path, 0777, true)) {
|
if (@\mkdir($path, 0777, true)) {
|
||||||
\chmod($path, 0777);
|
\chmod($path, 0777);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('Can\'t create directory /tmp/project-' . $projectID . '/' . $build->getId() . '/builtCode');
|
throw new Exception('Can\'t create directory /tmp/project-' . $projectID . '/' . $buildId . '/builtCode');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -768,7 +768,7 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
'appwrite-created' => strval($time),
|
'appwrite-created' => strval($time),
|
||||||
'appwrite-runtime' => $build->getAttribute('runtime', ''),
|
'appwrite-runtime' => $build->getAttribute('runtime', ''),
|
||||||
'appwrite-project' => $projectID,
|
'appwrite-project' => $projectID,
|
||||||
'appwrite-build' => $build->getId(),
|
'appwrite-build' => $buildId,
|
||||||
],
|
],
|
||||||
command: [
|
command: [
|
||||||
'tail',
|
'tail',
|
||||||
|
@ -778,7 +778,7 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
hostname: $container,
|
hostname: $container,
|
||||||
mountFolder: $deploymentPathTargetDir,
|
mountFolder: $deploymentPathTargetDir,
|
||||||
volumes: [
|
volumes: [
|
||||||
'/tmp/project-' . $projectID . '/' . $build->getId() . '/builtCode' . ':/usr/builtCode:rw'
|
'/tmp/project-' . $projectID . '/' . $buildId . '/builtCode' . ':/usr/builtCode:rw'
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -884,6 +884,9 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
// Update build with built code attribute
|
// Update build with built code attribute
|
||||||
$build = $database->updateDocument('builds', $buildId, $build);
|
$build = $database->updateDocument('builds', $buildId, $build);
|
||||||
|
|
||||||
|
$deployment->setAttribute('status', 'ready');
|
||||||
|
$database->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
|
|
||||||
$buildEnd = \microtime(true);
|
$buildEnd = \microtime(true);
|
||||||
|
|
||||||
Console::info('Build Stage Ran in ' . ($buildEnd - $buildStart) . ' seconds');
|
Console::info('Build Stage Ran in ' . ($buildEnd - $buildStart) . ' seconds');
|
||||||
|
@ -895,6 +898,9 @@ function runBuildStage(string $buildId, string $projectID): Document
|
||||||
|
|
||||||
$build = $database->updateDocument('builds', $buildId, $build);
|
$build = $database->updateDocument('builds', $buildId, $build);
|
||||||
|
|
||||||
|
$deployment->setAttribute('status', 'failed');
|
||||||
|
$database->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
|
|
||||||
// also remove the container if it exists
|
// also remove the container if it exists
|
||||||
if (isset($id)) {
|
if (isset($id)) {
|
||||||
$orchestration->remove($id, true);
|
$orchestration->remove($id, true);
|
||||||
|
@ -1099,18 +1105,18 @@ App::post('/v1/functions/:functionId/deployments/:deploymentId/builds/:buildId')
|
||||||
throw new Exception('Build not found', 404);
|
throw new Exception('Build not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($build->getAttribute('status') === 'running') {
|
if ($build->getAttribute('status') === 'building') {
|
||||||
throw new Exception('Build is already running', 409);
|
throw new Exception('Build is already running', 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if build is already finished
|
// Check if build is already finished
|
||||||
if ($build->getAttribute('status') === 'finished') {
|
if ($build->getAttribute('status') === 'ready') {
|
||||||
throw new Exception('Build is already finished', 409);
|
throw new Exception('Build is already finished', 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
go(function() use ($functionId, $deploymentId, $buildId, $projectId, $dbForProject, $function, $deployment) {
|
go(function() use ($functionId, $deploymentId, $buildId, $projectId, $dbForProject, $function, $deployment) {
|
||||||
Console::info('Starting build for deployment ' . $deployment['$id']);
|
Console::info('Starting build for deployment ' . $deployment['$id']);
|
||||||
runBuildStage($buildId, $projectId);
|
runBuildStage($buildId, $deploymentId, $projectId);
|
||||||
|
|
||||||
// Update the schedule
|
// Update the schedule
|
||||||
$schedule = $function->getAttribute('schedule', '');
|
$schedule = $function->getAttribute('schedule', '');
|
||||||
|
|
|
@ -136,10 +136,6 @@ class BuildsV1 extends Worker
|
||||||
'APPWRITE_FUNCTION_PROJECT_ID' => $projectId,
|
'APPWRITE_FUNCTION_PROJECT_ID' => $projectId,
|
||||||
]
|
]
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$deployment->setAttribute('buildId', $buildId);
|
|
||||||
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
|
||||||
|
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
$deployment->setAttribute('status', 'failed');
|
$deployment->setAttribute('status', 'failed');
|
||||||
$deployment->setAttribute('buildId', '');
|
$deployment->setAttribute('buildId', '');
|
||||||
|
@ -151,7 +147,7 @@ class BuildsV1 extends Worker
|
||||||
|
|
||||||
// Build the Code
|
// Build the Code
|
||||||
try {
|
try {
|
||||||
$deployment->setAttribute('status', 'building');
|
$deployment->setAttribute('buildId', $buildId);
|
||||||
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $deployment);
|
||||||
$this->createBuild($projectId, $functionId, $deploymentId, $buildId);
|
$this->createBuild($projectId, $functionId, $deploymentId, $buildId);
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
|
|
Loading…
Reference in a new issue