From 2caf909419a2f729291c5505888a0047306aec5a Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Fri, 17 Jul 2020 00:51:26 +0300 Subject: [PATCH] Added function execution logs --- app/views/console/functions/function.phtml | 42 ++++++++-------- app/workers/functions.php | 57 +++++++++++++++++++--- public/dist/scripts/app-all.js | 2 +- public/dist/scripts/app.js | 2 +- public/scripts/filters.js | 20 ++++++++ 5 files changed, 95 insertions(+), 28 deletions(-) diff --git a/app/views/console/functions/function.phtml b/app/views/console/functions/function.phtml index 86a79e8ca..c61a05283 100644 --- a/app/views/console/functions/function.phtml +++ b/app/views/console/functions/function.phtml @@ -233,35 +233,39 @@ $events = array_keys($this->getParam('events', []));

Logs

-
+
- - - - - + + + + + - +
DateEventClientLocationIPStatusDateExit CodeRuntime
-
  • Settings

    diff --git a/app/workers/functions.php b/app/workers/functions.php index eea657502..6f27a0e2f 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -61,6 +61,8 @@ class FunctionsV1 $projectId = $this->args['projectId']; $functionId = $this->args['functionId']; $functionTag = $this->args['functionTag']; + $executionId = $this->args['executionId']; + $functionTrigger = $this->args['functionTrigger']; $projectDB = new Database(); $projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register)); @@ -83,6 +85,32 @@ class FunctionsV1 throw new Exception('Tag not found', 404); } + Authorization::disable(); + $execution = $projectDB->getDocument($executionId); + + if (empty($execution->getId()) || Database::SYSTEM_COLLECTION_EXECUTIONS != $execution->getCollection()) { + $execution = $projectDB->createDocument([ + '$collection' => Database::SYSTEM_COLLECTION_EXECUTIONS, + '$permissions' => [ + 'read' => [], + 'write' => [], + ], + 'dateCreated' => time(), + 'functionId' => $function->getId(), + 'status' => 'proccesing', // waiting / proccesing / completed / failed + 'exitCode' => 0, + 'stdout' => '', + 'stderr' => '', + 'time' => 0, + ]); + + if (false === $execution) { + throw new Exception('Failed saving execution to DB', 500); + } + } + + Authorization::reset(); + $environment = (isset($environments[$function->getAttribute('env', '')])) ? $environments[$function->getAttribute('env', '')] : null; @@ -91,10 +119,10 @@ class FunctionsV1 throw new Exception('Environment "'.$function->getAttribute('env', '').' is not supported'); } - $vars = array_merge($function->getAttribute('vars', []), - [ + $vars = array_merge($function->getAttribute('vars', []), [ 'APPWRITE_FUNCTION_ID' => $functionId, 'APPWRITE_FUNCTION_TAG' => $functionTag, + 'APPWRITE_FUNCTION_TRIGGER' => $functionTrigger, 'APPWRITE_FUNCTION_ENV_NAME' => $environment['name'], 'APPWRITE_FUNCTION_ENV_VERSION' => $environment['version'], ]); @@ -117,11 +145,11 @@ class FunctionsV1 */ /** - * 1. Get event args - * 2. Unpackage code in the isolated container + * 1. Get event args - DONE + * 2. Unpackage code in the isolated container - DONE * 3. Execute in container with timeout - * + messure execution time - * + pass env vars + * + messure execution time - DONE + * + pass env vars - DONE * + pass one-time api key * 4. Update execution status * 5. Update execution stdout & stderr @@ -183,13 +211,28 @@ class FunctionsV1 $end = \microtime(true); + Authorization::disable(); + + $execution = $projectDB->updateDocument(array_merge($execution->getArrayCopy(), [ + 'status' => ($exitCode === 0) ? 'completed' : 'failed', + 'exitCode' => $exitCode, + 'stdout' => mb_substr($stdout, -2000), // log last 2000 chars output + 'stderr' => mb_substr($stderr, -2000), // log last 2000 chars output + 'time' => ($end - $start), + ])); + + Authorization::reset(); + + if (false === $function) { + throw new Exception('Failed saving execution to DB', 500); + } + /** * Get Usage Stats * -> Network (docker stats --no-stream --format="{{.NetIO}}" appwrite) * -> CPU Time * -> Invoctions (+1) * Report to usage worker - * Save execution status and results */ var_dump('stdout', $stdout); diff --git a/public/dist/scripts/app-all.js b/public/dist/scripts/app-all.js index 9ff48060d..7df33471c 100644 --- a/public/dist/scripts/app-all.js +++ b/public/dist/scripts/app-all.js @@ -2619,7 +2619,7 @@ size;}).add("selectedCollection",function($value,router){return $value===router. (hours?hours+"h ":"")+ (minutes?minutes+"m ":"")+ Number.parseFloat(seconds).toFixed(0)+"s");} -return"< 1s";}).add("markdown",function($value,markdown){return markdown.render($value);}).add("pageCurrent",function($value,env){return Math.ceil(parseInt($value||0)/env.PAGING_LIMIT)+1;}).add("pageTotal",function($value,env){let total=Math.ceil(parseInt($value||0)/env.PAGING_LIMIT);return total?total:1;}).add("humanFileSize",function($value){if(!$value){return 0;} +return"< 1s";}).add("seconds2hum",function($value){var seconds=($value).toFixed(2);var minutes=($value/(60)).toFixed(1);var hours=($value/(60*60)).toFixed(1);var days=($value/(60*60*24)).toFixed(1);if(seconds<60){return seconds+"s";}else if(minutes<60){return minutes+"m";}else if(hours<24){return hours+"h";}else{return days+"d"}}).add("markdown",function($value,markdown){return markdown.render($value);}).add("pageCurrent",function($value,env){return Math.ceil(parseInt($value||0)/env.PAGING_LIMIT)+1;}).add("pageTotal",function($value,env){let total=Math.ceil(parseInt($value||0)/env.PAGING_LIMIT);return total?total:1;}).add("humanFileSize",function($value){if(!$value){return 0;} let thresh=1000;if(Math.abs($value)=thresh&&u'+ units[u]+"");}).add("statsTotal",function($value){if(!$value){return 0;} diff --git a/public/dist/scripts/app.js b/public/dist/scripts/app.js index 765f4b58c..8ae438d4e 100644 --- a/public/dist/scripts/app.js +++ b/public/dist/scripts/app.js @@ -257,7 +257,7 @@ size;}).add("selectedCollection",function($value,router){return $value===router. (hours?hours+"h ":"")+ (minutes?minutes+"m ":"")+ Number.parseFloat(seconds).toFixed(0)+"s");} -return"< 1s";}).add("markdown",function($value,markdown){return markdown.render($value);}).add("pageCurrent",function($value,env){return Math.ceil(parseInt($value||0)/env.PAGING_LIMIT)+1;}).add("pageTotal",function($value,env){let total=Math.ceil(parseInt($value||0)/env.PAGING_LIMIT);return total?total:1;}).add("humanFileSize",function($value){if(!$value){return 0;} +return"< 1s";}).add("seconds2hum",function($value){var seconds=($value).toFixed(2);var minutes=($value/(60)).toFixed(1);var hours=($value/(60*60)).toFixed(1);var days=($value/(60*60*24)).toFixed(1);if(seconds<60){return seconds+"s";}else if(minutes<60){return minutes+"m";}else if(hours<24){return hours+"h";}else{return days+"d"}}).add("markdown",function($value,markdown){return markdown.render($value);}).add("pageCurrent",function($value,env){return Math.ceil(parseInt($value||0)/env.PAGING_LIMIT)+1;}).add("pageTotal",function($value,env){let total=Math.ceil(parseInt($value||0)/env.PAGING_LIMIT);return total?total:1;}).add("humanFileSize",function($value){if(!$value){return 0;} let thresh=1000;if(Math.abs($value)=thresh&&u'+ units[u]+"");}).add("statsTotal",function($value){if(!$value){return 0;} diff --git a/public/scripts/filters.js b/public/scripts/filters.js index 1c6e00fff..eb8c5c930 100644 --- a/public/scripts/filters.js +++ b/public/scripts/filters.js @@ -58,6 +58,26 @@ window.ls.filter return "< 1s"; }) + .add("seconds2hum", function($value) { + + var seconds = ($value).toFixed(2); + + var minutes = ($value / (60)).toFixed(1); + + var hours = ($value / (60 * 60)).toFixed(1); + + var days = ($value / (60 * 60 * 24)).toFixed(1); + + if (seconds < 60) { + return seconds + "s"; + } else if (minutes < 60) { + return minutes + "m"; + } else if (hours < 24) { + return hours + "h"; + } else { + return days + "d" + } + }) .add("markdown", function($value, markdown) { return markdown.render($value); })