Skip to content

Commit

Permalink
Only adds watched kill to history, improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Slivo-fr committed May 21, 2019
1 parent 651e286 commit 346a561
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bin/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
38 changes: 22 additions & 16 deletions src/Killbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -53,7 +53,7 @@ public function run()
unlink($filepath);
}

$this->saveCachedKills();
$this->saveKillHistory();
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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));
}
Expand All @@ -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;

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 346a561

Please sign in to comment.