1
0
Fork 0
mirror of synced 2024-09-29 17:01:37 +13:00

feat: remove handle shutdown method

This commit is contained in:
Christy Jacob 2022-02-03 20:22:56 +04:00
parent 7f1df839ec
commit 0b36c7e21c

View file

@ -6,8 +6,6 @@ use Appwrite\Messaging\Adapter\Realtime;
use Appwrite\Stats\Stats; use Appwrite\Stats\Stats;
use Appwrite\Utopia\Response; use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model\Execution; use Appwrite\Utopia\Response\Model\Execution;
use Cron\CronExpression;
use LanguageServerProtocol\Range;
use Swoole\ConnectionPool; use Swoole\ConnectionPool;
use Swoole\Coroutine as Co; use Swoole\Coroutine as Co;
use Swoole\Http\Request as SwooleRequest; use Swoole\Http\Request as SwooleRequest;
@ -16,19 +14,13 @@ use Swoole\Http\Server;
use Swoole\Process; use Swoole\Process;
use Utopia\App; use Utopia\App;
use Utopia\CLI\Console; use Utopia\CLI\Console;
use Utopia\Cache\Adapter\Redis as RedisCache;
use Utopia\Cache\Cache;
use Utopia\Config\Config; use Utopia\Config\Config;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\UID; use Utopia\Database\Validator\UID;
use Utopia\Logger\Log; use Utopia\Logger\Log;
use Utopia\Orchestration\Adapter\DockerCLI; use Utopia\Orchestration\Adapter\DockerCLI;
use Utopia\Orchestration\Orchestration; use Utopia\Orchestration\Orchestration;
use Utopia\Registry\Registry;
use Utopia\Storage\Device\Local; use Utopia\Storage\Device\Local;
use Utopia\Storage\Storage; use Utopia\Storage\Storage;
use Utopia\Swoole\Request; use Utopia\Swoole\Request;
@ -760,83 +752,81 @@ App::setMode(App::MODE_TYPE_PRODUCTION); // Define Mode
$http = new Server("0.0.0.0", 80); $http = new Server("0.0.0.0", 80);
function handleShutdown() // function handleShutdown()
{ // {
global $orchestrationPool; // global $orchestrationPool;
global $register; // global $register;
try { // try {
Console::info('Cleaning up containers before shutdown...'); // Console::info('Cleaning up containers before shutdown...');
// Remove all containers. // // Remove all containers.
/** @var Orchestration $orchestration */ // /** @var Orchestration $orchestration */
$orchestration = $orchestrationPool->get(); // $orchestration = $orchestrationPool->get();
$functionsToRemove = $orchestration->list(['label' => 'appwrite-type=function']); // $functionsToRemove = $orchestration->list(['label' => 'appwrite-type=function']);
foreach ($functionsToRemove as $container) { // foreach ($functionsToRemove as $container) {
go(fn () => $orchestration->remove($container->getId(), true)); // go(fn () => $orchestration->remove($container->getId(), true));
// Get a database instance // // Get a database instance
$db = $register->get('dbPool')->get(); // $db = $register->get('dbPool')->get();
$cache = $register->get('redisPool')->get(); // $cache = $register->get('redisPool')->get();
$cache = new Cache(new RedisCache($cache)); // $cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache); // $database = new Database(new MariaDB($db), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite')); // $database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace('_project_' . $container->getLabels()["appwrite-project"]); // $database->setNamespace('_project_' . $container->getLabels()["appwrite-project"]);
// Get list of all processing executions // // Get list of all processing executions
$executions = $database->find('executions', [ // $executions = $database->find('executions', [
new Query('deploymentId', Query::TYPE_EQUAL, [$container->getLabels()["appwrite-deployment"]]), // new Query('deploymentId', Query::TYPE_EQUAL, [$container->getLabels()["appwrite-deployment"]]),
new Query('status', Query::TYPE_EQUAL, ['waiting']) // new Query('status', Query::TYPE_EQUAL, ['waiting'])
]); // ]);
// Mark all processing executions as failed // // Mark all processing executions as failed
foreach ($executions as $execution) { // foreach ($executions as $execution) {
$execution // $execution
->setAttribute('status', 'failed') // ->setAttribute('status', 'failed')
->setAttribute('statusCode', 1) // ->setAttribute('statusCode', 1)
->setAttribute('stderr', 'Appwrite was shutdown during execution'); // ->setAttribute('stderr', 'Appwrite was shutdown during execution');
$database->updateDocument('executions', $execution->getId(), $execution); // $database->updateDocument('executions', $execution->getId(), $execution);
} // }
Console::info('Removed container ' . $container->getName()); // Console::info('Removed container ' . $container->getName());
} // }
} catch (\Throwable $error) { // } catch (\Throwable $error) {
logError($error, 'shutdownError'); // logError($error, 'shutdownError');
} finally { // } finally {
$orchestrationPool->put($orchestration); // $orchestrationPool->put($orchestration);
} // }
}; // };
$http->on('start', function ($http) { $http->on('start', function ($http) {
@Process::signal(SIGINT, function () use ($http) { @Process::signal(SIGINT, function () use ($http) {
handleShutdown(); // handleShutdown();
$http->shutdown(); $http->shutdown();
}); });
@Process::signal(SIGQUIT, function () use ($http) { @Process::signal(SIGQUIT, function () use ($http) {
handleShutdown(); // handleShutdown();
$http->shutdown(); $http->shutdown();
}); });
@Process::signal(SIGKILL, function () use ($http) { @Process::signal(SIGKILL, function () use ($http) {
handleShutdown(); // handleShutdown();
$http->shutdown(); $http->shutdown();
}); });
@Process::signal(SIGTERM, function () use ($http) { @Process::signal(SIGTERM, function () use ($http) {
handleShutdown(); // handleShutdown();
$http->shutdown(); $http->shutdown();
}); });
}); });
$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) { $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
global $register;
$request = new Request($swooleRequest); $request = new Request($swooleRequest);
$response = new Response($swooleResponse); $response = new Response($swooleResponse);
$app = new App('UTC'); $app = new App('UTC');
@ -911,7 +901,6 @@ $http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swo
} catch (Exception $e) { } catch (Exception $e) {
logError($e, "serverError"); logError($e, "serverError");
$swooleResponse->end('500: Server Error'); $swooleResponse->end('500: Server Error');
} finally {
} }
}); });