From 34e260f88f561cf26454fe72a10a3a403faf2ff3 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Fri, 11 Nov 2022 20:14:47 +0100 Subject: [fix] follow up of PR-1856 - the environment variable SEARXNG_REDIS_URL overrides the setting value redis.url - ./manage sets SEARXNG_REDIS_URL to unix:///usr/local/searxng-redis/run/redis.sock if: - the socket exists - SEARXNG_REDIS_URL is not already defined Update of PR #1856 Co-authored-by: Markus Heiser --- searx/settings_defaults.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'searx') diff --git a/searx/settings_defaults.py b/searx/settings_defaults.py index 470290f92..cfa1bb47c 100644 --- a/searx/settings_defaults.py +++ b/searx/settings_defaults.py @@ -174,7 +174,7 @@ SCHEMA = { 'default_http_headers': SettingsValue(dict, {}), }, 'redis': { - 'url': SettingsValue((None, False, str), False), + 'url': SettingsValue((None, False, str), False, 'SEARXNG_REDIS_URL'), }, 'ui': { 'static_path': SettingsDirectoryValue(str, os.path.join(searx_dir, 'static')), -- cgit v1.2.3 From b971167ced9623c038188ae7bf5a8085147bc35d Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Fri, 11 Nov 2022 21:58:32 +0100 Subject: move searx.shared.redisdb to searx.redisdb --- searx/plugins/limiter.py | 2 +- searx/redisdb.py | 70 ++++++++++++++++++++++++++++++++++++++ searx/search/checker/background.py | 2 +- searx/search/checker/scheduler.py | 2 +- searx/shared/__init__.py | 6 ---- searx/shared/redisdb.py | 70 -------------------------------------- searx/webapp.py | 2 +- 7 files changed, 74 insertions(+), 80 deletions(-) create mode 100644 searx/redisdb.py delete mode 100644 searx/shared/__init__.py delete mode 100644 searx/shared/redisdb.py (limited to 'searx') diff --git a/searx/plugins/limiter.py b/searx/plugins/limiter.py index c11fd506b..b66a0805c 100644 --- a/searx/plugins/limiter.py +++ b/searx/plugins/limiter.py @@ -16,7 +16,7 @@ Enable the plugin in ``settings.yml``: import re from flask import request -from searx.shared import redisdb +from searx import redisdb from searx.redislib import incr_sliding_window name = "Request limiter" diff --git a/searx/redisdb.py b/searx/redisdb.py new file mode 100644 index 000000000..0544d697f --- /dev/null +++ b/searx/redisdb.py @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Implementation of the redis client (redis-py_). + +.. _redis-py: https://github.com/redis/redis-py + +This implementation uses the :ref:`settings redis` setup from ``settings.yml``. +A redis DB connect can be tested by:: + + >>> from searx import redisdb + >>> redisdb.initialize() + True + >>> db = redisdb.client() + >>> db.set("foo", "bar") + True + >>> db.get("foo") + b'bar' + >>> + +""" + +import os +import pwd +import logging +import redis +from searx import get_setting + + +OLD_REDIS_URL_DEFAULT_URL = 'unix:///usr/local/searxng-redis/run/redis.sock?db=0' +"""This was the default Redis URL in settings.yml.""" + +_CLIENT = None +logger = logging.getLogger(__name__) + + +def client() -> redis.Redis: + return _CLIENT + + +def initialize(): + global _CLIENT # pylint: disable=global-statement + redis_url = get_setting('redis.url') + if not redis_url: + return False + try: + # create a client, but no connection is done + _CLIENT = redis.Redis.from_url(redis_url) + + # log the parameters as seen by the redis lib, without the password + kwargs = _CLIENT.get_connection_kwargs().copy() + kwargs.pop('password', None) + kwargs = ' '.join([f'{k}={v!r}' for k, v in kwargs.items()]) + logger.info("connecting to Redis %s", kwargs) + + # check the connection + _CLIENT.ping() + + # no error: the redis connection is working + logger.info("connected to Redis") + return True + except redis.exceptions.RedisError as e: + _CLIENT = None + _pw = pwd.getpwuid(os.getuid()) + logger.exception("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid) + if redis_url == OLD_REDIS_URL_DEFAULT_URL and isinstance(e, redis.exceptions.ConnectionError): + logger.info( + "You can safely ignore the above Redis error if you don't use Redis. " + "You can remove this error by setting redis.url to false in your settings.yml." + ) + return False diff --git a/searx/search/checker/background.py b/searx/search/checker/background.py index e5bd642c0..aec2a1790 100644 --- a/searx/search/checker/background.py +++ b/searx/search/checker/background.py @@ -14,7 +14,7 @@ from typing_extensions import TypedDict, Literal import redis.exceptions from searx import logger, settings, searx_debug -from searx.shared.redisdb import client as get_redis_client +from searx.redisdb import client as get_redis_client from searx.exceptions import SearxSettingsException from searx.search.processors import PROCESSORS from searx.search.checker import Checker diff --git a/searx/search/checker/scheduler.py b/searx/search/checker/scheduler.py index 1ae635951..cc3bb7380 100644 --- a/searx/search/checker/scheduler.py +++ b/searx/search/checker/scheduler.py @@ -17,7 +17,7 @@ import time import importlib from typing import Callable -from searx.shared.redisdb import client as get_redis_client +from searx.redisdb import client as get_redis_client from searx.redislib import lua_script_storage diff --git a/searx/shared/__init__.py b/searx/shared/__init__.py deleted file mode 100644 index 2c7fc9f8b..000000000 --- a/searx/shared/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# SPDX-License-Identifier: AGPL-3.0-or-later -# lint: pylint -"""Initialization of a *shared* storage. -""" - -from . import redisdb diff --git a/searx/shared/redisdb.py b/searx/shared/redisdb.py deleted file mode 100644 index d8e29d911..000000000 --- a/searx/shared/redisdb.py +++ /dev/null @@ -1,70 +0,0 @@ -# SPDX-License-Identifier: AGPL-3.0-or-later -# lint: pylint -"""Implementation of the redis client (redis-py_). - -.. _redis-py: https://github.com/redis/redis-py - -This implementation uses the :ref:`settings redis` setup from ``settings.yml``. -A redis DB connect can be tested by:: - - >>> from searx.shared import redisdb - >>> redisdb.init() - True - >>> db = redisdb.client() - >>> db.set("foo", "bar") - True - >>> db.get("foo") - b'bar' - >>> - -""" - -import os -import pwd -import logging -import redis -from searx import get_setting - - -OLD_REDIS_URL_DEFAULT_URL = 'unix:///usr/local/searxng-redis/run/redis.sock?db=0' -"""This was the default Redis URL in settings.yml.""" - -_CLIENT = None -logger = logging.getLogger('searx.shared.redisdb') - - -def client() -> redis.Redis: - return _CLIENT - - -def initialize(): - global _CLIENT # pylint: disable=global-statement - redis_url = get_setting('redis.url') - if not redis_url: - return False - try: - # create a client, but no connection is done - _CLIENT = redis.Redis.from_url(redis_url) - - # log the parameters as seen by the redis lib, without the password - kwargs = _CLIENT.get_connection_kwargs().copy() - kwargs.pop('password', None) - kwargs = ' '.join([f'{k}={v!r}' for k, v in kwargs.items()]) - logger.info("connecting to Redis %s", kwargs) - - # check the connection - _CLIENT.ping() - - # no error: the redis connection is working - logger.info("connected to Redis") - return True - except redis.exceptions.RedisError as e: - _CLIENT = None - _pw = pwd.getpwuid(os.getuid()) - logger.exception("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid) - if redis_url == OLD_REDIS_URL_DEFAULT_URL and isinstance(e, redis.exceptions.ConnectionError): - logger.info( - "You can safely ignore the above Redis error if you don't use Redis. " - "You can remove this error by setting redis.url to false in your settings.yml." - ) - return False diff --git a/searx/webapp.py b/searx/webapp.py index 4f334a9d0..d4206ca16 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -120,7 +120,7 @@ from searx.locales import ( # renaming names from searx imports ... from searx.autocomplete import search_autocomplete, backends as autocomplete_backends from searx.languages import language_codes as languages -from searx.shared.redisdb import initialize as redis_initialize +from searx.redisdb import initialize as redis_initialize from searx.search import SearchWithPlugins, initialize as search_initialize from searx.network import stream as http_stream, set_context_network_name from searx.search.checker import get_result as checker_get_result -- cgit v1.2.3