From f9e1fee619054ba1150ca1bff623a41dedfc5092 Mon Sep 17 00:00:00 2001 From: Mark Friedrich Date: Sun, 17 Jun 2018 12:25:38 +0200 Subject: [PATCH] - set `max_execution_time` for CLI Cronjob script, closed #636 --- app/main/cron/abstractcron.php | 27 +++++++++++++++++++++++++++ app/main/cron/cache.php | 3 ++- app/main/cron/ccpsystemsupdate.php | 4 ++-- app/main/cron/characterupdate.php | 8 +++++--- app/main/cron/mapupdate.php | 7 ++++++- app/main/cron/statisticsupdate.php | 4 +++- 6 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 app/main/cron/abstractcron.php diff --git a/app/main/cron/abstractcron.php b/app/main/cron/abstractcron.php new file mode 100644 index 00000000..04b4076d --- /dev/null +++ b/app/main/cron/abstractcron.php @@ -0,0 +1,27 @@ + should be less then execution period + const DEFAULT_MAX_EXECUTION_TIME = 50; + + /** + * set max execution time for cronjobs + * -> Default CLI execution time is "0" -> infinite! + * php.ini settings are ignored! http://php.net/manual/en/info.configuration.php#ini.max-execution-time + * @param int $time + */ + protected function setMaxExecutionTime(int $time = self::DEFAULT_MAX_EXECUTION_TIME){ + ini_set('max_execution_time', $time); + } + +} \ No newline at end of file diff --git a/app/main/cron/cache.php b/app/main/cron/cache.php index 63832068..2afbe1f8 100644 --- a/app/main/cron/cache.php +++ b/app/main/cron/cache.php @@ -10,7 +10,7 @@ namespace cron; use data\filesystem\Search; -class Cache { +class Cache extends AbstractCron { const LOG_TEXT = '%s [%\'_10s] files, size [%\'_10s] byte, not writable [%\'_10s] files, errors [%\'_10s], exec (%.3Fs)'; @@ -34,6 +34,7 @@ class Cache { * @param \Base $f3 */ function deleteExpiredData(\Base $f3){ + $this->setMaxExecutionTime(); $time_start = microtime(true); // cache dir (dir is recursively searched...) diff --git a/app/main/cron/ccpsystemsupdate.php b/app/main/cron/ccpsystemsupdate.php index 28a6c692..51e3c102 100644 --- a/app/main/cron/ccpsystemsupdate.php +++ b/app/main/cron/ccpsystemsupdate.php @@ -10,7 +10,7 @@ namespace Cron; use Controller; use DB; -class CcpSystemsUpdate { +class CcpSystemsUpdate extends AbstractCron { const LOG_TEXT = '%s prepare table (%.3F s), jump (%.3F s), kill (%.3F s), update all (%.3F s)'; @@ -35,7 +35,6 @@ class CcpSystemsUpdate { * @return array */ private function prepareSystemLogTables(){ - // get information for all systems from CCP DB $systemController = new Controller\Api\System(); $systemsData = $systemController->getSystems(); @@ -72,6 +71,7 @@ class CcpSystemsUpdate { * @param \Base $f3 */ function importSystemData($f3){ + $this->setMaxExecutionTime(); // prepare system jump log table ------------------------------------------------------------------------------ $time_start = microtime(true); diff --git a/app/main/cron/characterupdate.php b/app/main/cron/characterupdate.php index eb795ddb..fc6b81b4 100644 --- a/app/main/cron/characterupdate.php +++ b/app/main/cron/characterupdate.php @@ -11,7 +11,7 @@ use DB; use Model; -class CharacterUpdate { +class CharacterUpdate extends AbstractCron { /** * default character_log time until a log entry get re-checked by cronjob @@ -41,6 +41,7 @@ class CharacterUpdate { * @throws \Exception */ function deleteLogData(\Base $f3){ + $this->setMaxExecutionTime(); DB\Database::instance()->getDB('PF'); $logInactiveTime = $this->getCharacterLogInactiveTime($f3); @@ -84,6 +85,7 @@ class CharacterUpdate { * @throws \Exception */ function cleanUpCharacterData(\Base $f3){ + $this->setMaxExecutionTime(); DB\Database::instance()->getDB('PF'); /** @@ -96,7 +98,6 @@ class CharacterUpdate { ':active' => 1 ]); - if(is_object($characters)){ foreach($characters as $character){ /** @@ -115,7 +116,8 @@ class CharacterUpdate { * @param \Base $f3 * @throws \Exception */ - function deleteAuthenticationData($f3){ + function deleteAuthenticationData(\Base $f3){ + $this->setMaxExecutionTime(); DB\Database::instance()->getDB('PF'); /** diff --git a/app/main/cron/mapupdate.php b/app/main/cron/mapupdate.php index 99a61314..313816f8 100644 --- a/app/main/cron/mapupdate.php +++ b/app/main/cron/mapupdate.php @@ -11,7 +11,7 @@ use DB; use lib\Config; use Model; -class MapUpdate { +class MapUpdate extends AbstractCron { const LOG_TEXT_MAPS = '%s (%d maps)'; @@ -25,6 +25,7 @@ class MapUpdate { * @throws \Exception\PathfinderException */ function deactivateMapData(\Base $f3){ + $this->setMaxExecutionTime(); $privateMapLifetime = (int)Config::getMapsDefaultConfig('private.lifetime'); if($privateMapLifetime > 0){ @@ -49,6 +50,7 @@ class MapUpdate { * @throws \Exception */ function deleteMapData(\Base $f3){ + $this->setMaxExecutionTime(); $pfDB = DB\Database::instance()->getDB('PF'); $deletedMapsCount = 0; @@ -87,6 +89,7 @@ class MapUpdate { * @throws \Exception */ function deleteEolConnections(\Base $f3){ + $this->setMaxExecutionTime(); $eolExpire = (int)$f3->get('PATHFINDER.CACHE.EXPIRE_CONNECTIONS_EOL'); if($eolExpire > 0){ @@ -131,6 +134,7 @@ class MapUpdate { * @throws \Exception */ function deleteExpiredConnections(\Base $f3){ + $this->setMaxExecutionTime(); $whExpire = (int)$f3->get('PATHFINDER.CACHE.EXPIRE_CONNECTIONS_WH'); if($whExpire > 0){ @@ -176,6 +180,7 @@ class MapUpdate { * @param \Base $f3 */ function deleteSignatures(\Base $f3){ + $this->setMaxExecutionTime(); $signatureExpire = (int)$f3->get('PATHFINDER.CACHE.EXPIRE_SIGNATURES'); if($signatureExpire > 0){ diff --git a/app/main/cron/statisticsupdate.php b/app/main/cron/statisticsupdate.php index 26636709..ac9f78ca 100644 --- a/app/main/cron/statisticsupdate.php +++ b/app/main/cron/statisticsupdate.php @@ -9,7 +9,7 @@ namespace cron; use DB; -class StatisticsUpdate { +class StatisticsUpdate extends AbstractCron { const LOG_TEXT_STATISTICS = '%s (%d rows)'; @@ -20,6 +20,8 @@ class StatisticsUpdate { * @param \Base $f3 */ function deleteStatisticsData(\Base $f3){ + $this->setMaxExecutionTime(); + $currentYear = (int)date('o'); $currentWeek = (int)date('W'); $expiredYear = $currentYear - 1;