Implement admin config screen for Queue

This commit is contained in:
Kijin Sung 2024-10-10 01:45:14 +09:00
parent d8370ff59b
commit a2a1f3bcc6
6 changed files with 278 additions and 2 deletions

View file

@ -0,0 +1,115 @@
<?php
namespace Rhymix\Modules\Admin\Controllers\SystemConfig;
use Context;
use Rhymix\Framework\Config;
use Rhymix\Framework\Exception;
use Rhymix\Framework\Queue as RFQueue;
use Rhymix\Framework\Security;
use Rhymix\Modules\Admin\Controllers\Base;
class Queue extends Base
{
/**
* Display Notification Settings page
*/
public function dispAdminConfigQueue()
{
// Load queue drivers.
$queue_drivers = RFQueue::getSupportedDrivers();
uasort($queue_drivers, function($a, $b) {
if ($a['name'] === 'Dummy') return -1;
if ($b['name'] === 'Dummy') return 1;
return strnatcasecmp($a['name'], $b['name']);
});
Context::set('queue_drivers', $queue_drivers);
Context::set('queue_driver', config('queue.driver') ?: 'dummy');
// Set the default auth key.
if (!config('queue.key'))
{
config('queue.key', Security::getRandom(32));
}
// Set defaults for Redis.
if (!config('queue.redis'))
{
config('queue.redis', [
'host' => '127.0.0.1',
'port' => '6379',
'dbnum' => 0,
]);
}
$this->setTemplateFile('config_queue');
}
/**
* Update notification configuration.
*/
public function procAdminUpdateQueue()
{
$vars = Context::getRequestVars();
// Enabled?
$enabled = $vars->queue_enabled === 'Y';
// Validate the driver.
$drivers = RFQueue::getSupportedDrivers();
$driver = trim($vars->queue_driver);
if (!array_key_exists($driver, $drivers))
{
throw new Exception('1111');
}
if ($enabled && (!$driver || $driver === 'dummy'))
{
throw new Exception('2222');
}
// Validate required and optional driver settings.
$driver_config = array();
foreach ($drivers[$driver]['required'] as $conf_name)
{
$conf_value = trim($vars->{'queue_' . $driver . '_' . $conf_name} ?? '');
if ($conf_value === '')
{
throw new Exception('3333');
}
$driver_config[$conf_name] = $conf_value === '' ? null : $conf_value;
}
foreach ($drivers[$driver]['optional'] as $conf_name)
{
$conf_value = trim($vars->{'queue_' . $driver . '_' . $conf_name} ?? '');
$driver_config[$conf_name] = $conf_value === '' ? null : $conf_value;
}
// Validate the interval.
$interval = intval($vars->queue_interval ?? 1);
if ($interval < 1 || $interval > 10)
{
throw new Exception('4444');
}
// Validate the key.
$key = trim($vars->queue_key ?? '');
if (strlen($key) < 16 || !ctype_alnum($key))
{
throw new Exception('5555');
}
// Save system config.
Config::set("queue.enabled", $enabled);
Config::set("queue.driver", $driver);
Config::set("queue.interval", $interval);
Config::set("queue.key", $key);
Config::set("queue.$driver", $driver_config);
if (!Config::save())
{
throw new Exception('msg_failed_to_save_config');
}
$this->setMessage('success_updated');
$this->setRedirectUrl(Context::get('success_return_url') ?: getNotEncodedUrl('', 'module', 'admin', 'act', 'dispAdminConfigQueue'));
}
}

View file

