Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Active executions state #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 21 additions & 4 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -311,6 +312,7 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
'updated' => $startTime,
'status' => 'pending',
'key' => $secret,
'executions' => 0
]);

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
Meldiron marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible for the count to increment, but never decrement if the code between throws an exception


// Finish request
$response
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down