From 0e7008e72e17de9020aa8d0bf93c660b8097eb49 Mon Sep 17 00:00:00 2001 From: Mark Friedrich Date: Thu, 29 Nov 2018 21:21:08 +0100 Subject: [PATCH] - prevent logging SSO errors into error.log during daily EVE server downtime +10min, closed #719 --- app/environment.ini | 2 + app/main/controller/setup.php | 1 + app/main/exception/dateexception.php | 16 ++++++++ app/main/lib/DateRange.php | 55 ++++++++++++++++++++++++++++ app/main/lib/config.php | 37 +++++++++++++++++-- app/main/lib/web.php | 25 ++++++++++--- 6 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 app/main/exception/dateexception.php create mode 100644 app/main/lib/DateRange.php diff --git a/app/environment.ini b/app/environment.ini index d963ea7f..1f4be57a 100644 --- a/app/environment.ini +++ b/app/environment.ini @@ -30,6 +30,7 @@ DB_UNIVERSE_PASS = CCP_SSO_URL = https://sisilogin.testeveonline.com CCP_SSO_CLIENT_ID = CCP_SSO_SECRET_KEY = +CCP_SSO_DOWNTIME = 11:00 ; CCP ESI API CCP_ESI_URL = https://esi.tech.ccp.is @@ -82,6 +83,7 @@ DB_CCP_PASS = CCP_SSO_URL = https://login.eveonline.com CCP_SSO_CLIENT_ID = CCP_SSO_SECRET_KEY = +CCP_SSO_DOWNTIME = 11:00 ; CCP ESI API CCP_ESI_URL = https://esi.tech.ccp.is diff --git a/app/main/controller/setup.php b/app/main/controller/setup.php index ce262583..dff7dfe0 100644 --- a/app/main/controller/setup.php +++ b/app/main/controller/setup.php @@ -39,6 +39,7 @@ class Setup extends Controller { 'CCP_SSO_URL', 'CCP_SSO_CLIENT_ID', 'CCP_SSO_SECRET_KEY', + 'CCP_SSO_DOWNTIME', 'CCP_ESI_URL', 'CCP_ESI_DATASOURCE', 'SMTP_HOST', diff --git a/app/main/exception/dateexception.php b/app/main/exception/dateexception.php new file mode 100644 index 00000000..4b578fa7 --- /dev/null +++ b/app/main/exception/dateexception.php @@ -0,0 +1,16 @@ + 500 // invalid DateRange + ]; +} \ No newline at end of file diff --git a/app/main/lib/DateRange.php b/app/main/lib/DateRange.php new file mode 100644 index 00000000..3327ebb6 --- /dev/null +++ b/app/main/lib/DateRange.php @@ -0,0 +1,55 @@ +from = $from; + $this->to = $to; + } else { + $this->from = $to; + $this->to = $from; + } + } + } + + /** + * check if DateTime $dateCheck is within this range + * @param \DateTime $dateCheck + * @return bool + */ + public function inRange(\DateTime $dateCheck) : bool { + return $dateCheck >= $this->from && $dateCheck <= $this->to; + } +} \ No newline at end of file diff --git a/app/main/lib/config.php b/app/main/lib/config.php index 3a10c126..75c4e3c2 100644 --- a/app/main/lib/config.php +++ b/app/main/lib/config.php @@ -373,7 +373,6 @@ class Config extends \Prefab { ){ $uri = 'tcp://' . $ip . ':' . $port; } - return $uri; } @@ -391,7 +390,6 @@ class Config extends \Prefab { }catch (Exception\ConfigException $e){ LogController::getLogger('ERROR')->write($e->getMessage()); } - return $data; } @@ -405,8 +403,41 @@ class Config extends \Prefab { if(empty($status = @constant('Base::HTTP_' . $code))){ $status = @constant('self::HTTP_' . $code); } - return $status; } + /** + * check if a given DateTime() is within downTime range: downtime + 10m + * -> can be used for prevent logging errors during downTime + * @param \DateTime|null $dateCheck + * @return bool + * @throws Exception\DateException + * @throws \Exception + */ + static function inDownTimeRange(\DateTime $dateCheck = null) : bool { + // default daily downtime 00:00am + $downTimeParts = [0, 0]; + if( !empty($downTime = (string)self::getEnvironmentData('CCP_SSO_DOWNTIME')) ){ + $parts = array_map('intval', explode(':', $downTime)); + if(count($parts) === 2){ + // well formatted DOWNTIME found in config files + $downTimeParts = $parts; + } + } + + // downTime Range is 10m + $downtimeInterval = new \DateInterval('PT10M'); + $timezone = \Base::instance()->get('getTimeZone')(); + + // if set -> use current time + $dateCheck = is_null($dateCheck) ? new \DateTime('now', $timezone) : $dateCheck; + $dateDowntimeStart = new \DateTime('now', $timezone); + $dateDowntimeStart->setTime($downTimeParts[0],$downTimeParts[1]); + $dateDowntimeEnd = clone $dateDowntimeStart; + $dateDowntimeEnd->add($downtimeInterval); + + $dateRange = new DateRange($dateDowntimeStart, $dateDowntimeEnd); + return $dateRange->inRange($dateCheck); + } + } \ No newline at end of file diff --git a/app/main/lib/web.php b/app/main/lib/web.php index bf1ff510..2a665c1d 100644 --- a/app/main/lib/web.php +++ b/app/main/lib/web.php @@ -100,8 +100,9 @@ class Web extends \Web { * @param array $additionalOptions * @param int $retryCount request counter for failed call * @return array|FALSE|mixed + * @throws \Exception\DateException */ - public function request($url,array $options = null, $additionalOptions = [], $retryCount = 0 ) { + public function request($url,array $options = null, $additionalOptions = [], $retryCount = 0){ $f3 = \Base::instance(); if( !$f3->exists( $hash = $this->getCacheKey($url, $options) ) ){ @@ -134,7 +135,11 @@ class Web extends \Web { $url, json_decode($result['body']) ); - LogController::getLogger('ERROR')->write($errorMsg); + + // if request not within downTime time range -> log error + if( !Config::inDownTimeRange() ){ + LogController::getLogger('ERROR')->write($errorMsg); + } break; case 500: case 501: @@ -150,7 +155,11 @@ class Web extends \Web { $url, json_decode($result['body']) ); - LogController::getLogger('ERROR')->write($errorMsg); + + // if request not within downTime time range -> log error + if( !Config::inDownTimeRange() ){ + LogController::getLogger('ERROR')->write($errorMsg); + } // trigger error if($additionalOptions['suppressHTTPErrors'] !== true){ @@ -173,8 +182,10 @@ class Web extends \Web { json_decode($result['body']) ); - // log error - LogController::getLogger('ERROR')->write($errorMsg); + // if request not within downTime time range -> log error + if( !Config::inDownTimeRange() ){ + LogController::getLogger('ERROR')->write($errorMsg); + } if($additionalOptions['suppressHTTPErrors'] !== true){ $f3->error(504, $errorMsg); @@ -189,7 +200,9 @@ class Web extends \Web { $url ); - LogController::getLogger('ERROR')->write($errorMsg); + if( !Config::inDownTimeRange() ){ + LogController::getLogger('ERROR')->write($errorMsg); + } break; }