Validate queue driver configuration before saving

This commit is contained in:
Kijin Sung 2024-10-12 01:35:13 +09:00
parent 0ee9747a22
commit 36af489b15
7 changed files with 80 additions and 6 deletions

View file

@ -43,6 +43,14 @@ interface QueueInterface
*/
public static function isSupported(): bool;
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool;
/**
* Add a task.
*

View file

@ -61,6 +61,17 @@ class DB implements QueueInterface
return true;
}
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool
{
return true;
}
/**
* Constructor.
*

View file

@ -65,6 +65,17 @@ class Dummy implements QueueInterface
return true;
}
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool
{
return true;
}
/**
* Constructor.
*

View file

@ -66,6 +66,37 @@ class Redis implements QueueInterface
return class_exists('\\Redis');
}
/**
* Validate driver configuration.
*
* @param mixed $config
* @return bool
*/
public static function validateConfig($config): bool
{
try
{
$test = new \Redis;
$test->connect($config['host'], $config['port'] ?? 6379);
if (isset($config['user']) || isset($config['pass']))
{
$auth = [];
if (isset($config['user']) && $config['user']) $auth[] = $config['user'];
if (isset($config['pass']) && $config['pass']) $auth[] = $config['pass'];
$test->auth(count($auth) > 1 ? $auth : $auth[0]);
}
if (isset($config['dbnum']))
{
$test->select(intval($config['dbnum']));
}
return true;
}
catch (\Throwable $th)
{
return false;
}
}
/**
* Constructor.
*
@ -77,18 +108,17 @@ class Redis implements QueueInterface
{
$this->_conn = new \Redis;
$this->_conn->connect($config['host'], $config['port'] ?? 6379);
if(isset($config['user']) || isset($config['pass']))
if (isset($config['user']) || isset($config['pass']))
{
$auth = [];
if (isset($config['user']) && $config['user']) $auth[] = $config['user'];
if (isset($config['pass']) && $config['pass']) $auth[] = $config['pass'];
$this->_conn->auth(count($auth) > 1 ? $auth : $auth[0]);
}
if(isset($config['dbnum']))
if (isset($config['dbnum']))
{
$this->_conn->select(intval($config['dbnum']));
}
$this->_key = 'rxQueue_' . substr(hash_hmac('sha1', 'rxQueue_', config('crypto.authentication_key')), 0, 24);
}
catch (\RedisException $e)

View file

@ -68,7 +68,7 @@ class Queue extends Base
}
// Validate required and optional driver settings.
$driver_config = array();
$driver_config = [];
foreach ($drivers[$driver]['required'] as $conf_name)
{
$conf_value = trim($vars->{'queue_' . $driver . '_' . $conf_name} ?? '');
@ -105,6 +105,18 @@ class Queue extends Base
throw new Exception('msg_queue_invalid_key');
}
// Validate actual operation of the driver.
$driver_class = '\\Rhymix\\Framework\\Drivers\\Queue\\' . $driver;
if (!class_exists($driver_class) || !$driver_class::isSupported())
{
throw new Exception('msg_queue_driver_not_found');
}
if (!$driver_class::validateConfig($driver_config))
{
throw new Exception('msg_queue_driver_not_usable');
}
// Save system config.
Config::set("queue.enabled", $enabled);
Config::set("queue.driver", $driver);

View file

@ -306,7 +306,8 @@ $lang->msg_queue_instructions['webcron'] = 'Configure an external cron service t
$lang->msg_queue_instructions['systemd1'] = 'Put the following content in <code>/etc/systemd/system/rhymix-queue.service</code>';
$lang->msg_queue_instructions['systemd2'] = 'Put the following content in <code>/etc/systemd/system/rhymix-queue.timer</code>';
$lang->msg_queue_instructions['systemd3'] = 'Execute the following commands to enable the timer, and monitor your journal to make sure that it is operating as scheduled.';
$lang->msg_queue_driver_not_found = 'Invalid task queue driver';
$lang->msg_queue_driver_not_found = 'The selected task queue driver is not supported on this server.';
$lang->msg_queue_driver_not_usable = 'The selected task queue driver failed to initialize using the configuration values you entered.';
$lang->msg_queue_driver_cannot_be_dummy = 'In otder to use the task queue, you must select a driver other than "Not use"';
$lang->msg_queue_invalid_config = 'Missing or invalid configuration for the selected queue driver.';
$lang->msg_queue_invalid_interval = 'The calling interval must be between 1 and 10 minutes.';

View file

@ -302,7 +302,8 @@ $lang->msg_queue_instructions['webcron'] = '아래의 URL을 1분 간격 또는
$lang->msg_queue_instructions['systemd1'] = '<code>/etc/systemd/system/rhymix-queue.service</code> 파일에 아래와 같은 내용을 넣습니다.';
$lang->msg_queue_instructions['systemd2'] = '<code>/etc/systemd/system/rhymix-queue.timer</code> 파일에 아래와 같은 내용을 넣습니다.';
$lang->msg_queue_instructions['systemd3'] = '아래의 명령을 실행하여 타이머를 활성화하고, 정상 작동하는지 모니터링하십시오.';
$lang->msg_queue_driver_not_found = '지원하지 않는 비동기 드라이버입니다.';
$lang->msg_queue_driver_not_found = '이 서버에서 지원하지 않는 비동기 드라이버입니다.';
$lang->msg_queue_driver_not_usable = '입력하신 정보로 비동기 드라이버와 연동하는 데 실패했습니다. 드라이버 설정을 확인해 주십시오.';
$lang->msg_queue_driver_cannot_be_dummy = '비동기 작업을 사용하려면 "미사용" 이외의 드라이버를 선택해야 합니다.';
$lang->msg_queue_invalid_config = '비동기 드라이버의 필수 설정이 누락되었습니다.';
$lang->msg_queue_invalid_interval = '호출 간격은 1~10분 이내여야 합니다.';