Skip to content
Open
Changes from 3 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
16 changes: 12 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,8 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
}
}

$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 +765,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);
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 +921,8 @@ 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) {

if ($runtime['updated'] < $inactiveThreshold && $runtime['executions'] === 0) {
go(function () use ($activeRuntimeId, $runtime, $orchestrationPool, $activeRuntimes) {
try {
$connection = $orchestrationPool->pop();
Expand Down