From 346a561009c604d8f02bbb151847bf051df3c393 Mon Sep 17 00:00:00 2001 From: Slivo-fr Date: Tue, 21 May 2019 09:58:47 +0200 Subject: [PATCH] Only adds watched kill to history, improve logs --- Settings.php | 2 +- bin/cron.php | 2 +- src/Killbot.php | 38 ++++++++++++++++++++++---------------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Settings.php b/Settings.php index 300d6fe..937aa4e 100644 --- a/Settings.php +++ b/Settings.php @@ -33,7 +33,7 @@ class Settings // 4 minutes default max run time public static $MAX_RUN_TIME = 4 * 60; - public static $CACHED_KILL_NUMBER = 50; + public static $KILL_HISTORY_MAX_LENGTH = 50; /******************************************************************************************************************* * Mail settings diff --git a/bin/cron.php b/bin/cron.php index 311b30b..a4add7a 100644 --- a/bin/cron.php +++ b/bin/cron.php @@ -15,7 +15,7 @@ function ($code, $message, $file, $line) { set_exception_handler( function ($exception) { - Logger::log($exception->getMessage()); + Logger::log($exception); if (Settings::$ENV === 'PROD' && Settings::$SEND_MAIL) { AlertHandler::sendAlertMail($exception->getMessage(), $exception->getFile(), $exception->getLine()); } elseif (Settings::$ENV === 'DEV') { diff --git a/src/Killbot.php b/src/Killbot.php index 2708d9b..2cb5924 100644 --- a/src/Killbot.php +++ b/src/Killbot.php @@ -18,9 +18,9 @@ class Killbot * Array of lasts processed kills * @var array */ - protected $lastKills = []; + protected $killHistory = []; - const CACHED_KILL_FILENAME = 'cachedKills.json'; + const KILL_HISTORY_FILENAME = 'killHistory.json'; /** * Runs the bot @@ -30,7 +30,7 @@ public function run() { $isFeedEmpty = false; $timeout = time() + Settings::$MAX_RUN_TIME; - $this->loadCachedKills(); + $this->loadKillHistory(); // Processing queue while (!$isFeedEmpty && time() < $timeout) { @@ -53,7 +53,7 @@ public function run() unlink($filepath); } - $this->saveCachedKills(); + $this->saveKillHistory(); } /** @@ -83,9 +83,10 @@ private function processKill($data = null) try { $killId = $data->{'package'}->{'killID'}; + $isKillWatched = false; // RedisQ isn't meant to provide duplicate free feed. - if (in_array($killId, $this->lastKills)) { + if (in_array($killId, $this->killHistory)) { Logger::log('Duplicate kill processing aborted ' . $killId, Logger::INFO); return true; } @@ -98,6 +99,8 @@ private function processKill($data = null) // Only process kill that match settings entities if ($this->isWatchedKill($data, $watchedEntities)) { + $isKillWatched = true; + if (Settings::$DEBUG) { Logger::storeKillJson($killId, json_encode($data)); } @@ -108,7 +111,10 @@ private function processKill($data = null) } } - array_unshift($this->lastKills, $killId); + // Doesn't store non-watched kills + if ($isKillWatched) { + array_unshift($this->killHistory, $killId); + } return true; @@ -131,7 +137,7 @@ private function processKill($data = null) if (Settings::$ENV === 'DEV') { throw $exception; } else { - Logger::log($exception->getMessage()); + Logger::log($exception); return false; } } @@ -375,26 +381,26 @@ protected function getPendingFiles(): array } /** - * Writes cached kill to file + * Writes last kills to file * @throws Exception */ - protected function saveCachedKills() + protected function saveKillHistory() { - $this->lastKills = array_slice($this->lastKills, 0, Settings::$CACHED_KILL_NUMBER); - Utils::writeFile(json_encode($this->lastKills), Utils::getLogPath(), self::CACHED_KILL_FILENAME, 'w+'); + $this->killHistory = array_slice($this->killHistory, 0, Settings::$KILL_HISTORY_MAX_LENGTH); + Utils::writeFile(json_encode($this->killHistory), Utils::getLogPath(), self::KILL_HISTORY_FILENAME, 'w+'); } /** - * Loads cached kill + * Loads last kills * @throws Exception */ - protected function loadCachedKills() + protected function loadKillHistory() { - $file = Utils::getLogPath().self::CACHED_KILL_FILENAME; + $file = Utils::getLogPath().self::KILL_HISTORY_FILENAME; if (file_exists($file)) { - $json = file_get_contents(Utils::getLogPath().self::CACHED_KILL_FILENAME); - $this->lastKills = json_decode($json, true); + $json = file_get_contents(Utils::getLogPath().self::KILL_HISTORY_FILENAME); + $this->killHistory = json_decode($json, true); } } }