@ -283,6 +283,19 @@ $lang->og_extract_images_fallback = 'Use site default image only';
$lang->og_extract_hashtags = 'Extract Hashtags from Document';
$lang->og_use_nick_name = 'Include Author Name';
$lang->og_use_timestamps = 'Include Timestamps';
$lang->cmd_queue_enabled = 'Use Task Queue';
$lang->cmd_queue_enabled_help = 'The task queue will stop accepting new tasks if you uncheck the above.';
$lang->cmd_queue_driver = 'Queue Driver';
$lang->cmd_queue_driver_help = 'Select the driver for the task queue that suits your hosting environment and website needs.<br>Some drivers such as Redis will need the corresponding program to be installed on the server.';
$lang->cmd_queue_interval = 'Calling Interval';
$lang->cmd_queue_interval_help = 'Use a scheduler such as crontab or systemd timer to call the script on a set interval.<br>All tasks are processed as soon as possible regardless of the interval, but a short interval means quick recovery from any error.<br>For web-based cron, this should not exceed the max_execution_time setting in php.ini.<br>The max_execution_time on this server is %d seconds.';
$lang->cmd_queue_call_script = 'Processing Script';
$lang->cmd_queue_webcron_key = 'Webcron Auth Key';
$lang->cmd_queue_config_keys['host'] = 'Host';
$lang->cmd_queue_config_keys['port'] = 'Port';
$lang->cmd_queue_config_keys['user'] = 'User';
$lang->cmd_queue_config_keys['pass'] = 'Password';
$lang->cmd_queue_config_keys['dbnum'] = 'DB Number';
$lang->autoinstall = 'EasyInstall';
$lang->last_week = 'Last Week';
$lang->this_week = 'This Week';

View file

@ -279,6 +279,19 @@ $lang->og_extract_images_fallback = '사이트 대표 이미지 사용';
$lang->og_extract_hashtags = '본문에서 해시태그 추출';
$lang->og_use_nick_name = '글 작성자 이름 표시';
$lang->og_use_timestamps = '글 작성/수정 시각 표시';
$lang->cmd_queue_enabled = '비동기 작업 사용';
$lang->cmd_queue_enabled_help = '체크를 해제하면 더이상 작업을 접수하지 않습니다.';
$lang->cmd_queue_driver = '비동기 드라이버';
$lang->cmd_queue_driver_help = '비동기 작업을 관리할 방법을 설정합니다. 호스팅 환경과 사이트의 필요에 맞추어 선택하세요.<br>Redis 등 일부 드라이버는 서버에 해당 기능이 설치되어 있어야 사용할 수 있습니다.';
$lang->cmd_queue_interval = '호출 간격';
$lang->cmd_queue_interval_help = 'crontab, systemd timer, 웹크론 등을 사용하여 일정한 주기로 스크립트를 호출해 주십시오.<br>모든 비동기 작업은 호출 간격과 무관하게 실시간으로 처리되나, 호출 간격이 짧으면 장애 발생시 신속하게 복구됩니다.<br>웹크론 사용시에는 php.ini의 실행 시간 제한을 초과하지 않는 것이 좋습니다.<br>이 서버의 max_execution_time은 %d초로 설정되어 있습니다.';
$lang->cmd_queue_call_script = '작업 처리 스크립트';
$lang->cmd_queue_webcron_key = '웹크론 인증키';
$lang->cmd_queue_config_keys['host'] = '호스트';
$lang->cmd_queue_config_keys['port'] = '포트';
$lang->cmd_queue_config_keys['user'] = '아이디';
$lang->cmd_queue_config_keys['pass'] = '암호';
$lang->cmd_queue_config_keys['dbnum'] = 'DB 번호';
$lang->autoinstall = '쉬운 설치';
$lang->last_week = '지난주';
$lang->this_week = '이번주';

View file

