Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 11 additions & 20 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once __DIR__ . '/../vendor/autoload.php';

use Appwrite\Runtimes\Runtimes;
use OpenRuntimes\Executor\ActiveRuntimes;
use OpenRuntimes\Executor\Usage;
use Swoole\Process;
use Swoole\Runtime;
Expand Down Expand Up @@ -87,18 +88,8 @@
* Create a Swoole table to store runtime information
*/
$register->set('activeRuntimes', function () {
$table = new Table(1024);

$table->column('id', Table::TYPE_STRING, 256);
$table->column('created', Table::TYPE_FLOAT);
$table->column('updated', Table::TYPE_FLOAT);
$table->column('name', Table::TYPE_STRING, 256);
$table->column('hostname', Table::TYPE_STRING, 256);
$table->column('status', Table::TYPE_STRING, 128);
$table->column('key', Table::TYPE_STRING, 256);
$table->create();

return $table;
$state = new ActiveRuntimes();
return $state;
});

/**
Expand Down Expand Up @@ -256,7 +247,7 @@ function getStorageDevice(string $root): Device
}
}

function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration): void
function removeAllRuntimes(ActiveRuntimes $activeRuntimes, Orchestration $orchestration): void
{
Console::log('Cleaning up containers...');

Expand Down Expand Up @@ -387,7 +378,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
->inject('activeRuntimes')
->inject('response')
->inject('log')
->action(function (string $runtimeId, string $image, string $entrypoint, string $source, string $destination, array $variables, string $runtimeEntrypoint, string $command, int $timeout, bool $remove, int $cpus, int $memory, string $version, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
->action(function (string $runtimeId, string $image, string $entrypoint, string $source, string $destination, array $variables, string $runtimeEntrypoint, string $command, int $timeout, bool $remove, int $cpus, int $memory, string $version, Orchestration $orchestration, ActiveRuntimes $activeRuntimes, Response $response, Log $log) {
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)

Expand Down Expand Up @@ -633,10 +624,10 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
->desc("List currently active runtimes")
->inject('activeRuntimes')
->inject('response')
->action(function (Table $activeRuntimes, Response $response) {
->action(function (ActiveRuntimes $activeRuntimes, Response $response) {
$runtimes = [];

foreach ($activeRuntimes as $runtime) {
foreach ($activeRuntimes->getAll() as $runtime) {
$runtimes[] = $runtime;
}

Expand All @@ -651,7 +642,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
->inject('activeRuntimes')
->inject('response')
->inject('log')
->action(function (string $runtimeId, Table $activeRuntimes, Response $response, Log $log) {
->action(function (string $runtimeId, ActiveRuntimes $activeRuntimes, Response $response, Log $log) {
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)

$log->addTag('runtimeId', $activeRuntimeId);
Expand All @@ -674,7 +665,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
->inject('activeRuntimes')
->inject('response')
->inject('log')
->action(function (string $runtimeId, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
->action(function (string $runtimeId, Orchestration $orchestration, ActiveRuntimes $activeRuntimes, Response $response, Log $log) {
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)

Expand Down Expand Up @@ -714,7 +705,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
->inject('response')
->inject('log')
->action(
function (string $runtimeId, ?string $payload, string $path, string $method, array $headers, int $timeout, string $image, string $source, string $entrypoint, array $variables, int $cpus, int $memory, string $version, string $runtimeEntrypoint, Table $activeRuntimes, Response $response, Log $log) {
function (string $runtimeId, ?string $payload, string $path, string $method, array $headers, int $timeout, string $image, string $source, string $entrypoint, array $variables, int $cpus, int $memory, string $version, string $runtimeEntrypoint, ActiveRuntimes $activeRuntimes, Response $response, Log $log) {
if (empty($payload)) {
$payload = '';
}
Expand Down Expand Up @@ -816,7 +807,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
}

// Update swoole table
$runtime = $activeRuntimes->get($activeRuntimeId) ?? [];
$runtime = $activeRuntimes->get($activeRuntimeId);
$runtime['updated'] = \time();
$activeRuntimes->set($activeRuntimeId, $runtime);

Expand Down
47 changes: 47 additions & 0 deletions src/Executor/ActiveRuntimes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace OpenRuntimes\Executor;

class ActiveRuntimes
{
/**
* @var array<string, mixed> $runtimes
*/
protected array $runtimes = [];

/**
* @param array<mixed> $runtime
*/
public function set(string $id, array $runtime): self
{
$this->runtimes[$id] = $runtime;
return $this;
}

/**
* @return array<mixed>
*/
public function get(string $id): array
{
return $this->runtimes[$id] ?? [];
}

/**
* @return array<string, mixed>
*/
public function getAll(): array
{
return $this->runtimes;
}

public function exists(string $id): bool
{
return \array_key_exists($id, $this->runtimes);
}

public function del(string $id): self
{
unset($this->runtimes[$id]);
return $this;
}
}