summaryrefslogtreecommitdiff
path: root/searx/shared
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2022-07-15 18:38:32 +0200
committerAlexandre Flament <alex@al-f.net>2022-11-05 12:04:50 +0100
commitfe419e355bf1527c51e3aee98495d08b89510320 (patch)
tree0ed03d5e3d77e68ac5a5834e5890f5f27fe29fe7 /searx/shared
parentd764d94a70b0b10291105a867227975d59af5675 (diff)
downloadsearxng-fe419e355bf1527c51e3aee98495d08b89510320.tar.gz
searxng-fe419e355bf1527c51e3aee98495d08b89510320.zip
The checker requires Redis
Remove the abstraction in searx.shared.SharedDict. Implement a basic and dedicated scheduler for the checker using a Redis script.
Diffstat (limited to 'searx/shared')
-rw-r--r--searx/shared/__init__.py41
-rw-r--r--searx/shared/redisdb.py18
-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, 10 insertions, 175 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..d0071f72c 100644
--- a/searx/shared/redisdb.py
+++ b/searx/shared/redisdb.py
@@ -26,26 +26,20 @@ import redis
from searx import get_setting
-logger = logging.getLogger('searx.shared.redis')
+logger = logging.getLogger('searx.shared.redisdb')
_client = None
-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'))
+def client() -> redis.Redis:
return _client
-def init():
+def initialize():
+ global _client # pylint: disable=global-statement
try:
- c = client()
- logger.info("connected redis DB --> %s", c.acl_whoami())
- return True
+ _client = redis.Redis.from_url(get_setting('redis.url'))
+ logger.info("connected redis: %s", get_setting('redis.url'))
except redis.exceptions.ConnectionError as exc:
_pw = pwd.getpwuid(os.getuid())
logger.error("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid)
logger.error(" %s", exc)
- 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