Skip to content
Merged
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
83 changes: 30 additions & 53 deletions app/http.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
<?php

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/error.php';
require_once __DIR__ . '/controllers.php';

use OpenRuntimes\Executor\Runner\Docker;
use OpenRuntimes\Executor\Runner\ImagePuller;
use OpenRuntimes\Executor\Runner\Maintenance;
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
use OpenRuntimes\Executor\Runner\Network;
use Swoole\Process;
use Swoole\Runtime;
use Utopia\Console;
use Utopia\Http\Http;
use Utopia\Http\Response;
use Utopia\Http\Adapter\Swoole\Server;
use Utopia\System\System;
use Utopia\Orchestration\Adapter\DockerAPI;
use Utopia\Orchestration\Orchestration;

use function Swoole\Coroutine\run;

// Unlimited memory limit to handle as many coroutines/requests as possible
ini_set('memory_limit', '-1');

$payloadSize = 22 * (1024 * 1024);
$settings = [
'package_max_length' => $payloadSize,
Expand All @@ -37,56 +27,43 @@

Http::setMode((string)System::getEnv('OPR_EXECUTOR_ENV', Http::MODE_TYPE_PRODUCTION));

Http::onStart()
->inject('orchestration')
->inject('network')
->inject('imagePuller')
->inject('maintenance')
->action(function (Orchestration $orchestration, Network $network, ImagePuller $imagePuller, Maintenance $maintenance) {
/* Fetch own container information */
$hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname');
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');

/* Create desired networks if they don't exist */
$network->setup(
explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'),
$selfContainer->getName()
);
Http::setResource('networks', fn (): array => $network->getAvailable());

/* Pull images */
$imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: ''));

/* Start maintenance task */
$maintenance->start(
(int)System::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'),
(int)System::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60')
);

Console::success('Executor is ready.');
});

Http::onRequest()
->inject('response')
->action(function (Response $response) {
$response->addHeader('Server', 'Executor');
});

run(function () use ($settings) {
$orchestration = new Orchestration(new DockerAPI(
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''),
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '')
));
$runtimes = new Runtimes();

/* Fetch own container information */
$hostname = gethostname() ?: throw new \RuntimeException('Could not determine hostname');
$selfContainer = $orchestration->list(['name' => $hostname])[0] ?? throw new \RuntimeException('Own container not found');

/* Create desired networks if they don't exist */
$network = new Network($orchestration);
$network->setup(
explode(',', System::getEnv('OPR_EXECUTOR_NETWORK') ?: 'openruntimes-runtimes'),
$selfContainer->getName()
);

/* Pull images */
$imagePuller = new ImagePuller($orchestration);
$imagePuller->pull(explode(',', System::getEnv('OPR_EXECUTOR_IMAGES') ?: ''));

/* Start maintenance task */
$maintenance = new Maintenance($orchestration, $runtimes);
$maintenance->start(
(int)System::getEnv('OPR_EXECUTOR_MAINTENANCE_INTERVAL', '3600'),
(int)System::getEnv('OPR_EXECUTOR_INACTIVE_THRESHOLD', '60')
);

/* Runner service, used to manage runtimes */
$runner = new Docker($orchestration, $runtimes, $network->getAvailable());
Http::setResource('runner', fn () => $runner);

$server = new Server('0.0.0.0', '80', $settings);
$http = new Http($server, 'UTC');

Process::signal(SIGTERM, function () use ($maintenance, $runner, $network) {
// This doesn't actually work. We need to fix utopia-php/http@0.34.x
$maintenance->stop();
$network->cleanup();
$runner->cleanup();
});

Console::success('Executor is ready.');

$http->start();
});
30 changes: 30 additions & 0 deletions app/init.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
<?php

use OpenRuntimes\Executor\Runner\Docker;
use OpenRuntimes\Executor\Runner\ImagePuller;
use OpenRuntimes\Executor\Runner\Maintenance;
use OpenRuntimes\Executor\Runner\Network;
use OpenRuntimes\Executor\Runner\Repository\Runtimes;
use OpenRuntimes\Executor\Runner\Adapter;
use Utopia\Http\Http;
use Utopia\Orchestration\Adapter\DockerAPI;
use Utopia\Orchestration\Orchestration;
use Utopia\System\System;
use Utopia\Registry\Registry;
use Utopia\Config\Config;

const MAX_LOG_SIZE = 5 * 1024 * 1024;
const MAX_BUILD_LOG_SIZE = 1 * 1000 * 1000;

Config::load('errors', __DIR__ . '/config/errors.php');

$registry = new Registry();

$registry->set('runtimes', fn () => new Runtimes());

Http::setResource('runtimes', fn () => $registry->get('runtimes'));

Http::setResource('orchestration', fn (): Orchestration => new Orchestration(new DockerAPI(
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_USERNAME', ''),
System::getEnv('OPR_EXECUTOR_DOCKER_HUB_PASSWORD', '')
)));

Http::setResource('network', fn (Orchestration $orchestration): Network => new Network($orchestration), ['orchestration']);

Http::setResource('imagePuller', fn (Orchestration $orchestration): ImagePuller => new ImagePuller($orchestration), ['orchestration']);

Http::setResource('maintenance', fn (Orchestration $orchestration, Runtimes $runtimes): Maintenance => new Maintenance($orchestration, $runtimes), ['orchestration', 'runtimes']);

Http::setResource('runner', fn (Orchestration $orchestration, Runtimes $runtimes, array $networks): Adapter => new Docker($orchestration, $runtimes, $networks), ['orchestration', 'runtimes', 'networks']);