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: Contianer autoRestart #90

Merged
merged 6 commits into from
Aug 2, 2024
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
3 changes: 1 addition & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ jobs:

- name: Run Tests
run: |
docker run --rm -v $PWD:/app --network openruntimes-runtimes -w /app phpswoole/swoole:5.1.2-php8.3-alpine sh -c \
"composer install --profile --ignore-platform-reqs && composer test"
docker run --rm -v $PWD:/app -v /var/run/docker.sock:/var/run/docker.sock --network openruntimes-runtimes -w /app phpswoole/swoole:5.1.2-php8.3-alpine sh -c "apk update && apk add docker-cli && composer install --profile --ignore-platform-reqs && composer test"
12 changes: 8 additions & 4 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,12 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
->param('cpus', 1, new Integer(), 'Container CPU.', true)
->param('memory', 512, new Integer(), 'Comtainer RAM memory.', true)
->param('version', 'v4', new WhiteList(['v2', 'v4']), 'Runtime Open Runtime version.', true)
->param('restartPolicy', DockerAPI::RESTART_NO, new WhiteList([DockerAPI::RESTART_NO, DockerAPI::RESTART_ALWAYS, DockerAPI::RESTART_ON_FAILURE, DockerAPI::RESTART_UNLESS_STOPPED], true), 'Define restart policy for the runtime once an exit code is returned. Default value is "no". Possible values are "no", "always", "on-failure", "unless-stopped".', true)
->inject('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, string $restartPolicy, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
$runtimeName = System::getHostname() . '-' . $runtimeId;

$runtimeHostname = \uniqid();
Expand Down Expand Up @@ -536,7 +537,8 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
],
volumes: $volumes,
network: \strval($openruntimes_network) ?: 'executor_runtimes',
workdir: $workdir
workdir: $workdir,
restart: $restartPolicy
);

if (empty($containerId)) {
Expand Down Expand Up @@ -748,12 +750,13 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
->param('version', 'v4', new WhiteList(['v2', 'v4']), 'Runtime Open Runtime version.', true)
->param('runtimeEntrypoint', '', new Text(1024, 0), 'Commands to run when creating a container. Maximum of 100 commands are allowed, each 1024 characters long.', true)
->param('logging', true, new Boolean(true), 'Whether executions will be logged.', true)
->param('restartPolicy', DockerAPI::RESTART_NO, new WhiteList([DockerAPI::RESTART_NO, DockerAPI::RESTART_ALWAYS, DockerAPI::RESTART_ON_FAILURE, DockerAPI::RESTART_UNLESS_STOPPED], true), 'Define restart policy once exit code is returned by command. Default value is "no". Possible values are "no", "always", "on-failure", "unless-stopped".', true)
->inject('activeRuntimes')
->inject('response')
->inject('request')
->inject('log')
->action(
function (string $runtimeId, ?string $payload, string $path, string $method, mixed $headers, int $timeout, string $image, string $source, string $entrypoint, mixed $variables, int $cpus, int $memory, string $version, string $runtimeEntrypoint, bool $logging, Table $activeRuntimes, Response $response, Request $request, Log $log) {
function (string $runtimeId, ?string $payload, string $path, string $method, mixed $headers, int $timeout, string $image, string $source, string $entrypoint, mixed $variables, int $cpus, int $memory, string $version, string $runtimeEntrypoint, bool $logging, string $restartPolicy, Table $activeRuntimes, Response $response, Request $request, Log $log) {
if (empty($payload)) {
$payload = '';
}
Expand Down Expand Up @@ -821,7 +824,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, mix
}

// Prepare request to executor
$sendCreateRuntimeRequest = function () use ($runtimeId, $image, $source, $entrypoint, $variables, $cpus, $memory, $version, $runtimeEntrypoint) {
$sendCreateRuntimeRequest = function () use ($runtimeId, $image, $source, $entrypoint, $variables, $cpus, $memory, $version, $restartPolicy, $runtimeEntrypoint) {
$statusCode = 0;
$errNo = -1;
$executorResponse = '';
Expand All @@ -837,6 +840,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, mix
'cpus' => $cpus,
'memory' => $memory,
'version' => $version,
'restartPolicy' => $restartPolicy,
'runtimeEntrypoint' => $runtimeEntrypoint
]);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"utopia-php/registry": "0.5.*",
"utopia-php/preloader": "0.2.*",
"utopia-php/system": "0.8.*",
"utopia-php/orchestration": "0.12.*",
"utopia-php/orchestration": "0.13.*",
"appwrite/php-runtimes": "0.14.*"
},
"require-dev": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 61 additions & 2 deletions tests/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,65 @@ public function testExecute(array $data): void
$this->assertEquals(200, $response['headers']['status-code']);
}

public function testRestartPolicy(): void
{
$output = '';
Console::execute('cd /app/tests/resources/functions/php-exit && tar --exclude code.tar.gz -czf code.tar.gz .', '', $output);

$command = 'php src/server.php';

/** Build runtime */

$params = [
'runtimeId' => 'test-build-restart-policy',
'source' => '/storage/functions/php-exit/code.tar.gz',
'destination' => '/storage/builds/test-restart-policy',
'entrypoint' => 'index.php',
'image' => 'openruntimes/php:v4-8.1',
'command' => 'tar -zxf /tmp/code.tar.gz -C /mnt/code && helpers/build.sh ""'
];

$response = $this->client->call(Client::METHOD_POST, '/runtimes', [], $params);
$this->assertEquals(201, $response['headers']['status-code']);

$buildPath = $response['body']['path'];

/** Execute function */

$response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec-restart-policy/executions', [], [
'source' => $buildPath,
'entrypoint' => 'index.php',
'image' => 'openruntimes/php:v4-8.1',
'runtimeEntrypoint' => 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $command . '"',
'restartPolicy' => 'always'
]);
$this->assertEquals(500, $response['headers']['status-code']);

