summaryrefslogtreecommitdiff
path: root/searx/shared
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2022-11-05 17:50:40 +0100
committerGitHub <noreply@github.com>2022-11-05 17:50:40 +0100
commitd37afb8ab90f75f888179b6efd3e1e8977ef6dea (patch)
tree181d2b38112d9439f836e6d9a286f5355a311ffa /searx/shared
parentd764d94a70b0b10291105a867227975d59af5675 (diff)
parente92755d358df5b34b0181f48f8ba02c7f2939e8f (diff)
Merge pull request #1856 from dalf/checker_requires_redis
The checker requires Redis
Diffstat (limited to 'searx/shared')
-rw-r--r--searx/shared/__init__.py41
-rw-r--r--searx/shared/redisdb.py37
-rw-r--r--searx/shared/shared_abstract.py22
-rw-r--r--searx/shared/shared_simple.py40
-rw-r--r--searx/shared/shared_uwsgi.py64
5 files changed, 25 insertions, 179 deletions
diff --git a/searx/shared/__init__.py b/searx/shared/__init__.py
index d10ddb33d..2c7fc9f8b 100644
--- a/searx/shared/__init__.py
+++ b/searx/shared/__init__.py
@@ -1,39 +1,6 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
+# lint: pylint
+"""Initialization of a *shared* storage.
+"""
-import logging
-import importlib
-
-logger = logging.getLogger('searx.shared')
-
-__all__ = ['SharedDict', 'schedule']
-
-try:
- uwsgi = importlib.import_module('uwsgi')
-except:
- # no uwsgi
- from .shared_simple import SimpleSharedDict as SharedDict, schedule
-
- logger.info('Use shared_simple implementation')
-else:
- try:
- uwsgi.cache_update('dummy', b'dummy')
- if uwsgi.cache_get('dummy') != b'dummy':
- raise Exception()
- except:
- # uwsgi.ini configuration problem: disable all scheduling
- logger.error(
- 'uwsgi.ini configuration error, add this line to your uwsgi.ini\n'
- 'cache2 = name=searxngcache,items=2000,blocks=2000,blocksize=4096,bitmap=1'
- )
- from .shared_simple import SimpleSharedDict as SharedDict
-
- def schedule(delay, func, *args):
- return False
-
- else:
- # uwsgi
- from .shared_uwsgi import UwsgiCacheSharedDict as SharedDict, schedule
-
- logger.info('Use shared_uwsgi implementation')
-
-storage = SharedDict()
+from . import redisdb
diff --git a/searx/shared/redisdb.py b/searx/shared/redisdb.py
index bb7a0eeb4..6cc4e46d1 100644
--- a/searx/shared/redisdb.py
+++ b/searx/shared/redisdb.py
@@ -26,26 +26,31 @@ import redis
from searx import get_setting
-logger = logging.getLogger('searx.shared.redis')
-_client = None
+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():
- global _client # pylint: disable=global-statement
- if _client is None:
- # not thread safe: in the worst case scenario, two or more clients are
- # initialized only one is kept, the others are garbage collected.
- _client = redis.Redis.from_url(get_setting('redis.url'))
- return _client
+def client() -> redis.Redis:
+ return _CLIENT
-def init():
+
+def initialize():
+ global _CLIENT # pylint: disable=global-statement
+ redis_url = get_setting('redis.url')
try:
- c = client()
- logger.info("connected redis DB --> %s", c.acl_whoami())
- return True
- except redis.exceptions.ConnectionError as exc:
+ if redis_url:
+ _CLIENT = redis.Redis.from_url(redis_url)
+ logger.info("connected redis: %s", redis_url)
+ return True
+ except redis.exceptions.ConnectionError:
_pw = pwd.getpwuid(os.getuid())
- logger.error("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid)
- logger.error(" %s", exc)
+ logger.exception("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid)
+ if redis_url == OLD_REDIS_URL_DEFAULT_URL:
+ 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/shared/shared_abstract.py b/searx/shared/shared_abstract.py
deleted file mode 100644
index af4be30ae..000000000
--- a/searx/shared/shared_abstract.py
+++ /dev/null
@@ -1,22 +0,0 @@
-# SPDX-License-Identifier: AGPL-3.0-or-later
-# pyright: strict
-from abc import ABC, abstractmethod
-from typing import Optional
-
-
-class SharedDict(ABC):
- @abstractmethod
- def get_int(self, key: str) -> Optional[int]:
- pass
-
- @abstractmethod
- def set_int(self, key: str, value: int):
- pass
-
- @abstractmethod
- def get_str(self, key: str) -> Optional[str]:
- pass
-
- @abstractmethod
- def set_str(self, key: str, value: str):
- pass
diff --git a/searx/shared/shared_simple.py b/searx/shared/shared_simple.py
deleted file mode 100644
index 2b9d4c2da..000000000
--- a/searx/shared/shared_simple.py
+++ /dev/null
@@ -1,40 +0,0 @@
-# SPDX-License-Identifier: AGPL-3.0-or-later
-
-import threading
-from typing import Optional
-
-from . import shared_abstract
-
-
-class SimpleSharedDict(shared_abstract.SharedDict):
-
- __slots__ = ('d',)
-
- def __init__(self):
- self.d = {}
-
- def get_int(self, key: str) -> Optional[int]:
- return self.d.get(key, None)
-
- def set_int(self, key: str, value: int):
- self.d[key] = value
-
- def get_str(self, key: str) -> Optional[str]:
- return self.d.get(key, None)
-
- def set_str(self, key: str, value: str):
- self.d[key] = value
-
-
-def schedule(delay, func, *args):
- def call_later():
- t = threading.Timer(delay, wrapper)
- t.daemon = True
- t.start()
-
- def wrapper():
- call_later()
- func(*args)
-
- call_later()
- return True
diff --git a/searx/shared/shared_uwsgi.py b/searx/shared/shared_uwsgi.py
deleted file mode 100644
index 0248c6234..000000000
--- a/searx/shared/shared_uwsgi.py
+++ /dev/null
@@ -1,64 +0,0 @@
-# SPDX-License-Identifier: AGPL-3.0-or-later
-
-import time
-from typing import Optional
-import uwsgi # pyright: ignore # pylint: disable=E0401
-from . import shared_abstract
-
-
-_last_signal = 10
-
-
-class UwsgiCacheSharedDict(shared_abstract.SharedDict):
- def get_int(self, key: str) -> Optional[int]:
- value = uwsgi.cache_get(key)
- if value is None:
- return value
- else:
- return int.from_bytes(value, 'big')
-
- def set_int(self, key: str, value: int):
- b = value.to_bytes(4, 'big')
- uwsgi.cache_update(key, b)
-
- def get_str(self, key: str) -> Optional[str]:
- value = uwsgi.cache_get(key)
- if value is None:
- return value
- else:
- return value.decode('utf-8')
-
- def set_str(self, key: str, value: str):
- b = value.encode('utf-8')
- uwsgi.cache_update(key, b)
-
-
-def schedule(delay, func, *args):
- """
- Can be implemented using a spooler.
- https://uwsgi-docs.readthedocs.io/en/latest/PythonDecorators.html
-
- To make the uwsgi configuration simple, use the alternative implementation.
- """
- global _last_signal
-
- def sighandler(signum):
- now = int(time.time())
- key = 'scheduler_call_time_signal_' + str(signum)
- uwsgi.lock()
- try:
- updating = uwsgi.cache_get(key)
- if updating is not None:
- updating = int.from_bytes(updating, 'big')
- if now - updating < delay:
- return
- uwsgi.cache_update(key, now.to_bytes(4, 'big'))
- finally:
- uwsgi.unlock()
- func(*args)
-
- signal_num = _last_signal
- _last_signal += 1
- uwsgi.register_signal(signal_num, 'worker', sighandler)
- uwsgi.add_timer(signal_num, delay)
- return True