diff --git a/app/executor.php b/app/executor.php index a21779c896..79a6902bf5 100644 --- a/app/executor.php +++ b/app/executor.php @@ -28,6 +28,7 @@ use Utopia\Storage\Device\S3; use Utopia\Storage\Storage; use Utopia\Swoole\Request; use Utopia\Swoole\Response; +use Utopia\System\System; use Utopia\Validator\ArrayList; use Utopia\Validator\Assoc; use Utopia\Validator\Boolean; @@ -256,6 +257,8 @@ App::post('/v1/runtimes') '/dev/null' ] : []; + $executorName = \explode('-', $runtimeId)[0]; + $containerId = $orchestration->run( image: $baseImage, name: $runtimeId, @@ -265,6 +268,7 @@ App::post('/v1/runtimes') labels: [ 'openruntimes-id' => $runtimeId, 'openruntimes-type' => 'runtime', + 'openruntimes-executor' => $executorName, 'openruntimes-created' => strval($startTimeUnix), 'openruntimes-runtime' => $runtime, ], @@ -645,22 +649,31 @@ App::post('/v1/execution') ); App::get('/v1/health') + ->param('name', '', new Text(64), 'Name of the executor.') ->desc("Get usage stats of host machine") ->inject('response') - ->action(function (Response $response) { + ->action(function (string $name, Response $response) use ($orchestrationPool) { + $systemCores = System::getCPUCores(); + $systemUsage = System::getCPUUtilisation() / $systemCores; + $functionsUsage = []; + + try { + $orchestration = $orchestrationPool->get(); + $runtimes = $orchestration->list(['label' => 'openruntimes-executor=' . $name]); + // $cpuUsages = $orchestration->getStats([ 'label' => 'openruntimes-executor=' . $name ]); // TODO: Add duration 4s, add label + } catch(\Exception $err) { + // TODO: Handle better + \var_dump($err); + } finally { + $orchestrationPool->put($orchestration); + } + $response ->setStatusCode(Response::STATUS_CODE_OK) ->json([ 'status' => 'pass', - /* - TODO: Add host machine stats using Utopia/System and Utopia/orchestrator. Something like: - - 'hostUsage' => 0.8, - 'functionRuntimeUsages' => [ - '[FUNCTION_ID]' => 0.2 - ] - */ - + 'hostUsage' => $systemUsage, + 'functionsUsage' => $functionsUsage ]); }); diff --git a/app/functionsProxy.php b/app/functionsProxy.php index 5941beaf3c..011803bfb9 100644 --- a/app/functionsProxy.php +++ b/app/functionsProxy.php @@ -79,7 +79,7 @@ function fetchExecutorsState(RedisPool $redisPool, bool $forceShowError = false) try { [$id, $hostname] = \explode('=', $executor); - $endpoint = 'http://' . $hostname . '/v1/health'; + $endpoint = 'http://' . $hostname . '/v1/health?name=' . $id; $ch = \curl_init(); diff --git a/composer.json b/composer.json index 973aec8539..cf0bfaca96 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "ext-zlib": "*", "ext-sockets": "*", "appwrite/php-clamav": "1.1.*", - "appwrite/php-runtimes": "0.11.*", + "appwrite/php-runtimes": "dev-feat-upgrade-system-version as 0.11.0", + "utopia-php/system": "0.5.*", "utopia-php/framework": "0.21.*", "utopia-php/logger": "0.3.*", "utopia-php/abuse": "0.13.*", diff --git a/composer.lock b/composer.lock index cd2040eb1a..8ef306f2b8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "568151395a8877f87d9bdce048adc2dc", + "content-hash": "60436077b574163b67a5d7cd221443cd", "packages": [ { "name": "adhocore/jwt", @@ -115,15 +115,15 @@ }, { "name": "appwrite/php-runtimes", - "version": "0.11.0", + "version": "dev-feat-upgrade-system-version", "source": { "type": "git", "url": "https://github.com/appwrite/runtimes.git", - "reference": "547fc026e11c0946846a8ac690898f5bf53be101" + "reference": "8679a0d36b1bd54f4b5a295e7097630d6cd4de9c" }, "require": { "php": ">=8.0", - "utopia-php/system": "0.4.*" + "utopia-php/system": "0.5.*" }, "require-dev": { "phpunit/phpunit": "^9.3", @@ -154,7 +154,7 @@ "php", "runtimes" ], - "time": "2022-08-15T14:03:36+00:00" + "time": "2022-10-13T12:30:32+00:00" }, { "name": "chillerlan/php-qrcode", @@ -2667,16 +2667,16 @@ }, { "name": "utopia-php/system", - "version": "0.4.0", + "version": "0.5.0", "source": { "type": "git", "url": "https://github.com/utopia-php/system.git", - "reference": "67c92c66ce8f0cc925a00bca89f7a188bf9183c0" + "reference": "698a58aeaba0d939d2fec6a88a7422ce6e59e870" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/system/zipball/67c92c66ce8f0cc925a00bca89f7a188bf9183c0", - "reference": "67c92c66ce8f0cc925a00bca89f7a188bf9183c0", + "url": "https://api.github.com/repos/utopia-php/system/zipball/698a58aeaba0d939d2fec6a88a7422ce6e59e870", + "reference": "698a58aeaba0d939d2fec6a88a7422ce6e59e870", "shasum": "" }, "require": { @@ -2684,6 +2684,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.3", + "squizlabs/php_codesniffer": "^3.6", "vimeo/psalm": "4.0.1" }, "type": "library", @@ -2716,9 +2717,9 @@ ], "support": { "issues": "https://github.com/utopia-php/system/issues", - "source": "https://github.com/utopia-php/system/tree/0.4.0" + "source": "https://github.com/utopia-php/system/tree/0.5.0" }, - "time": "2021-02-04T14:14:49+00:00" + "time": "2022-09-23T13:24:58+00:00" }, { "name": "utopia-php/websocket", @@ -5358,9 +5359,18 @@ "time": "2022-09-28T08:42:51+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "appwrite/php-runtimes", + "version": "dev-feat-upgrade-system-version", + "alias": "0.11.0", + "alias_normalized": "0.11.0.0" + } + ], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "appwrite/php-runtimes": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { @@ -5384,5 +5394,5 @@ "platform-overrides": { "php": "8.0" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.2.0" } diff --git a/docker-compose.yml b/docker-compose.yml index 84cf4027f0..87ff889e99 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -483,6 +483,8 @@ services: - DEBUG=false - TESTING=true - VERSION=dev + ports: + - 23421:80 networks: appwrite: runtimes: