diff --git a/.env b/.env index 2e0c698..5b155e6 100644 --- a/.env +++ b/.env @@ -2,6 +2,7 @@ OPR_EXECUTOR_ENV=development OPR_EXECUTOR_RUNTIMES=php-8.1,dart-2.17,deno-1.24,node-18.0,python-3.9,ruby-3.1,cpp-17 OPR_EXECUTOR_CONNECTION_STORAGE=file://localhost OPR_EXECUTOR_INACTIVE_TRESHOLD=60 +OPR_EXECUTOR_RUNTIME_TRESHOLD=600 OPR_EXECUTOR_MAINTENANCE_INTERVAL=60 OPR_EXECUTOR_NETWORK=openruntimes-runtimes OPR_EXECUTOR_SECRET=executor-secret-key diff --git a/README.md b/README.md index c356b09..ab14567 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ services: - OPR_EXECUTOR_RUNTIMES - OPR_EXECUTOR_CONNECTION_STORAGE - OPR_EXECUTOR_INACTIVE_TRESHOLD + - OPR_EXECUTOR_RUNTIME_TRESHOLD - OPR_EXECUTOR_MAINTENANCE_INTERVAL - OPR_EXECUTOR_NETWORK - OPR_EXECUTOR_SECRET @@ -78,6 +79,7 @@ OPR_EXECUTOR_ENV=development OPR_EXECUTOR_RUNTIMES=php-8.0 OPR_EXECUTOR_CONNECTION_STORAGE=file://localhost OPR_EXECUTOR_INACTIVE_TRESHOLD=60 +OPR_EXECUTOR_RUNTIME_TRESHOLD=60 OPR_EXECUTOR_MAINTENANCE_INTERVAL=60 OPR_EXECUTOR_NETWORK=openruntimes-runtimes OPR_EXECUTOR_SECRET=executor-secret-key diff --git a/app/http.php b/app/http.php index efd8d2e..b5897d6 100644 --- a/app/http.php +++ b/app/http.php @@ -105,6 +105,7 @@ $table->column('hostname', Table::TYPE_STRING, 256); $table->column('status', Table::TYPE_STRING, 128); $table->column('key', Table::TYPE_STRING, 256); + $table->column('executions', Table::TYPE_INT, 11); // Amount of active executions $table->create(); return $table; @@ -311,6 +312,7 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void 'updated' => $startTime, 'status' => 'pending', 'key' => $secret, + 'executions' => 0 ]); /** @@ -445,6 +447,7 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void 'updated' => \microtime(true), 'status' => 'Up ' . \round($duration, 2) . 's', 'key' => $secret, + 'executions' => 0 ]); } catch (Throwable $th) { $localDevice->deletePath($tmpFolder); @@ -635,6 +638,13 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st } } + // Update swoole table + $activeRuntimes->set($activeRuntimeId, [ + 'updated' => \microtime(true) + ]); + + $activeRuntimes->incr($activeRuntimeId, 'executions', 1); + // Ensure runtime started for ($i = 0; $i < 5; $i++) { if ($activeRuntimes->get($activeRuntimeId)['status'] !== 'pending') { @@ -760,9 +770,11 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st ]; // Update swoole table - $runtime = $activeRuntimes->get($activeRuntimeId); - $runtime['updated'] = \microtime(true); - $activeRuntimes->set($activeRuntimeId, $runtime); + $activeRuntimes->set($activeRuntimeId, [ + 'updated' => \microtime(true) + ]); + + $activeRuntimes->decr($activeRuntimeId, 'executions', 1); // Finish request $response @@ -914,7 +926,12 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st Console::info("Running maintenance task ..."); foreach ($activeRuntimes as $activeRuntimeId => $runtime) { $inactiveThreshold = \time() - \intval(App::getEnv('OPR_EXECUTOR_INACTIVE_TRESHOLD', '60')); - if ($runtime['updated'] < $inactiveThreshold) { + $runtimeThreshold = \time() - \intval(App::getEnv('OPR_EXECUTOR_RUNTIME_TRESHOLD', '600')); + + $softKill = $runtime['updated'] < $inactiveThreshold && $runtime['executions'] === 0; + $forceKill = $runtime['updated'] < $runtimeThreshold; + + if ($softKill || $forceKill) { go(function () use ($activeRuntimeId, $runtime, $orchestrationPool, $activeRuntimes) { try { $connection = $orchestrationPool->pop(); diff --git a/docker-compose.yml b/docker-compose.yml index 048fab6..9cca04a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,6 +37,7 @@ services: - OPR_EXECUTOR_RUNTIMES - OPR_EXECUTOR_CONNECTION_STORAGE - OPR_EXECUTOR_INACTIVE_TRESHOLD + - OPR_EXECUTOR_RUNTIME_TRESHOLD - OPR_EXECUTOR_MAINTENANCE_INTERVAL - OPR_EXECUTOR_NETWORK - OPR_EXECUTOR_SECRET