From b594effb847b86fc6b1dae49d7948c46bba35e58 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Tue, 25 Apr 2023 16:45:03 +0300 Subject: alfa version; uploaded into test server (coder) --- core/cli/cli-cache.php | 65 ++++++++++++++++++++ core/cli/curl-cache.php | 155 ++++++++++++++++++++++++++++++++++++++++++++++++ core/cli/info.md | 23 +++++++ 3 files changed, 243 insertions(+) create mode 100644 core/cli/cli-cache.php create mode 100755 core/cli/curl-cache.php create mode 100644 core/cli/info.md (limited to 'core') diff --git a/core/cli/cli-cache.php b/core/cli/cli-cache.php new file mode 100644 index 0000000..57d8090 --- /dev/null +++ b/core/cli/cli-cache.php @@ -0,0 +1,65 @@ +#!/usr/local/bin/php + $category) { + + echo textColor( textWidth($category['Title'], 72), NORMAL); + app\controllers\cli\CliContoller::cacheCategoryByUrl($category['FullFriendlyUrl']); + } + + # end of script ------------------------------------------------------------ + +} catch(Exception $e) { // Basic error handling + echo 'Caught exception: ', textColor($e->getMessage(), ERROR), "\n"; + die(); +} + + +$dt = number_format( microtime(true) - $t0 , 0) ."sec"; + +// echo ending... +echo textColor("\nOperation Completed {$dt}\n\n", NORMAL); diff --git a/core/cli/curl-cache.php b/core/cli/curl-cache.php new file mode 100755 index 0000000..6ab2e93 --- /dev/null +++ b/core/cli/curl-cache.php @@ -0,0 +1,155 @@ +#!/usr/local/bin/php +success) { + echo "\033[91mError reading " . $call ."\n\033[39m"; + return false; + } + + return $json; +} + +/** multiple curl get calls + * + * @param $array: an array of urls to be called + * + */ +function multiCurl($array) { + + $multiCurl = array(); // array of curl handlers + $result = array(); // data to be returned + $mh = curl_multi_init(); // multi handler + $module = array(); // array of module names / 1:1 to $result array + + foreach ($array as $i => $category) { + $module[$i] = $category->ID; // guid (module name) + $fetchURL = API_ROOT .'url/'. $category->FullFriendlyUrl; + $multiCurl[$i] = curl_init(); + curl_setopt($multiCurl[$i], CURLOPT_URL,$fetchURL); + curl_setopt($multiCurl[$i], CURLOPT_HEADER,0); + curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER,1); + curl_multi_add_handle($mh, $multiCurl[$i]); + } + + $index=null; + + do { + curl_multi_exec($mh,$index); + } while($index > 0); + + foreach($multiCurl as $k => $ch) { + $result[$k] = curl_multi_getcontent($ch); + curl_multi_remove_handle($mh, $ch); + } + curl_multi_close($mh); // close multi-curl +} + +// force printing string with specified length +function forceLen($str, $len = 72) { + $space = " "; + for($i = 0 ; $i<$len ; $i++) $space .= "."; + return mb_substr($str.$space, 0, $len-1) ." "; +} + + +/** main procedure + * ----------------------------------------------------------------------------- + */ + +echo "\033[39mReading Tree of product categories...\n"; +$tree = singleCurl('tree'); // ask for categories tree + +if ($tree !==false) echo "\033[32mCategories tree is cached\033[39m\n\n"; + +$categories = singleCurl('category'); + +// request every category to create cache if not exist +// send requests in small batches to avoid enormous server-stress +echo "\033[39mRequesting re-Cache for every category\033[39m\n"; +echo "\033[33mPlease Wait...\n\n"; + +$buffer = []; // buffer array +$counter = 0; // counter to track items in buffer +foreach($categories->result as $category) { + + echo "\033[39m". forceLen($category->Title) ."\033[33m"."Sent.\n\033[39m"; + + $counter++; + + // try several batch lengths to establish the ideal one that produces + // results in a smooth quite fase rthm, without stressing the server; + // so that visitors can navigate the production site white the script + // is running; + // for my testing this number is 3-5. I set the cou 3 items give fast results without stressing the server + // so "if ($counter > 2) { ... " is the smoothest option + if ($counter > 2) { + multiCurl($buffer); + $counter = 0; + $buffer = []; + } + + $buffer[] = $category; + +} + +multiCurl($buffer); // run once more for the last non-filled buffer + +$dt = number_format( microtime(true) - $t0 , 1) ."sec"; + +// echo ending... +echo "\n\033[32mOperation Completed \033[39m({$dt})\n\n"; +echo "\033[39m"; // back to default color; + + +// check +// http://www.idein.it/joomla/14-docker-php-apache-with-crontab +?> \ No newline at end of file diff --git a/core/cli/info.md b/core/cli/info.md new file mode 100644 index 0000000..39f8c11 --- /dev/null +++ b/core/cli/info.md @@ -0,0 +1,23 @@ +# cli + +This section is used to setup an anom micro-framework for the cli-interface. +The cli interface is commonly used to support and optimize the whole app. + +A cli script may run manualy, for example using... + + php cli-cache.php + +although... + +it is recommended to run periodically using the cron service: + + 15 * * * * php path/to/cli-cache.php + +or ... + +when a docker instanse of the project fires up; for example including +the following line of code inside the docker/bin/docker-entrypoint.sh +(runs the script after 5 minutes delay to make sure that the instance +is up and running) + + at -f /var/www/core/cli/refresh-cache.php -t now +5 minutes -- cgit v1.2.3