@ -0,0 +1,119 @@
<config autoescape="on" />
<include target="config_header.html" />
<load target="js/queue_config.js" />
<div cond="!empty($XE_VALIDATOR_MESSAGE) && $XE_VALIDATOR_ID == 'modules/admin/tpl/config_queue/1'" class="message {$XE_VALIDATOR_MESSAGE_TYPE}">
<p>{$XE_VALIDATOR_MESSAGE}</p>
</div>
<script type="text/javascript">
var queue_drivers = {json_encode($queue_drivers)|noescape};
</script>
<form action="./" method="post" class="x_form-horizontal">
<input type="hidden" name="module" value="admin" />
<input type="hidden" name="act" value="procAdminUpdateQueue" />
<input type="hidden" name="xe_validator_id" value="modules/admin/tpl/config_queue/1" />
<section class="section">
<h2>{$lang->subtitle_queue}</h2>
<div class="x_control-group">
<label class="x_control-label">{$lang->cmd_queue_enabled}</label>
<div class="x_controls">
<label for="queue_enabled" class="x_inline">
<input type="checkbox" name="queue_enabled" id="queue_enabled" value="Y" checked="checked"|cond="config('queue.enabled')" />
{$lang->cmd_queue_enabled}
</label>
<br>
<p class="x_help-block">{$lang->cmd_queue_enabled_help}</p>
</div>
</div>
<div class="x_control-group">
<label class="x_control-label" for="queue_driver">{$lang->cmd_queue_driver}</label>
<div class="x_controls">
<select name="queue_driver" id="queue_driver">
<!--@foreach($queue_drivers as $driver_name => $driver_definition)-->
<option value="{$driver_name}" selected="selected"|cond="$queue_driver === $driver_name">{$driver_name === 'dummy' ? $lang->notuse : $driver_definition['name']}</option>
<!--@end-->
</select>
<p class="x_help-block">{$lang->cmd_queue_driver_help}</p>
</div>
</div>
<!--@foreach($queue_drivers as $driver_name => $driver_definition)-->
{@ $conf_names = array_merge($driver_definition['required'], $driver_definition['optional'])}
<!--@foreach($conf_names as $conf_name)-->
{@ $conf_value = escape(config("queue.$driver_name.$conf_name"))}
{@ $text_keys = ['host', 'user']}
{@ $number_keys = ['port', 'dbnum']}
{@ $password_keys = ['pass']}
<!--@if(in_array($conf_name, $text_keys))-->
<div class="x_control-group hidden-by-default show-for-{$driver_name}">
<label class="x_control-label" for="queue_{$driver_name}_{$conf_name}">{$lang->cmd_queue_config_keys[$conf_name]}</label>
<div class="x_controls">
<input type="text" name="queue_{$driver_name}_{$conf_name}" id="queue_{$driver_name}_{$conf_name}" value="{$conf_value}" />
</div>
</div>
<!--@end-->
<!--@if(in_array($conf_name, $number_keys))-->
<div class="x_control-group hidden-by-default show-for-{$driver_name}">
<label class="x_control-label" for="queue_{$driver_name}_{$conf_name}">{$lang->cmd_queue_config_keys[$conf_name]}</label>
<div class="x_controls">
<input type="number" name="queue_{$driver_name}_{$conf_name}" id="queue_{$driver_name}_{$conf_name}" value="{$conf_value}" />
</div>
</div>
<!--@end-->
<!--@if(in_array($conf_name, $password_keys))-->
<div class="x_control-group hidden-by-default show-for-{$driver_name}">
<label class="x_control-label" for="queue_{$driver_name}_{$conf_name}">{$lang->cmd_queue_config_keys[$conf_name]}</label>
<div class="x_controls">
<input type="password" name="queue_{$driver_name}_{$conf_name}" id="queue_{$driver_name}_{$conf_name}" value="{$conf_value}" autocomplete="new-password" />
</div>
</div>
<!--@end-->
<!--@end-->
<!--@end-->
</section>
<section>
<h2>{$lang->cmd_queue_call_script}</h2>
<div class="x_control-group">
<label class="x_control-label" for="queue_key">{$lang->cmd_queue_webcron_key}</label>
<div class="x_controls">
<input type="text" class="x_full-width" name="queue_key" id="queue_key" value="{config('queue.key')}" />
</div>
</div>
<div class="x_control-group">
<label class="x_control-label" for="queue_interval">{$lang->cmd_queue_interval}</label>
<div class="x_controls">
<input type="number" name="queue_interval" id="queue_interval" min="1" max="10" value="{config('queue.interval') ?: 1}" />
<span class="x_inline">{$lang->unit_min}</span>
<br>
<p class="x_help-block" style="margin-top:10px">{sprintf($lang->cmd_queue_interval_help, ini_get('max_execution_time'))|noescape}</p>
</div>
</div>
</section>
<div class="x_clearfix btnArea">
<div class="x_pull-right">
<button type="submit" class="x_btn x_btn-primary">{$lang->cmd_save}</button>
</div>
</div>
</form>

View file

@ -0,0 +1,16 @@
(function($) {
$(function() {
$("#queue_driver").on("change", function() {
var selected_driver = $(this).val();
$(this).parents("section").find("div.x_control-group.hidden-by-default, p.x_help-block.hidden-by-default").each(function() {
if ($(this).hasClass("show-for-" + selected_driver)) {
$(this).show();
} else {
$(this).hide();
}
});
}).triggerHandler("change");
});
})(jQuery);