1
0
Fork 0
mirror of synced 2024-09-20 03:17:30 +12:00
This commit is contained in:
Matej Bačo 2024-08-22 10:51:08 +00:00
parent 4424955e64
commit 2a6873b756
3 changed files with 39 additions and 16 deletions

View file

@ -1682,8 +1682,17 @@ App::patch('/v1/functions/:functionId/deployments/:deploymentId/build')
])); ]));
} }
$executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST')); $dbForProject->purgeCachedDocument('deployments', $deployment->getId());
$deleteBuild = $executor->deleteRuntime($project->getId(), $deploymentId . "-build");
try {
$executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
$executor->deleteRuntime($project->getId(), $deploymentId . "-build");
} catch (\Throwable $th) {
// Don't throw if the deployment doesn't exist
if ($th->getCode() !== 404) {
throw $th;
}
}
$queueForEvents $queueForEvents
->setParam('functionId', $function->getId()) ->setParam('functionId', $function->getId())

View file

@ -511,6 +511,8 @@ class Builds extends Action
return; return;
} }
$isCanceled = false;
Co::join([ Co::join([
Co\go(function () use ($executor, &$response, $project, $deployment, $source, $function, $runtime, $vars, $command, $cpus, $memory, &$err) { Co\go(function () use ($executor, &$response, $project, $deployment, $source, $function, $runtime, $vars, $command, $cpus, $memory, &$err) {
try { try {
@ -535,12 +537,16 @@ class Builds extends Action
$err = $error; $err = $error;
} }
}), }),
Co\go(function () use ($executor, $project, $deployment, &$response, &$build, $dbForProject, $allEvents, &$err) { Co\go(function () use ($executor, $project, $deployment, &$response, &$build, $dbForProject, $allEvents, &$err, &$isCanceled) {
try { try {
$executor->getLogs( $executor->getLogs(
deploymentId: $deployment->getId(), deploymentId: $deployment->getId(),
projectId: $project->getId(), projectId: $project->getId(),
callback: function ($logs) use (&$response, &$err, &$build, $dbForProject, $allEvents, $project) { callback: function ($logs) use (&$response, &$err, &$build, $dbForProject, $allEvents, $project, &$isCanceled) {
if($isCanceled) {
return;
}
// If we have response or error from concurrent coroutine, we already have latest logs // If we have response or error from concurrent coroutine, we already have latest logs
if ($response === null && $err === null) { if ($response === null && $err === null) {
$build = $dbForProject->getDocument('builds', $build->getId()); $build = $dbForProject->getDocument('builds', $build->getId());
@ -549,6 +555,12 @@ class Builds extends Action
throw new \Exception('Build not found', 404); throw new \Exception('Build not found', 404);
} }
if ($build->getAttribute('status') === 'canceled') {
$isCanceled = true;
Console::info('Ignoring realtime logs because build has been canceled');
return;
}
$logs = \mb_substr($logs, 0, null, 'UTF-8'); // Get only valid UTF8 part - removes leftover half-multibytes causing SQL errors $logs = \mb_substr($logs, 0, null, 'UTF-8'); // Get only valid UTF8 part - removes leftover half-multibytes causing SQL errors
$build = $build->setAttribute('logs', $build->getAttribute('logs', '') . $logs); $build = $build->setAttribute('logs', $build->getAttribute('logs', '') . $logs);
@ -607,6 +619,8 @@ class Builds extends Action
$build->setAttribute('size', $response['size']); $build->setAttribute('size', $response['size']);
$build->setAttribute('logs', $response['output']); $build->setAttribute('logs', $response['output']);
$build = $dbForProject->updateDocument('builds', $buildId, $build);
if ($isVcsEnabled) { if ($isVcsEnabled) {
$this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $function, $deployment->getId(), $dbForProject, $dbForConsole); $this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $function, $deployment->getId(), $dbForProject, $dbForConsole);
} }
@ -648,12 +662,12 @@ class Builds extends Action
$build->setAttribute('status', 'failed'); $build->setAttribute('status', 'failed');
$build->setAttribute('logs', $th->getMessage()); $build->setAttribute('logs', $th->getMessage());
$build = $dbForProject->updateDocument('builds', $buildId, $build);
if ($isVcsEnabled) { if ($isVcsEnabled) {
$this->runGitAction('failed', $github, $providerCommitHash, $owner, $repositoryName, $project, $function, $deployment->getId(), $dbForProject, $dbForConsole); $this->runGitAction('failed', $github, $providerCommitHash, $owner, $repositoryName, $project, $function, $deployment->getId(), $dbForProject, $dbForConsole);
} }
} finally { } finally {
$build = $dbForProject->updateDocument('builds', $buildId, $build);
/** /**
* Send realtime Event * Send realtime Event
*/ */

View file

@ -514,7 +514,7 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(200, $deployments['headers']['status-code']); $this->assertEquals(200, $deployments['headers']['status-code']);
$this->assertEquals(1, $deployments['body']['total']); $this->assertEquals(1, $deployments['body']['total']);
$this->assertNotEmpty($deployments['body']['deployments'][0]['$id']); $this->assertNotEmpty($deployments['body']['deployments'][0]['$id']);
$this->assertGreaterThan(0, $deployments['body']['deployments'][0]['size']); $this->assertEquals(0, $deployments['body']['deployments'][0]['size']);
$deploymentId = $deployments['body']['deployments'][0]['$id']; $deploymentId = $deployments['body']['deployments'][0]['$id'];
@ -522,6 +522,13 @@ class FunctionsCustomServerTest extends Scope
// Deployment is automatically activated // Deployment is automatically activated
$this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId); $this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId);
$deployments = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments/' . $deploymentId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
$this->assertGreaterThan(0, $deployments['body']['size']);
$function = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId, array_merge([ $function = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId, array_merge([
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],
@ -689,19 +696,12 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(200, $cancel['headers']['status-code']); $this->assertEquals(200, $cancel['headers']['status-code']);
$this->assertEquals('canceled', $cancel['body']['status']); $this->assertEquals('canceled', $cancel['body']['status']);
// Confirm the deployment is canceled
$deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/deployments/' . $deploymentId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
/** /**
* Build worker still runs the build. * Build worker still runs the build.
* 15s sleep gives worker enough time to finish build. * 30s sleep gives worker enough time to finish build.
* After build finished, it should still be canceled, not ready. * After build finished, it should still be canceled, not ready.
*/ */
\sleep(15); \sleep(30);
$deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/deployments/' . $deploymentId, [ $deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/deployments/' . $deploymentId, [
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],