From 6ca1179e4788e5e4565321c6a9095f0418a00cdd Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sat, 2 Nov 2013 10:35:59 +0100 Subject: [PATCH] [IMPROVED] Optimized IDLE Worker Query Fixes #811 --- public/include/classes/base.class.php | 3 ++ public/include/classes/worker.class.php | 59 ++++++++----------------- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/public/include/classes/base.class.php b/public/include/classes/base.class.php index 1f059c37..d02283e0 100644 --- a/public/include/classes/base.class.php +++ b/public/include/classes/base.class.php @@ -47,6 +47,9 @@ class Base { public function setTokenType($tokentype) { $this->tokentype = $tokentype; } + public function setShare($share) { + $this->share = $share; + } public function setErrorMessage($msg) { $this->sError = $msg; } diff --git a/public/include/classes/worker.class.php b/public/include/classes/worker.class.php index 45ee6ff6..f468d30c 100644 --- a/public/include/classes/worker.class.php +++ b/public/include/classes/worker.class.php @@ -1,38 +1,10 @@ debug = $debug; - $this->mysqli = $mysqli; - $this->user = $user; - $this->share = $share; - $this->config = $config; - $this->debug->append("Instantiated Worker class", 2); - } - - // get and set methods - private function setErrorMessage($msg) { - $this->sError = $msg; - } - public function getError() { - return $this->sError; - } - - private function checkStmt($bState) { - if ($bState ===! true) { - $this->debug->append("Failed to prepare statement: " . $this->mysqli->error); - $this->setErrorMessage('Internal application Error'); - return false; - } - return true; - } +class Worker extends Base { + protected $table = 'pool_worker'; /** * Update worker list for a user @@ -71,16 +43,19 @@ class Worker { * @param none * @return data array Workers in IDLE state and monitoring enabled **/ - public function getAllIdleWorkers() { + public function getAllIdleWorkers($interval=600) { $this->debug->append("STA " . __METHOD__, 4); $stmt = $this->mysqli->prepare(" - SELECT account_id, id, username - FROM " . $this->table . " AS w - WHERE monitor = 1 - AND ( - SELECT IFNULL(SUM(IF(our_result = 'Y', 1, 0)), 0) FROM " . $this->share->getTableName() . " WHERE username = w.username AND time > DATE_SUB(now(), INTERVAL 10 MINUTE) - ) = 0"); - if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) + SELECT w.account_id AS account_id, w.id AS id, w.username AS username + FROM " . $this->share->getTableName() . " AS s + RIGHT JOIN " . $this->getTableName() . " AS w + ON w.username = s.username + AND s.time > DATE_SUB(now(), INTERVAL ? SECOND) + AND our_result = 'Y' + WHERE w.monitor = 1 + AND s.id IS NULL + "); + if ($this->checkStmt($stmt) && $stmt->bind_param('i', $interval) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_all(MYSQLI_ASSOC); // Catchall $this->setErrorMessage("Unable to fetch IDLE, monitored workers"); @@ -297,4 +272,8 @@ class Worker { } } -$worker = new Worker($debug, $mysqli, $user, $share, $config); +$worker = new Worker(); +$worker->setDebug($debug); +$worker->setMysql($mysqli); +$worker->setShare($share); +$worker->setConfig($config);