Chg: Renamed information cronjob section to background jobs and added queue status

This commit is contained in:
Lucas Bartholemy 2018-06-27 11:48:56 +02:00
parent 0d7926288d
commit 9dc691fc82
9 changed files with 240 additions and 48 deletions

View File

@ -56,3 +56,4 @@ HumHub Change Log - v1.3-dev Branch
- Enh: Added Theme cascading to reduce view overwrites
- Enh: Automatic theme stylesheet loading including parent theme stylesheets
- Chg: Moved OpenSans font to core assets
- Chg: Renamed information cronjob section to Background jobs and added queue status

View File

@ -11,6 +11,8 @@ namespace humhub\modules\admin\controllers;
use humhub\modules\admin\components\Controller;
use humhub\modules\admin\components\DatabaseInfo;
use humhub\modules\admin\libs\HumHubAPI;
use humhub\modules\queue\interfaces\QueueInfoInterface;
use ReflectionClass;
use Yii;
/**
@ -88,21 +90,44 @@ class InformationController extends Controller
/**
* Caching Options
*/
public function actionCronjobs()
public function actionBackgroundJobs()
{
$currentUser = '';
if (function_exists('get_current_user')) {
$currentUser = get_current_user();
$lastRunHourly = (int) Yii::$app->settings->getUncached('cronLastHourlyRun');
$lastRunDaily = (int) Yii::$app->settings->getUncached('cronLastDailyRun');
$queue = Yii::$app->queue;
$waitingJobs = null;
$delayedJobs = null;
$doneJobs = null;
$reservedJobs = null;
if ($queue instanceof QueueInfoInterface) {
/** @var QueueInfoInterface $queue */
$waitingJobs = $queue->getWaitingJobCount();
$delayedJobs = $queue->getDelayedJobCount();
$doneJobs = $queue->getDoneJobCount();
$reservedJobs = $queue->getReservedJobCount();
}
$lastRunHourly = Yii::$app->settings->getUncached('cronLastHourlyRun');
$lastRunDaily = Yii::$app->settings->getUncached('cronLastDailyRun');
$driverName = null;
try {
$reflect = new ReflectionClass($queue);
$driverName = $reflect->getShortName();
} catch (\ReflectionException $e) {
Yii::error('Could not determine queue driver: '. $e->getMessage());
}
return $this->render('cronjobs', [
return $this->render('background-jobs', [
'lastRunHourly' => $lastRunHourly,
'lastRunDaily' => $lastRunDaily,
'currentUser' => $currentUser,
'waitingJobs' => $waitingJobs,
'delayedJobs' => $delayedJobs,
'doneJobs' => $doneJobs,
'reservedJobs' => $reservedJobs,
'driverName' => $driverName
]);
}

View File

@ -0,0 +1,71 @@
<?php
/**
* @var string $driverName
* @var int|null $lastRunHourly
* @var int|null $lastRunDaily
* @var int|null $waitingJobs
* @var int|null $delayedJobs
* @var int|null $doneJobs
* @var int|null $reservedJobs
*/
if (empty($lastRunHourly)) {
$lastRunHourly = "<span style='color:red'>" . Yii::t('AdminModule.information', 'Never') . "</span>";
} else {
$lastRunHourly = Yii::$app->formatter->asRelativeTime($lastRunHourly);
}
if (empty($lastRunDaily)) {
$lastRunDaily = "<span style='color:red'>" . Yii::t('AdminModule.information', 'Never') . "</span>";
} else {
$lastRunDaily = Yii::$app->formatter->asRelativeTime($lastRunDaily);
}
?>
<div class="row">
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<?= Yii::t('AdminModule.information', '<strong>CronJob</strong> Status'); ?>
</div>
<div class="panel-body">
<strong><?= Yii::t('AdminModule.information', 'Last run (hourly):'); ?></strong><br> <?= $lastRunHourly; ?>
<br/><br/>
<strong><?= Yii::t('AdminModule.information', 'Last run (daily):'); ?></strong><br> <?= $lastRunDaily; ?>
<br/><br/>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel">
<div class="panel-heading">
<?= Yii::t('AdminModule.information', '<strong>Queue</strong> Status'); ?>
</div>
<div class="panel-body">
<strong><?= Yii::t('AdminModule.information', 'Driver'); ?></strong><br/>
<?= $driverName; ?><br/>
<br/>
<strong><?= Yii::t('AdminModule.information', 'Waiting'); ?></strong><br/>
<?= $waitingJobs ?><br/>
<br/>
<strong><?= Yii::t('AdminModule.information', 'Delayed'); ?></strong><br/>
<?= $delayedJobs ?><br/>
<br/>
<strong><?= Yii::t('AdminModule.information', 'Reserved'); ?></strong><br/>
<?= $reservedJobs ?><br/>
<br/>
<strong><?= Yii::t('AdminModule.information', 'Done'); ?></strong><br/>
<?= $doneJobs ?><br/>
<br/>
</div>
</div>
</div>
</div>
<p><?= Yii::t('AdminModule.information', 'Please refer to the documentation to setup the cronjobs and queue workers.'); ?></p>

View File

@ -1,32 +0,0 @@
<p>
<strong>Status:</strong><br/>
<?php
if ($lastRunHourly == "") {
$lastRunHourly = "<span style='color:red'>" . Yii::t('AdminModule.views_setting_cronjob', 'Never') . "</span>";
} else {
$lastRunHourly = \humhub\widgets\TimeAgo::widget(['timestamp' => $lastRunHourly]);
}
if ($lastRunDaily == "") {
$lastRunDaily = "<span style='color:red'>" . Yii::t('AdminModule.views_setting_cronjob', 'Never') . "</span>";
} else {
$lastRunDaily = \humhub\widgets\TimeAgo::widget(['timestamp' => $lastRunDaily]);
}
?>
<?= Yii::t('AdminModule.views_setting_cronjob', 'Last run (hourly):'); ?> <?= $lastRunHourly; ?> <br/>
<?= Yii::t('AdminModule.views_setting_cronjob', 'Last run (daily):'); ?> <?= $lastRunDaily; ?>
</p>
<p><?= Yii::t('AdminModule.views_setting_cronjob', 'Please make sure following cronjobs are installed:'); ?></p>
<pre>
<strong><?= Yii::t('AdminModule.views_setting_cronjob', 'Crontab of user: {user}', array('{user}' => $currentUser)); ?></strong>
30 * * * * <?= Yii::getAlias('@app/yii'); ?> cron/hourly >/dev/null 2>&1
0 18 * * * <?= Yii::getAlias('@app/yii'); ?> cron/daily >/dev/null 2>&1
<?php if ($currentUser != ""): ?>
<strong><?= Yii::t('AdminModule.views_setting_cronjob', 'Or Crontab of root user'); ?></strong>
*/5 * * * * su -c "<?= Yii::getAlias('@app/yii'); ?> cron/hourly" <?= $currentUser; ?> >/dev/null 2>&1
0 18 * * * su -c "<?= Yii::getAlias('@app/yii'); ?> cron/daily" <?= $currentUser; ?> >/dev/null 2>&1
<?php endif; ?>
</pre>

View File

@ -46,10 +46,10 @@ class InformationMenu extends \humhub\widgets\BaseMenu
]);
$this->addItem([
'label' => Yii::t('AdminModule.information', 'CronJobs'),
'url' => Url::to(['/admin/information/cronjobs']),
'label' => Yii::t('AdminModule.information', 'Background Jobs'),
'url' => Url::to(['/admin/information/background-jobs']),
'sortOrder' => 400,
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'admin' && Yii::$app->controller->id == 'information' && Yii::$app->controller->action->id == 'cronjobs'),
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'admin' && Yii::$app->controller->id == 'information' && Yii::$app->controller->action->id == 'background-jobs'),
]);
$this->addItem([

View File

@ -2,12 +2,16 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\queue\driver;
use humhub\modules\queue\interfaces\QueueInfoInterface;
use Yii;
use yii\db\Expression;
use yii\db\Query;
use yii\queue\db\Queue;
/**
@ -16,12 +20,66 @@ use yii\queue\db\Queue;
* @since 1.2
* @author Luke
*/
class MySQL extends Queue
class MySQL extends Queue implements QueueInfoInterface
{
/**
* @inheritdoc
*/
public $mutex = 'yii\mutex\MysqlMutex';
/**
* @return int the number of waiting jobs in the queue
*/
public function getWaitingJobCount()
{
return (new Query())
->from($this->tableName)
->andWhere(['channel' => $this->channel])
->andWhere(['reserved_at' => null])
->andWhere(['delay' => 0])->count();
}
/**
* @return int the number of delayed jobs in the queue
*/
public function getDelayedJobCount()
{
return (new Query())
->from($this->tableName)
->andWhere(['channel' => $this->channel])
->andWhere(['reserved_at' => null])
->andWhere(['>', 'delay', 0])->count();
}
/**
* @return int the number of reserved jobs in the queue
*/
public function getReservedJobCount()
{
return (new Query())
->from($this->tableName)
->andWhere(['channel' => $this->channel])
->andWhere('[[reserved_at]] is not null')
->andWhere(['done_at' => null])->count();
}
/**
* @return int the number of done jobs in the queue
*/
public function getDoneJobCount()
{
$databaseName = (new Query())->select(new Expression('DATABASE()'))->scalar();
$tableName = Yii::$app->db->schema->getRawTableName($this->tableName);
$total = (new Query())
->select('AUTO_INCREMENT')
->from('INFORMATION_SCHEMA.TABLES')
->where(['TABLE_SCHEMA' => $databaseName, 'TABLE_NAME' => $tableName])
->scalar();
return $total - $this->getWaitingJobCount() - $this->getDelayedJobCount() - $this->getReservedJobCount();
}
}

View File

@ -8,6 +8,7 @@
namespace humhub\modules\queue\driver;
use humhub\modules\queue\interfaces\QueueInfoInterface;
use yii\queue\redis\Queue;
/**
@ -16,7 +17,38 @@ use yii\queue\redis\Queue;
* @since 1.2
* @author Luke
*/
class Redis extends Queue
class Redis extends Queue implements QueueInfoInterface
{
/**
* @return int|null the number of waiting jobs in the queue
*/
public function getWaitingJobCount()
{
return (int)$this->redis->llen($this->channel . ".waiting");
}
/**
* @return int|null the number of delayed jobs in the queue
*/
public function getDelayedJobCount()
{
return (int)$this->redis->zcount($this->channel . ".delayed", '-inf', '+inf');
}
/**
* @return int|null the number of reserved jobs in the queue
*/
public function getReservedJobCount()
{
return (int)$this->redis->zcount($this->channel . ".reserved", '-inf', '+inf');
}
/**
* @return int|null the number of done jobs in the queue
*/
public function getDoneJobCount()
{
$total = $this->redis->get($this->channel . ".message_id");
return $total - $this->getWaitingJobCount() - $this->getDelayedJobCount() - $this->getReservedJobCount();
}
}

View File

@ -0,0 +1,37 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\queue\interfaces;
/**
* QueueInfoInterface
*
* @author Luke
*/
interface QueueInfoInterface
{
/**
* @return int|null the number of waiting jobs in the queue
*/
public function getWaitingJobCount();
/**
* @return int|null the number of delayed jobs in the queue
*/
public function getDelayedJobCount();
/**
* @return int|null the number of reserved jobs in the queue
*/
public function getReservedJobCount();
/**
* @return int|null the number of done jobs in the queue
*/
public function getDoneJobCount();
}

View File

@ -190,7 +190,7 @@ class ThemeHelper
LessHelper::getVariableFile($theme)
);
if (isset($variables['baseTheme']) && isset($themes[$variables['baseTheme']])) {
if (isset($variables['baseTheme']) && isset($themes[$variables['baseTheme']]) && $variables['baseTheme'] !== $theme->name) {
return $themes[$variables['baseTheme']];
}