From d09ab4ec1c79886d42782f3afba05ca337741334 Mon Sep 17 00:00:00 2001 From: Altamash Shaikh Date: Mon, 21 Oct 2024 08:12:54 +0530 Subject: [PATCH] Fixes PHPCS test --- Commands/LockStatus.php | 2 +- Commands/Monitor.php | 88 +++++++++++-------- Commands/PrintQueuedRequests.php | 3 +- Commands/Process.php | 23 +++-- Commands/Test.php | 19 ++-- Configuration.php | 12 +-- Queue.php | 6 +- Queue/Backend.php | 2 +- Queue/Backend/MySQL.php | 29 +++--- Queue/Backend/Redis.php | 9 +- Queue/Backend/RedisCluster.php | 39 ++++---- Queue/Backend/Sentinel.php | 4 +- Queue/Factory.php | 9 +- Queue/Lock.php | 5 +- Queue/LockExpiredException.php | 4 +- Queue/Manager.php | 10 +-- Queue/Processor.php | 4 +- Queue/Processor/Handler.php | 2 +- QueuedTracking.php | 3 +- Settings/NumWorkers.php | 2 + SystemCheck.php | 3 +- SystemSettings.php | 13 +-- Tasks.php | 6 +- Tracker/Handler.php | 1 + Tracker/Response.php | 4 +- Updates/3.2.0.php | 1 + Updates/3.3.4.php | 1 + Updates/5.0.8.php | 7 +- tests/Framework/Mock/ForcedException.php | 3 +- tests/Framework/Mock/Tracker.php | 3 +- tests/Framework/Mock/Tracker/Response.php | 2 +- .../TestCase/IntegrationTestCase.php | 4 +- tests/Integration/Queue/Backend/MysqlTest.php | 6 +- tests/Integration/Queue/Backend/RedisTest.php | 4 +- .../Queue/Backend/SentinelTest.php | 1 + tests/Integration/Queue/FactoryTest.php | 3 +- tests/Integration/Queue/LockTest.php | 3 +- tests/Integration/Queue/ManagerTest.php | 9 +- .../Queue/Processor/HandlerTest.php | 14 +-- tests/Integration/Queue/ProcessorTest.php | 14 +-- tests/Integration/QueueTest.php | 1 + tests/Integration/QueuedTrackingTest.php | 3 +- tests/Integration/Settings/NumWorkersTest.php | 2 +- tests/Integration/SettingsTest.php | 2 +- tests/Integration/SystemCheckTest.php | 2 +- tests/Integration/Tracker/HandlerTest.php | 1 + .../CheckDirectDependencyUseCommandTest.php | 3 +- tests/System/TrackerTest.php | 1 + tests/Unit/ConfigurationTest.php | 4 +- tests/Unit/Queue/Processor/HandlerTest.php | 5 +- tests/Unit/QueueTest.php | 2 + 51 files changed, 227 insertions(+), 176 deletions(-) diff --git a/Commands/LockStatus.php b/Commands/LockStatus.php index 8ffce4c..ab85bfb 100644 --- a/Commands/LockStatus.php +++ b/Commands/LockStatus.php @@ -1,4 +1,5 @@ setName('queuedtracking:lock-status'); diff --git a/Commands/Monitor.php b/Commands/Monitor.php index 25b633a..0f8c98a 100644 --- a/Commands/Monitor.php +++ b/Commands/Monitor.php @@ -1,4 +1,5 @@ setName('queuedtracking:monitor'); @@ -38,7 +38,7 @@ protected function doExecute(): int } $output->write(str_repeat("\r\n", 100)); - $output->write("\e[".(100)."A"); + $output->write("\e[" . (100) . "A"); $iterations = $this->getIterationsFromArg(); if ($iterations !== null) { @@ -63,22 +63,25 @@ protected function doExecute(): int } $output->writeln(sprintf('Up to %d workers will be used', $manager->getNumberOfAvailableQueues())); - $output->writeln(sprintf('Processor will start once there are at least %s request sets in the queue', - $manager->getNumberOfRequestsToProcessAtSameTime())); + $output->writeln(sprintf( + 'Processor will start once there are at least %s request sets in the queue', + $manager->getNumberOfRequestsToProcessAtSameTime() + )); $iterationCount = 0; - + $qCurrentPage = 1; $qCount = count($queues); $qPerPAge = min(max($this->getPerPageFromArg(), 1), $qCount); $qPageCount = ceil($qCount / $qPerPAge); - - readline_callback_handler_install('', function() {}); - stream_set_blocking (STDIN, false); + + readline_callback_handler_install('', function () { + }); + stream_set_blocking(STDIN, false); $output->writeln(str_repeat("-", 30)); - $output->writeln("".str_pad(" Q INDEX", 10).str_pad(" | REQUEST SETS", 20).""); + $output->writeln("" . str_pad(" Q INDEX", 10) . str_pad(" | REQUEST SETS", 20) . ""); $output->writeln(str_repeat("-", 30)); - + $lastStatsTimer = microtime(true) - 2; $lastSumInQueue = false; $diffSumInQueue = 0; @@ -87,13 +90,12 @@ protected function doExecute(): int $output->write(str_repeat("\r\n", $qPerPAge + 5)); while (1) { - if (microtime(true) - $lastStatsTimer >= 2 || $keyPressed != "") - { - $output->write("\e[".($qPerPAge + 5)."A"); + if (microtime(true) - $lastStatsTimer >= 2 || $keyPressed != "") { + $output->write("\e[" . ($qPerPAge + 5) . "A"); $qCurrentPage = min(max($qCurrentPage, 1), $qPageCount); $memory = $backend->getMemoryStats(); // I know this will only work with redis currently as it is not defined in backend interface etc. needs to be refactored once we add another backend - + $sumInQueue = 0; foreach ($queues as $sumQ) { $sumInQueue += $sumQ->getNumberOfRequestSetsInQueue(); @@ -102,31 +104,34 @@ protected function doExecute(): int if ($lastSumInQueue !== false) { $diffSumInQueue = $lastSumInQueue - $sumInQueue; $diffRps = round($diffSumInQueue / (microtime(true) - $lastStatsTimer), 2); - $diffSumInQueue = $diffSumInQueue < 0 ? "".abs($diffRps)."" : "{$diffRps}"; + $diffSumInQueue = $diffSumInQueue < 0 ? "" . abs($diffRps) . "" : "{$diffRps}"; } - + $numInQueue = 0; for ($idxPage = 0; $idxPage < $qPerPAge; $idxPage++) { $idx = ($qCurrentPage - 1) * $qPerPAge + $idxPage; if (isset($queues[$idx])) { $q = $queues[$idx]->getNumberOfRequestSetsInQueue(); $numInQueue += (int)$q; - $output->writeln(str_pad($idx, 10, " ", STR_PAD_LEFT)." | ".str_pad(number_format($q), 16, " ", STR_PAD_LEFT)); + $output->writeln(str_pad($idx, 10, " ", STR_PAD_LEFT) . " | " . str_pad(number_format($q), 16, " ", STR_PAD_LEFT)); } else { - $output->writeln(str_pad("", 10)." | ".str_pad("", 16)); + $output->writeln(str_pad("", 10) . " | " . str_pad("", 16)); } } - + $output->writeln(str_repeat("-", 30)); - $output->writeln("".str_pad(" ".($qCount)." Q", 10)." | ".str_pad(number_format($sumInQueue)." R", 16).""); + $output->writeln("" . str_pad(" " . ($qCount) . " Q", 10) . " | " . str_pad(number_format($sumInQueue) . " R", 16) . ""); $output->writeln(str_repeat("-", 30)); $output->writeln(sprintf( - "Q [%s-%s] | page %s/%s | press (0-9.,q) or arrow(L,R,U,D) | diff/sec %s \n". - "%s used memory (%s peak). %d workers active.".str_repeat(" ", 15), - ($idx - $qPerPAge + 1), - $idx, $qCurrentPage, $qPageCount, $diffSumInQueue, - $memory['used_memory_human'] ?? 'Unknown', - $memory['used_memory_peak_human'] ?? 'Unknown', + "Q [%s-%s] | page %s/%s | press (0-9.,q) or arrow(L,R,U,D) | diff/sec %s \n" . + "%s used memory (%s peak). %d workers active." . str_repeat(" ", 15), + ($idx - $qPerPAge + 1), + $idx, + $qCurrentPage, + $qPageCount, + $diffSumInQueue, + $memory['used_memory_human'] ?? 'Unknown', + $memory['used_memory_peak_human'] ?? 'Unknown', $lock->getNumberOfAcquiredLocks() )); @@ -145,26 +150,35 @@ protected function doExecute(): int $keyPressed = strlen($keyStroke) == 3 ? $keyStroke[2] : (strlen($keyStroke) > 0 ? $keyStroke[0] : ""); if ($keyPressed != "" and in_array($keyPressed, array(".", ",", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "q"))) { switch ($keyPressed) { - case "0": case "1": case "2": case "3": case "4": - case "5": case "6": case "7": case "8": case "9": - $keyPressed = $keyPressed != "0" ? $keyPressed : "10"; - $qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed; break; - case "C": + case "0": + case "1": + case "2": + case "3": + case "4": + case "5": + case "6": + case "7": + case "8": + case "9": + $keyPressed = $keyPressed != "0" ? $keyPressed : "10"; + $qCurrentPage = floor(($qCurrentPage - 0.1) / 10) * 10 + (int)$keyPressed; + break; + case "C": $qCurrentPage++; break; - case "D": + case "D": $qCurrentPage--; break; - case "A": + case "A": $qCurrentPage += 10; break; - case "B": + case "B": $qCurrentPage -= 10; break; - case ",": + case ",": $qCurrentPage = 1; break; - case ".": + case ".": $qCurrentPage = $qPageCount; break; case "q": @@ -204,7 +218,7 @@ private function getIterationsFromArg() * Loads the `perpage` argument from the commands arguments. * * @return int|null - */ + */ private function getPerPageFromArg() { $perPage = $this->getInput()->getOption('perpage'); diff --git a/Commands/PrintQueuedRequests.php b/Commands/PrintQueuedRequests.php index 7dfd1dc..9628360 100644 --- a/Commands/PrintQueuedRequests.php +++ b/Commands/PrintQueuedRequests.php @@ -1,4 +1,5 @@ setName('queuedtracking:print-queued-requests'); @@ -54,7 +54,6 @@ protected function doExecute(): int $output->writeln(var_export($requests, 1)); $output->writeln(sprintf('These were the requests of queue %s. Use --queue-id=%s to print only information for this queue.', $thisQueueId, $thisQueueId)); - } return self::SUCCESS; diff --git a/Commands/Process.php b/Commands/Process.php index a913561..1290fa8 100644 --- a/Commands/Process.php +++ b/Commands/Process.php @@ -1,4 +1,5 @@ setName('queuedtracking:process'); - $this->addRequiredValueOption('queue-id', null, 'If set, will only work on that specific queue. For example "0" or "1" (if there are multiple queues). Not recommended when only one worker is in use. If for example 4 workers are in use, you may want to use 0, 1, 2, or 3.'); - $this->addRequiredValueOption('force-num-requests-process-at-once', null, 'If defined, it overwrites the setting of how many requests will be picked out of the queue and processed at once. Must be a number which is >= 1. By default, the configured value from the settings will be used. This can be useful for example if you want to process every single request within the queue. If otherwise a batch size of say 100 is configured, then there may be otherwise 99 requests left in the queue. It can be also useful for testing purposes.'); + $this->addRequiredValueOption( + 'queue-id', + null, + 'If set, will only work on that specific queue. For example "0" or "1" (if there are multiple queues). Not recommended when only one worker is in use. If for example 4 workers are in use, you may want to use 0, 1, 2, or 3.' + ); + $this->addRequiredValueOption( + 'force-num-requests-process-at-once', + null, + 'If defined, it overwrites the setting of how many requests will be picked out of the queue and processed at once. Must be a number which is >= 1. By default, the configured value from the settings will be used.' . + ' This can be useful for example if you want to process every single request within the queue.' . + ' If otherwise a batch size of say 100 is configured, then there may be otherwise 99 requests left in the queue. It can be also useful for testing purposes.' + ); $this->addRequiredValueOption('cycle', 'c', 'The proccess will automatically loop for "n" cycle time(s), set "0" to infinite.', 1); $this->addRequiredValueOption('sleep', 's', 'Take a nap for "n" second(s) before recycle, minimum is 1 second.', 1); $this->addRequiredValueOption('delay', 'd', 'Delay before finished', 0); - $this->setDescription('Processes all queued tracking requests in case there are enough requests in the queue and in case they are not already in process by another script. To keep track of the queue use the --verbose option or execute the queuedtracking:monitor command.'); + $this->setDescription('Processes all queued tracking requests in case there are enough requests in the queue and in case they are not already in process by another script. To keep track of the queue use the --verbose' . + ' option or execute the queuedtracking:monitor command.'); } /** @@ -91,7 +102,7 @@ protected function doExecute(): int } $numberOfProcessCycle = (int)$numberOfProcessCycle; $infiniteCycle = $numberOfProcessCycle == 0; - + $delayedBeforeFinish = (int)$input->getOption('delay'); $napster = max(1, $input->getOption('sleep')); @@ -156,7 +167,7 @@ protected function doExecute(): int if ($delayedBeforeFinish > 0) { sleep($delayedBeforeFinish); } - + return self::SUCCESS; } diff --git a/Commands/Test.php b/Commands/Test.php index f78de26..038f0a0 100644 --- a/Commands/Test.php +++ b/Commands/Test.php @@ -1,4 +1,5 @@ writeln('PHPRedis version: ' . $extension->getVersion()); - } catch(\Exception $e) { + } catch (\Exception $e) { $output->writeln('No PHPRedis extension (not a problem if sentinel is used):' . $e->getMessage()); } } @@ -107,12 +108,14 @@ protected function doExecute(): int $redis = $backend->getConnection(); if ($isUsingRedis && !$shouldSkipCheckingMemoryConfigValues) { - $evictionPolicy = $this->getRedisConfig($redis, 'maxmemory-policy'); $output->writeln('MaxMemory Eviction Policy config: ' . $evictionPolicy); if ($evictionPolicy !== 'allkeys-lru' && $evictionPolicy !== 'noeviction') { - $output->writeln('The eviction policy can likely lead to errors when memory is low. We recommend to use eviction policy allkeys-lru or alternatively noeviction. Read more here: http://redis.io/topics/lru-cache'); + $output->writeln( + 'The eviction policy can likely lead to errors when memory is low. We recommend to use eviction policy allkeys-lru or alternatively noeviction.' . + ' Read more here: http://redis.io/topics/lru-cache' + ); } $evictionPolicy = $this->getRedisConfig($redis, 'maxmemory'); @@ -126,7 +129,7 @@ protected function doExecute(): int $output->writeln('Redis is connected: ' . (int) $redis->isConnected()); } - if ($backend->testConnection()){ + if ($backend->testConnection()) { $output->writeln('Connection works in general'); } else { $output->writeln('Connection does not actually work: ' . $redis->getLastError()); @@ -144,7 +147,7 @@ protected function doExecute(): int if (!$backend->setIfNotExists('foo', 'bar', 5)) { $message = "setIfNotExists(foo, bar, 1) does not work, most likely we won't be able to acquire a lock: " . $backend->getLastError(); $output->writeln($message); - } else{ + } else { $initialTtl = $backend->getTimeToLive('foo'); if ($initialTtl >= 3000 && $initialTtl <= 5000) { $output->writeln('Initial expire seems to be set correctly'); @@ -195,7 +198,6 @@ protected function doExecute(): int $backend->appendValuesToList('fooList', array('value1', 'value2', 'value3')); $values = $backend->getFirstXValuesFromList('fooList', 2); if ($values == array('value1', 'value2')) { - $backend->removeFirstXValuesFromList('fooList', 1); $backend->removeFirstXValuesFromList('fooList', 1); $values = $backend->getFirstXValuesFromList('fooList', 2); @@ -204,7 +206,6 @@ protected function doExecute(): int } else { $output->writeln('List feature seems to work only partially: ' . var_export($values, 1)); } - } else { $output->writeln('List feature seems to not work fine: ' . $redis->getLastError() . ''); } @@ -247,11 +248,11 @@ private function testRedis($redis, $method, $params, $keyToCleanUp) $result = call_user_func_array(array($redis, $method), $params); - $paramsMapped = array_map(function($item) { + $paramsMapped = array_map(function ($item) { if (is_string($item)) { return $item; } - + return str_replace(["\r", "\n", " "], '', var_export($item, true)); }, $params); $paramsInline = implode(', ', $paramsMapped); diff --git a/Configuration.php b/Configuration.php index ad54131..d55f1d6 100644 --- a/Configuration.php +++ b/Configuration.php @@ -1,4 +1,5 @@ getEngine(); - $statement = sprintf("CREATE TABLE IF NOT EXISTS `%s` ( %s ) ENGINE=%s DEFAULT CHARSET=utf8 ;", + $statement = sprintf( + "CREATE TABLE IF NOT EXISTS `%s` ( %s ) ENGINE=%s DEFAULT CHARSET=utf8 ;", $table, $createDefinition, - $engine); + $engine + ); // DbHelper::createTable() won't work in tracker mode! Db::get()->query($statement); @@ -70,7 +74,6 @@ public function testConnection() { try { return '1' == Db::get()->fetchOne('SELECT 1'); - } catch (\Exception $e) { Log::debug($e->getMessage()); } @@ -105,7 +108,6 @@ public function appendValuesToList($key, $values) $value = gzcompress($value); try { - $result = Db::query($query, array($value)); if ($result === false) { @@ -115,11 +117,9 @@ public function appendValuesToList($key, $values) } unset($result); - } catch (\Exception $e) { // anything else but Mysqli in tracker mode (eg PDO or Mysqli in regular mode) if ($this->isErrorTableNotExists($e)) { - // we create list tables only on demand $this->createListTable($key); Db::query($query, array($value)); @@ -265,22 +265,26 @@ public function setIfNotExists($key, $value, $ttlInSeconds) // todo: we could combine get() and keyExists() in one query! if ($this->keyExists($key)) { // most of the time an expired key should not exist... we don't want to lock the row unncessarily therefore we check first - // if value exists... + // if value exists... $sql = sprintf('DELETE FROM %s WHERE queue_key = ? and not (%s)', $this->tablePrefixed, $this->getQueryPartExpiryTime()); Db::query($sql, array($key)); } - $query = sprintf('INSERT INTO %s (`queue_key`, `queue_value`, `expiry_time`) + $query = sprintf( + 'INSERT INTO %s (`queue_key`, `queue_value`, `expiry_time`) VALUES (?,?,(UNIX_TIMESTAMP() + ?))', - $this->tablePrefixed); + $this->tablePrefixed + ); // we make sure to update the row if the key is expired and consider it as "deleted" try { $query = Db::query($query, array($key, $value, (int) $ttlInSeconds)); } catch (\Exception $e) { - if ($e->getCode() == 23000 + if ( + $e->getCode() == 23000 || strpos($e->getMessage(), 'Duplicate entry') !== false - || strpos($e->getMessage(), ' 1062 ') !== false) { + || strpos($e->getMessage(), ' 1062 ') !== false + ) { return false; } throw $e; @@ -431,5 +435,4 @@ public function flushAll() $this->dropTable($table); } } - } diff --git a/Queue/Backend/Redis.php b/Queue/Backend/Redis.php index 95f590a..f2f4cf2 100644 --- a/Queue/Backend/Redis.php +++ b/Queue/Backend/Redis.php @@ -1,4 +1,5 @@ connectIfNeeded(); return 'TEST' === $this->redis->echo('TEST'); - } catch (\Exception $e) { Log::debug($e->getMessage()); } @@ -117,11 +118,11 @@ public function getFirstXValuesFromList($key, $numValues) $this->connectIfNeeded(); $values = $this->redis->lRange($key, 0, $numValues - 1); - foreach($values as $key => $value) { + foreach ($values as $key => $value) { $tmpValue = @gzuncompress($value); // Avoid warning if not compressed - + // if empty, string is not compressed. Use original value - if(empty($tmpValue)) { + if (empty($tmpValue)) { $values[$key] = $value; } else { $values[$key] = $tmpValue; diff --git a/Queue/Backend/RedisCluster.php b/Queue/Backend/RedisCluster.php index 7d5319c..848d8ab 100644 --- a/Queue/Backend/RedisCluster.php +++ b/Queue/Backend/RedisCluster.php @@ -1,15 +1,16 @@ connectIfNeeded(); return 'TEST' === $this->redis->echo('TEST_ECHO', 'TEST'); - } catch (\Exception $e) { Log::debug($e->getMessage()); } @@ -67,16 +67,17 @@ public function getLastError() * @param int|float|double $bytes byte number. * @param int $precision decimal round. * * @return string - */ - private function formatBytes($bytes, $precision = 2) { - $units = array('B', 'K', 'M', 'G', 'T'); - - $bytes = max($bytes, 0); - $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); - $pow = min($pow, count($units) - 1); - $bytes /= (1 << (10 * $pow)); - - return round($bytes, $precision) . $units[$pow]; + */ + private function formatBytes($bytes, $precision = 2) + { + $units = array('B', 'K', 'M', 'G', 'T'); + + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + $bytes /= (1 << (10 * $pow)); + + return round($bytes, $precision) . $units[$pow]; } public function getMemoryStats() @@ -91,7 +92,7 @@ public function getMemoryStats() 'used_memory_peak_human' => 0 ); - foreach ($hosts as $idx=>$host) { + foreach ($hosts as $idx => $host) { $info = $this->redis->info(array($host, (int)$ports[$idx]), 'memory'); $memory['used_memory_human'] += $info['used_memory'] ?? 0; $memory['used_memory_peak_human'] += $info['used_memory_peak'] ?? 0; @@ -149,11 +150,11 @@ public function getFirstXValuesFromList($key, $numValues) $this->connectIfNeeded(); $values = $this->redis->lRange($key, 0, $numValues - 1); - foreach($values as $key => $value) { + foreach ($values as $key => $value) { $tmpValue = @gzuncompress($value); // Avoid warning if not compressed - + // if empty, string is not compressed. Use original value - if(empty($tmpValue)) { + if (empty($tmpValue)) { $values[$key] = $value; } else { $values[$key] = $tmpValue; @@ -283,7 +284,7 @@ public function flushAll() $hosts = explode(',', $this->host); $ports = explode(',', $this->port); - foreach ($hosts as $idx=>$host) { + foreach ($hosts as $idx => $host) { $this->redis->flushDB(array($host, (int)$ports[$idx])); } } @@ -307,7 +308,7 @@ protected function connect() $hostsPorts = array_map(fn($host, $port): string => "$host:$port", $hosts, $ports); try { - $this->redis = new \RedisCluster(NULL, $hostsPorts, $this->timeout, $this->timeout, true, $this->password); + $this->redis = new \RedisCluster(null, $hostsPorts, $this->timeout, $this->timeout, true, $this->password); return true; } catch (Exception $e) { throw new Exception('Could not connect to redis cluster: ' . $e->getMessage()); diff --git a/Queue/Backend/Sentinel.php b/Queue/Backend/Sentinel.php index 17a1c2c..271feec 100644 --- a/Queue/Backend/Sentinel.php +++ b/Queue/Backend/Sentinel.php @@ -1,4 +1,5 @@ getMessage()); } @@ -67,5 +68,4 @@ protected function evalScript($script, $keys, $args) { return $this->redis->eval($script, $keys, $args); } - } diff --git a/Queue/Factory.php b/Queue/Factory.php index 1caaefc..887801f 100644 --- a/Queue/Factory.php +++ b/Queue/Factory.php @@ -1,4 +1,5 @@ setSentinelMasterName($masterName); - $redis->setDatabase($database); + $redis->setDatabase($database); } - } - elseif($settings->isUsingClusterBackend()) { + } elseif ($settings->isUsingClusterBackend()) { $redis = new Queue\Backend\RedisCluster(); } else { $redis = new Queue\Backend\Redis(); - $redis->setDatabase($database); + $redis->setDatabase($database); } $redis->setConfig($host, $port, $timeout, $password); return $redis; } - } diff --git a/Queue/Lock.php b/Queue/Lock.php index 5b6189f..84a9561 100644 --- a/Queue/Lock.php +++ b/Queue/Lock.php @@ -1,4 +1,5 @@ createQueue($queueId); while ($requestSets = $queue->getRequestSetsToProcess()) { - foreach ($requestSets as $requestSet) { $this->addRequestSetToQueues($requestSet); } @@ -216,14 +217,13 @@ protected function getQueueIdForVisitor($visitorId) $visitorId = strtolower(substr($visitorId, 0, 3)); if (ctype_xdigit($visitorId) === true) { $id = hexdec($visitorId); - } - else { + } else { $pos1 = ord($visitorId); $pos2 = isset($visitorId[1]) ? ord($visitorId[1]) : $pos1; $pos3 = isset($visitorId[2]) ? ord($visitorId[2]) : $pos2; $id = $pos1 + $pos2 + $pos3; } - + return $id % $this->numQueuesAvailable; } @@ -264,7 +264,7 @@ public function lockNext() if ($shouldProcess && $this->lock->acquireLock($this->forceQueueId)) { return $queue; } - + // do not try to acquire a different lock return; } diff --git a/Queue/Processor.php b/Queue/Processor.php index 439945d..e69a327 100644 --- a/Queue/Processor.php +++ b/Queue/Processor.php @@ -1,4 +1,5 @@ queueManager->lockNext()) { Common::printDebug('Acquired lock for queue ' . $queue->getId()); @@ -157,7 +157,6 @@ public function process(Tracker $tracker = null) $this->queueManager->unlock(); } - } catch (Exception $e) { Common::printDebug('Failed to process a request set: ' . $e->getMessage()); @@ -243,5 +242,4 @@ private function forceRollbackAndThrowExceptionAsAnotherProcessMightProcessSameR $this->handler->rollBack($tracker); throw new LockExpiredException(sprintf("Rolled back during %s as we no longer have lock or the lock was never acquired. So far tracker processed %s requests", $activity, $tracker->getCountOfLoggedRequests())); } - } diff --git a/Queue/Processor/Handler.php b/Queue/Processor/Handler.php index a9543c1..f0cdb7b 100644 --- a/Queue/Processor/Handler.php +++ b/Queue/Processor/Handler.php @@ -1,4 +1,5 @@ Piwik::translate('QueuedTracking_AvailableRedisBackendTypeStandAlone'), - 2=>Piwik::translate('QueuedTracking_AvailableRedisBackendTypeSentinel'), - 3=>Piwik::translate('QueuedTracking_AvailableRedisBackendTypeCluster') + 1 => Piwik::translate('QueuedTracking_AvailableRedisBackendTypeStandAlone'), + 2 => Piwik::translate('QueuedTracking_AvailableRedisBackendTypeSentinel'), + 3 => Piwik::translate('QueuedTracking_AvailableRedisBackendTypeCluster') ); } - protected function assignValueIsIntValidator (FieldConfig $field) { + protected function assignValueIsIntValidator(FieldConfig $field) + { $field->validate = function ($value) { if ((is_string($value) && !ctype_digit($value)) || (!is_string($value) && !is_int($value))) { throw new \Exception(Piwik::translate('QueuedTracking_ExceptionValueIsNotInt')); @@ -402,5 +404,4 @@ public function save() } } } - } diff --git a/Tasks.php b/Tasks.php index 7c19432..a1438bd 100644 --- a/Tasks.php +++ b/Tasks.php @@ -1,4 +1,5 @@ isMysqlBackend() && $settings->queueEnabled->getValue()) { - $db = Db::get(); $prefix = Common::prefixTable(MySQL::QUEUED_TRACKING_TABLE_PREFIX); $tables = $db->fetchCol("SHOW TABLES LIKE '" . $prefix . "%'"); @@ -104,5 +105,4 @@ public function optimizeQueueTable() Db::optimizeTables($tables, $force); } } - -} \ No newline at end of file +} diff --git a/Tracker/Handler.php b/Tracker/Handler.php index 3b695e5..f90881d 100644 --- a/Tracker/Handler.php +++ b/Tracker/Handler.php @@ -1,4 +1,5 @@ 1) { @@ -26,5 +27,4 @@ public function sendResponseToBrowserDirectly() ob_end_flush(); flush(); } - } diff --git a/Updates/3.2.0.php b/Updates/3.2.0.php index b3c2e9e..721ac9b 100644 --- a/Updates/3.2.0.php +++ b/Updates/3.2.0.php @@ -1,4 +1,5 @@ getValue() == 0) { $tmp_useWhatRedisBackendType->setValue($old_useSentinelBackend->getValue() == true ? 2 : 1); $tmp_useWhatRedisBackendType->save(); - } + } } } diff --git a/tests/Framework/Mock/ForcedException.php b/tests/Framework/Mock/ForcedException.php index 889f097..fb31cf5 100644 --- a/tests/Framework/Mock/ForcedException.php +++ b/tests/Framework/Mock/ForcedException.php @@ -1,4 +1,5 @@ '1', 'index' => $i)); } - } $set = new RequestSet(); @@ -128,6 +128,4 @@ protected function buildRequestSetContainingError($numberOfRequestSets, $indexTh return $set; } - - } diff --git a/tests/Integration/Queue/Backend/MysqlTest.php b/tests/Integration/Queue/Backend/MysqlTest.php index 58dab6f..c9195b3 100644 --- a/tests/Integration/Queue/Backend/MysqlTest.php +++ b/tests/Integration/Queue/Backend/MysqlTest.php @@ -1,4 +1,5 @@ assertTrue($this->backend->hasAtLeastXRequestsQueued($this->emptyListKey, 1)); $this->assertFalse($this->backend->hasAtLeastXRequestsQueued($this->emptyListKey, 2)); - $this->backend->appendValuesToList($this->emptyListKey, range(1,11)); + $this->backend->appendValuesToList($this->emptyListKey, range(1, 11)); $this->assertTrue($this->backend->hasAtLeastXRequestsQueued($this->emptyListKey, 10)); $this->assertTrue($this->backend->hasAtLeastXRequestsQueued($this->emptyListKey, 11)); $this->assertTrue($this->backend->hasAtLeastXRequestsQueued($this->emptyListKey, 12)); @@ -370,5 +371,4 @@ public function test_getKeysMatchingPattern_shouldReturnAnEmptyArrayIfNothingMat $keys = $backend->getKeysMatchingPattern('*fere*'); $this->assertEquals(array(), $keys); } - } diff --git a/tests/Integration/Queue/Backend/RedisTest.php b/tests/Integration/Queue/Backend/RedisTest.php index db0ba90..85cc2ca 100644 --- a/tests/Integration/Queue/Backend/RedisTest.php +++ b/tests/Integration/Queue/Backend/RedisTest.php @@ -1,4 +1,5 @@ assertTrue($this->redis->hasAtLeastXRequestsQueued($this->emptyListKey, 1)); $this->assertFalse($this->redis->hasAtLeastXRequestsQueued($this->emptyListKey, 2)); - $this->redis->appendValuesToList($this->emptyListKey, range(1,11)); + $this->redis->appendValuesToList($this->emptyListKey, range(1, 11)); $this->assertTrue($this->redis->hasAtLeastXRequestsQueued($this->emptyListKey, 10)); $this->assertTrue($this->redis->hasAtLeastXRequestsQueued($this->emptyListKey, 11)); $this->assertTrue($this->redis->hasAtLeastXRequestsQueued($this->emptyListKey, 12)); @@ -329,5 +330,4 @@ public function test_getKeysMatchingPattern_shouldReturnAnEmptyArrayIfNothingMat $keys = $backend->getKeysMatchingPattern('*fere*'); $this->assertEquals(array(), $keys); } - } diff --git a/tests/Integration/Queue/Backend/SentinelTest.php b/tests/Integration/Queue/Backend/SentinelTest.php index eabe4fd..a7f3ca1 100644 --- a/tests/Integration/Queue/Backend/SentinelTest.php +++ b/tests/Integration/Queue/Backend/SentinelTest.php @@ -1,4 +1,5 @@ createRedisBackend()); @@ -98,5 +98,4 @@ public function test_getSettings_shouldReturnASingleton() $settings = Factory::getSettings(); $this->assertEquals(0.7, $settings->redisTimeout->getValue()); } - } diff --git a/tests/Integration/Queue/LockTest.php b/tests/Integration/Queue/LockTest.php index d0407e5..ef3cade 100644 --- a/tests/Integration/Queue/LockTest.php +++ b/tests/Integration/Queue/LockTest.php @@ -1,4 +1,5 @@ assertSame(26, $this->manager->getNumberOfRequestSetsInAllQueues()); - $this->assertNumberOfRequestSetsInQueueEquals(3, $queueId = 0); + $this->assertNumberOfRequestSetsInQueueEquals(3, $queueId = 0); $this->assertNumberOfRequestSetsInQueueEquals(9, $queueId = 1); - $this->assertNumberOfRequestSetsInQueueEquals(7, $queueId = 2); + $this->assertNumberOfRequestSetsInQueueEquals(7, $queueId = 2); $this->assertNumberOfRequestSetsInQueueEquals(7, $queueId = 3); - $this->assertNumberOfRequestSetsInQueueEquals(0, $queueId = 4); // this queue is not available + $this->assertNumberOfRequestSetsInQueueEquals(0, $queueId = 4); // this queue is not available } public function test_addRequestSetToQueues_getNumberOfRequestSetsInAllQueues_shouldMoveAllInSameQueue_IfAllHaveSameUID() @@ -455,5 +455,4 @@ private function addRequestSetToQueues($numRequestSets) $this->manager->addRequestSetToQueues($this->buildRequestSetWithIdSite(1)); } } - } diff --git a/tests/Integration/Queue/Processor/HandlerTest.php b/tests/Integration/Queue/Processor/HandlerTest.php index c207d92..953ba2c 100644 --- a/tests/Integration/Queue/Processor/HandlerTest.php +++ b/tests/Integration/Queue/Processor/HandlerTest.php @@ -1,4 +1,5 @@ transactionId; @@ -46,13 +47,13 @@ public function setUp(): void parent::setUp(); Fixture::createWebsite('2014-01-02 03:04:05'); - + $this->handler = $this->createHandler(); $this->tracker = new Tracker(); $this->handler->init($this->tracker); } - + public function tearDown(): void { $this->handler->rollBack($this->tracker); @@ -273,12 +274,12 @@ public function test_process_ShouldRestoreTheEnvironmentOfARequest() $cookieBackup = $_COOKIE; $requestSet = $this->buildRequestSet(2); - $requestSet->setEnvironment(array('server' => array('myserver' => 0), 'cookie' => array('testcookie'=> 7))); + $requestSet->setEnvironment(array('server' => array('myserver' => 0), 'cookie' => array('testcookie' => 7))); $this->handler->process($this->tracker, $requestSet); $this->assertEquals(array('myserver' => 0), $_SERVER); - $this->assertEquals(array('testcookie'=> 7), $_COOKIE); + $this->assertEquals(array('testcookie' => 7), $_COOKIE); $_SERVER = $serverBackup; $_COOKIE = $cookieBackup; @@ -288,5 +289,4 @@ private function createHandler() { return new TestHandler(); } - } diff --git a/tests/Integration/Queue/ProcessorTest.php b/tests/Integration/Queue/ProcessorTest.php index 40f5b9c..1661fdb 100644 --- a/tests/Integration/Queue/ProcessorTest.php +++ b/tests/Integration/Queue/ProcessorTest.php @@ -1,4 +1,5 @@ backend = $this->createMySQLBackend(); $this->lock = new Queue\Lock($this->backend); @@ -181,7 +181,7 @@ public function test_processRequestSets_ShouldReturnAnEmptyArrayIfAllWereTracker $requestSetsToRetry = $this->processor->processRequestSets($tracker, $queuedRequestSets); $this->assertEquals(array(), $requestSetsToRetry); - $this->assertSame(5+1+1+3, $tracker->getCountOfLoggedRequests()); + $this->assertSame(5 + 1 + 1 + 3, $tracker->getCountOfLoggedRequests()); } public function test_processRequestSets_ShouldReturnOnlyValidRequestSetsInCaseThereIsAFaultyOne() @@ -282,7 +282,7 @@ public function test_process_ShouldRetryProcessingAllRequestsWithoutTheFailedOne $tracker = $this->processor->process($this->createTracker()); - $this->assertSame(1+5+1+2+1+4, $tracker->getCountOfLoggedRequests()); + $this->assertSame(1 + 5 + 1 + 2 + 1 + 4, $tracker->getCountOfLoggedRequests()); $this->assertNumberOfRequestSetsLeftInQueue(0); } @@ -361,7 +361,7 @@ public function test_process_shouldRestoreEnvironmentAfterTrackingRequests() $this->queue->setNumberOfRequestsToProcessAtSameTime(1); $requestSet = $this->buildRequestSet(5); - $requestSet->setEnvironment(array('server' => array('test' => 1), 'cookie' => array('testcookie'=> 7))); + $requestSet->setEnvironment(array('server' => array('test' => 1), 'cookie' => array('testcookie' => 7))); $this->queue->addRequestSetToQueues($requestSet); $tracker = $this->process(); diff --git a/tests/Integration/QueueTest.php b/tests/Integration/QueueTest.php index e476a09..8ce7a48 100644 --- a/tests/Integration/QueueTest.php +++ b/tests/Integration/QueueTest.php @@ -1,4 +1,5 @@ assertTrue($handler instanceof Handler); } - } diff --git a/tests/Integration/Settings/NumWorkersTest.php b/tests/Integration/Settings/NumWorkersTest.php index 8a98e65..00f2c60 100644 --- a/tests/Integration/Settings/NumWorkersTest.php +++ b/tests/Integration/Settings/NumWorkersTest.php @@ -1,4 +1,5 @@ assertSame(0, $queues[2]->getNumberOfRequestSetsInQueue()); $this->assertSame(0, $queues[3]->getNumberOfRequestSetsInQueue()); } - } diff --git a/tests/Integration/SettingsTest.php b/tests/Integration/SettingsTest.php index 7c1abc5..09b5d38 100644 --- a/tests/Integration/SettingsTest.php +++ b/tests/Integration/SettingsTest.php @@ -1,4 +1,5 @@ usesFoundList[$pluginName]); } -} \ No newline at end of file +} diff --git a/tests/System/TrackerTest.php b/tests/System/TrackerTest.php index 9d3ee00..ffa8089 100644 --- a/tests/System/TrackerTest.php +++ b/tests/System/TrackerTest.php @@ -1,4 +1,5 @@ QueuedTracking = array(); $this->assertEquals(Configuration::$DEFAULT_NOTIFY_EMAILS, $this->configuration->getNotifyEmails()); } - - } diff --git a/tests/Unit/Queue/Processor/HandlerTest.php b/tests/Unit/Queue/Processor/HandlerTest.php index b65a77e..01f6396 100644 --- a/tests/Unit/Queue/Processor/HandlerTest.php +++ b/tests/Unit/Queue/Processor/HandlerTest.php @@ -1,4 +1,5 @@ assertEquals($this->transactionId, $this->db->rollbackTransactionId); $this->assertFalse($this->db->commitTransactionId); } - } diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index 838ca12..110ff97 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -1,4 +1,5 @@