\sleep(5);

$response = $this->client->call(Client::METHOD_POST, '/runtimes/test-exec-restart-policy/executions', [], [
'source' => $buildPath,
'entrypoint' => 'index.php',
'image' => 'openruntimes/php:v4-8.1',
'runtimeEntrypoint' => 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $command . '"'
]);
$this->assertEquals(500, $response['headers']['status-code']);

\sleep(5);

/** Ensure restart policy */

$output = [];
\exec('docker logs executor-test-exec-restart-policy', $output);
$output = \implode("\n", $output);
$occurances = \substr_count($output, 'HTTP server successfully started!');
$this->assertEquals(3, $occurances);

/** Delete runtime */
$response = $this->client->call(Client::METHOD_DELETE, '/runtimes/test-exec-restart-policy', [], []);
$this->assertEquals(200, $response['headers']['status-code']);
}

/**
*
* @return array<mixed>
Expand Down Expand Up @@ -692,7 +751,7 @@ public function testCustomRuntimes(string $folder, string $image, string $entryp
'destination' => '/storage/builds/test',
'entrypoint' => $entrypoint,
'image' => $image,
'timeout' => 60,
'timeout' => 120,
'command' => 'tar -zxf /tmp/code.tar.gz -C /mnt/code && helpers/build.sh "' . $buildCommand . '"',
'remove' => true
];
Expand All @@ -712,7 +771,7 @@ public function testCustomRuntimes(string $folder, string $image, string $entryp
'entrypoint' => $entrypoint,
'image' => $image,
'runtimeEntrypoint' => 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "' . $startCommand . '"',
'timeout' => 60,
'timeout' => 120,
'variables' => [
'TEST_VARIABLE' => 'Variable secret'
],
Expand Down
6 changes: 6 additions & 0 deletions tests/resources/functions/php-exit/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return function ($context) {
exit(1);
return $context->res->send('OK');
};
Loading