From 38ca8dc6fca249fcfad9687d7f85c538b31b5875 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 19 Feb 2022 04:15:03 +0400 Subject: [PATCH] feat: update php test code --- app/executor.php | 60 ++++++++------------- src/Executor/Executor.php | 2 +- tests/resources/functions/php-fn.tar.gz | Bin 351 -> 415 bytes tests/resources/functions/php-fn/index.php | 1 + tests/resources/functions/php.tar.gz | Bin 386 -> 446 bytes tests/resources/functions/php/index.php | 1 + 6 files changed, 26 insertions(+), 38 deletions(-) diff --git a/app/executor.php b/app/executor.php index ad5728a7f..873d50e7b 100644 --- a/app/executor.php +++ b/app/executor.php @@ -129,7 +129,7 @@ function logError(Throwable $error, string $action, Utopia\Route $route = null) App::post('/v1/runtimes') ->desc("Create a new runtime server") - ->param('runtimeId', '', new Text(62), 'Unique runtime ID.') + ->param('runtimeId', '', new Text(64), 'Unique runtime ID.') ->param('source', '', new Text(0), 'Path to source files.') ->param('destination', '', new Text(0), 'Destination folder to store build files into.', true) ->param('vars', [], new Assoc(), 'Environment Variables required for the build') @@ -145,9 +145,7 @@ App::post('/v1/runtimes') ->inject('response') ->action(function (string $runtimeId, string $source, string $destination, array $vars, array $commands, string $runtime, string $network, string $baseImage, string $entrypoint, bool $remove, string $workdir, $orchestrationPool, $activeRuntimes, Response $response) { - $containerName = 'r-' . $runtimeId; - - if ($activeRuntimes->exists($containerName)) { + if ($activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime already exists.', 409); } @@ -159,7 +157,7 @@ App::post('/v1/runtimes') $endTime = 0; try { - Console::info('Building container : ' . $containerName); + Console::info('Building container : ' . $runtimeId); /** * Temporary file paths in the executor */ @@ -201,8 +199,8 @@ App::post('/v1/runtimes') $containerId = $orchestration->run( image: $baseImage, - name: $containerName, - hostname: $containerName, + name: $runtimeId, + hostname: $runtimeId, vars: $vars, labels: [ 'openruntimes-id' => $runtimeId, @@ -222,7 +220,7 @@ App::post('/v1/runtimes') } if (!empty($network)) { - $orchestration->networkConnect($containerName, $network); + $orchestration->networkConnect($runtimeId, $network); } /** @@ -230,7 +228,7 @@ App::post('/v1/runtimes') */ if (!empty($commands)) { $status = $orchestration->execute( - name: $containerName, + name: $runtimeId, command: $commands, stdout: $stdout, stderr: $stderr, @@ -242,10 +240,6 @@ App::post('/v1/runtimes') } } - - var_dump($destination); - var_dump($tmpBuild); - /** * Move built code to expected build directory */ @@ -257,7 +251,6 @@ App::post('/v1/runtimes') $device = new Local($destination); $outputPath = $device->getPath(\uniqid() . '.' . \pathinfo('code.tar.gz', PATHINFO_EXTENSION)); - var_dump($outputPath); $buffer = $device->read($tmpBuild); if(!$device->write($outputPath, $buffer)) { @@ -282,9 +275,9 @@ App::post('/v1/runtimes') ]); if (!$remove) { - $activeRuntimes->set($containerName, [ + $activeRuntimes->set($runtimeId, [ 'id' => $containerId, - 'name' => $containerName, + 'name' => $runtimeId, 'created' => $startTime, 'updated' => $endTime, 'status' => 'Up ' . \round($endTime - $startTime, 2) . 's', @@ -329,18 +322,16 @@ App::get('/v1/runtimes') App::get('/v1/runtimes/:runtimeId') ->desc("Get a runtime by its ID") - ->param('runtimeId', '', new Text(62), 'Runtime unique ID.') + ->param('runtimeId', '', new Text(64), 'Runtime unique ID.') ->inject('activeRuntimes') ->inject('response') ->action(function ($runtimeId, $activeRuntimes, Response $response) { - $container = 'r-' . $runtimeId; - - if(!$activeRuntimes->exists($container)) { + if(!$activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime not found', 404); } - $runtime = $activeRuntimes->get($container); + $runtime = $activeRuntimes->get($runtimeId); $response ->setStatusCode(Response::STATUS_CODE_OK) @@ -349,25 +340,23 @@ App::get('/v1/runtimes/:runtimeId') App::delete('/v1/runtimes/:runtimeId') ->desc('Delete a runtime') - ->param('runtimeId', '', new Text(62), 'Runtime unique ID.', false) + ->param('runtimeId', '', new Text(64), 'Runtime unique ID.', false) ->inject('orchestrationPool') ->inject('activeRuntimes') ->inject('response') ->action(function (string $runtimeId, $orchestrationPool, $activeRuntimes, Response $response) { - $container = 'r-' . $runtimeId; - - if(!$activeRuntimes->exists($container)) { + if(!$activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime not found', 404); } - Console::info('Deleting runtime: ' . $container); + Console::info('Deleting runtime: ' . $runtimeId); try { $orchestration = $orchestrationPool->get(); - $orchestration->remove($container, true); - $activeRuntimes->del($container); - Console::success('Removed runtime container: ' . $container); + $orchestration->remove($runtimeId, true); + $activeRuntimes->del($runtimeId); + Console::success('Removed runtime container: ' . $runtimeId); } finally { $orchestrationPool->put($orchestration); } @@ -391,7 +380,7 @@ App::delete('/v1/runtimes/:runtimeId') App::post('/v1/execution') ->desc('Create an execution') - ->param('runtimeId', '', new Text(62), 'The runtimeID to execute') + ->param('runtimeId', '', new Text(64), 'The runtimeID to execute') ->param('path', '', new Text(0), 'Path containing the built files.', false) ->param('vars', [], new Assoc(), 'Environment variables required for the build', false) ->param('data', '', new Text(8192), 'Data to be forwarded to the function, this is user specified.', true) @@ -404,13 +393,11 @@ App::post('/v1/execution') ->action( function (string $runtimeId, string $path, array $vars, string $data, string $runtime, string $entrypoint, $timeout, string $baseImage, $activeRuntimes, Response $response) { - $container = 'r-' . $runtimeId; - - if (!$activeRuntimes->exists($container)) { + if (!$activeRuntimes->exists($runtimeId)) { throw new Exception('Runtime not found. Please create the runtime.', 404); } - $runtime = $activeRuntimes->get($container); + $runtime = $activeRuntimes->get($runtimeId); $secret = $runtime['key']; if (empty($secret)) { throw new Exception('Runtime secret not found. Please re-create the runtime.', 500); @@ -434,7 +421,7 @@ App::post('/v1/execution') 'payload' => $data, 'timeout' => $timeout ?? (int) App::getEnv('_APP_FUNCTIONS_TIMEOUT', 900) ]); - \curl_setopt($ch, CURLOPT_URL, "http://" . $container . ":3000/"); + \curl_setopt($ch, CURLOPT_URL, "http://" . $runtimeId . ":3000/"); \curl_setopt($ch, CURLOPT_POST, true); \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -510,7 +497,7 @@ App::post('/v1/execution') /** Update swoole table */ $runtime['updated'] = \time(); - $activeRuntimes->set($container, $runtime); + $activeRuntimes->set($runtimeId, $runtime); $response ->setStatusCode(Response::STATUS_CODE_OK) @@ -615,7 +602,6 @@ $http->on('start', function ($http) { try { $orchestration = $orchestrationPool->get(); $orphans = $orchestration->list(['label' => 'openruntimes-type=runtime']); - } catch (\Throwable $th) { } finally { $orchestrationPool->put($orchestration); } diff --git a/src/Executor/Executor.php b/src/Executor/Executor.php index 5ee1499bd..e68e67c83 100644 --- a/src/Executor/Executor.php +++ b/src/Executor/Executor.php @@ -129,7 +129,7 @@ class Executor $status = $response['headers']['status-code']; if ($status >= 400) { - for ($attempts = 0; $attempts < 10; $attempts++) { + for ($attempts = 0; $attempts < 5; $attempts++) { switch ($status) { case 404: $response = $this->createRuntime( diff --git a/tests/resources/functions/php-fn.tar.gz b/tests/resources/functions/php-fn.tar.gz index 7d5a0c5f649384044e9025c17e210945e9a6d235..e4bdd8bae6e1b708d8075fed3228985b6d227dc6 100644 GIT binary patch literal 415 zcmV;Q0bu?giwFSOH4tI|1MQT{Zh}A*hFS9zvuJ`%ybMAQCjAvUpeT#%+I|K>38pKlK5Os3vo3KxX1Da+sxl_WwzO5#y~mjoz5QD}-h zfV>Drg#mK$r7=ttgiD{4;%xkF5Wek}j`wuVM{MhL4(IqES^OiaH@5Byzt;DgePMf$ zekhXk?feB^5wiJCr+u%4{u{} zs$tAb%|iCwMDJSKNVl~f2il!%pIV3T=I4EjY92^ehlmb!aOT5$$haRp43TcpAy54I zV9V6{ePq&6Q*%DjoamM|L>DyRO9mZ$ip()tU&c?9^0z=!>NY1<3Y^_jE%gFs;{V}H zP$MZ|lQA->&wgsoM-FC`U5t(SD8n2LP}ia^@Wg)vFUVc+(PDr5#w%5-RH;(2J^_4; J{#gJH005na$X);d literal 351 zcmV-l0igaLiwFQz$M0bP1MQSgYr-%XhduXG$XWMDpjidHlL52$tnO0007)*sG diff --git a/tests/resources/functions/php-fn/index.php b/tests/resources/functions/php-fn/index.php index 17cf700d3..8b729ed40 100644 --- a/tests/resources/functions/php-fn/index.php +++ b/tests/resources/functions/php-fn/index.php @@ -1,6 +1,7 @@ json([ 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'], diff --git a/tests/resources/functions/php.tar.gz b/tests/resources/functions/php.tar.gz index 71404d2e7f7092f55e285859a7f30b5e748c4e7d..a7d6026c7e3852ecabe474822258e3793f30d3a4 100644 GIT binary patch literal 446 zcmV;v0YUyBiwFR}H4tI|1MQUCYQjJehI5^#7^I+rhgD;QS}kH$3DiX5YAIHNH7hDK zu{lUf>D%&PhT3xd5$}_St*m5J$Kx<#du;p9r7{TbQ;0ge8(>S6Xo)A)1a@p z27C1mBt7uPW-v66h)_I<{sq5J$Og(er&MgyC zS2lg|sN-gUJvmFubZ8phd0^TuK@A=kj%Dfwomd9lr3{nQTqQ>taBYvKt3l3&zhhOd_A!Un7&u*O zkj4?r(7zAvD{@nnuugCNRLIJ2njP%(*`o;QF)TTF}}Im(yf- oj`iH5_KMnsE&7*lfFH4G`Ka=LeJ5OW(M9K>Z*W6N2>=WL0Q(Qsr2qf` literal 386 zcmV-|0e$`-iwFQV$M0bP1MQUGYQiuS#=Y)S9Auz^&Qz}__n*YL%bwki%>sTVQ^{ccW^eEH?%lpLE`baTCWUga()giuqGAdIbMBZ{aN zk3)$K34thyt$ItUHBlXqDAkad19ZWh4hlSf?($S@huI6;qb@z|=p?VFX8K$<+j=}?g~8K&UjgZ?zY8cByd@+X6d zq4xXONJrh9laa<)BkBO3QGg#i(!nR#81eCW{QainBk&}3nPY1ToTkebOqqVuH(DMkZ~zJb03;{72mk;8 diff --git a/tests/resources/functions/php/index.php b/tests/resources/functions/php/index.php index f70ef9674..352e3c109 100644 --- a/tests/resources/functions/php/index.php +++ b/tests/resources/functions/php/index.php @@ -1,6 +1,7 @@ json([ 'APPWRITE_FUNCTION_ID' => $request->env['APPWRITE_FUNCTION_ID'], 'APPWRITE_FUNCTION_NAME' => $request->env['APPWRITE_FUNCTION_NAME'],