[IMPROVED] Optimized IDLE Worker Query

Fixes #811
This commit is contained in:
Sebastian Grewe 2013-11-02 10:35:59 +01:00
parent ab4e5df2bc
commit 6ca1179e47
2 changed files with 22 additions and 40 deletions

View File

@ -47,6 +47,9 @@ class Base {
public function setTokenType($tokentype) { public function setTokenType($tokentype) {
$this->tokentype = $tokentype; $this->tokentype = $tokentype;
} }
public function setShare($share) {
$this->share = $share;
}
public function setErrorMessage($msg) { public function setErrorMessage($msg) {
$this->sError = $msg; $this->sError = $msg;
} }

View File

@ -1,38 +1,10 @@
<?php <?php
// Make sure we are called from index.php // Make sure we are called from index.php
if (!defined('SECURITY')) if (!defined('SECURITY')) die('Hacking attempt');
die('Hacking attempt');
class Worker { class Worker extends Base {
private $sError = ''; protected $table = 'pool_worker';
private $table = 'pool_worker';
public function __construct($debug, $mysqli, $user, $share, $config) {
$this->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;
}
/** /**
* Update worker list for a user * Update worker list for a user
@ -71,16 +43,19 @@ class Worker {
* @param none * @param none
* @return data array Workers in IDLE state and monitoring enabled * @return data array Workers in IDLE state and monitoring enabled
**/ **/
public function getAllIdleWorkers() { public function getAllIdleWorkers($interval=600) {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare(" $stmt = $this->mysqli->prepare("
SELECT account_id, id, username SELECT w.account_id AS account_id, w.id AS id, w.username AS username
FROM " . $this->table . " AS w FROM " . $this->share->getTableName() . " AS s
WHERE monitor = 1 RIGHT JOIN " . $this->getTableName() . " AS w
AND ( ON w.username = s.username
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) AND s.time > DATE_SUB(now(), INTERVAL ? SECOND)
) = 0"); AND our_result = 'Y'
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) 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); return $result->fetch_all(MYSQLI_ASSOC);
// Catchall // Catchall
$this->setErrorMessage("Unable to fetch IDLE, monitored workers"); $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);