Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize consumeTasks method to address performance issues caused by full table scans in SQL queries #164

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 49 additions & 43 deletions module/cron/control.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ public function consumeTasks(int $execId)
$this->cron->updateTime('consumer', $execId);

/* Consume. */
$task = $this->dao->select('*')->from(TABLE_QUEUE)->where('status')->eq('wait')->andWhere('command')->ne('')->orderBy('createdDate')->fetch();
$task = $this->dao->select('*')->from(TABLE_QUEUE)->where('status')->eq('wait')
// This condition can cause performance issues and should be handled within the application logic instead.
// ->andWhere('command')->ne('')
->orderBy('createdDate')->fetch();
if(!$task) break;

$this->consumeTask($execId, $task);
Expand All @@ -370,55 +373,58 @@ public function consumeTask(int $execId, object $task)
{
/* Other executor may execute the task at the same time,so we mark execId and wait 500ms to check whether we own it. */
$this->dao->clearCache();
$this->dao->update(TABLE_QUEUE)->set('status')->eq('doing')->set('execId')->eq($execId)->where('id')->eq($task->id)->exec();
usleep(500);

$task = $this->dao->select('*')->from(TABLE_QUEUE)->where('id')->eq($task->id)->fetch();
if($task->execId != $execId) return;

/* Execution command. */
$output = '';
$return = '';

unset($_SESSION['company']);
unset($this->app->company);

/* Mark that this request was triggered by the scheduled task, not by the user. */
$_SESSION['fromCron'] = true;

$this->loadModel('common');
$this->common->setCompany();
$this->common->loadConfigFromDB();

try
if (!empty($task->command))
{
if($task->type == 'zentao')
$this->dao->update(TABLE_QUEUE)->set('status')->eq('doing')->set('execId')->eq($execId)->where('id')->eq($task->id)->exec();
usleep(500);

$task = $this->dao->select('*')->from(TABLE_QUEUE)->where('id')->eq($task->id)->fetch();
if($task->execId != $execId) return;

/* Execution command. */
$output = '';
$return = '';

unset($_SESSION['company']);
unset($this->app->company);

/* Mark that this request was triggered by the scheduled task, not by the user. */
$_SESSION['fromCron'] = true;

$this->loadModel('common');
$this->common->setCompany();
$this->common->loadConfigFromDB();

try
{
parse_str($task->command, $params);
if(isset($params['moduleName']) and isset($params['methodName']))
if($task->type == 'zentao')
{
$this->viewType = 'html';
$moduleName = $params['moduleName'];
$methodName = $params['methodName'];
$this->app->loadLang($moduleName);
$this->app->loadConfig($moduleName);
unset($params['moduleName'], $params['methodName']);
$output = $this->fetch($moduleName, $methodName, $params);
parse_str($task->command, $params);
if(isset($params['moduleName']) and isset($params['methodName']))
{
$this->viewType = 'html';
$moduleName = $params['moduleName'];
$methodName = $params['methodName'];
$this->app->loadLang($moduleName);
$this->app->loadConfig($moduleName);
unset($params['moduleName'], $params['methodName']);
$output = $this->fetch($moduleName, $methodName, $params);
}
}
elseif($task->type == 'system')
{
exec($task->command, $out, $return);
if($out) $output = implode(PHP_EOL, $out);
}
}
elseif($task->type == 'system')
catch(EndResponseException $endResponseException)
{
exec($task->command, $out, $return);
if($out) $output = implode(PHP_EOL, $out);
$output = $endResponseException->getContent();
}
catch(Exception $e)
{
$output = $e;
}
}
catch(EndResponseException $endResponseException)
{
$output = $endResponseException->getContent();
}
catch(Exception $e)
{
$output = $e;
}

$this->dao->update(TABLE_QUEUE)->set('status')->eq('done')->where('id')->eq($task->id)->exec();
Expand Down