mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00
[mod] document, simplify & pylint searx/shared/__init__.py
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
parent
e03eb1b4f4
commit
0763cec005
1 changed files with 27 additions and 32 deletions
|
@ -1,50 +1,45 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# lint: pylint
|
||||
"""Initialization of a *shared* storage.
|
||||
|
||||
The *shared* :py:obj:`storage` is a dictionary type that is stored:
|
||||
|
||||
- in a redis DB. If a redis DB is not available it is stored
|
||||
- in a uWSGI cache. If no uWSGI is not available it is stored
|
||||
- in a simple python dictionary.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from . import redisdb
|
||||
|
||||
logger = logging.getLogger('searx.shared')
|
||||
|
||||
__all__ = ["storage", "schedule"]
|
||||
|
||||
|
||||
try:
|
||||
if redisdb.init():
|
||||
# First: try to use Redis
|
||||
from .redisdb import client
|
||||
|
||||
client().ping()
|
||||
from .shared_redis import RedisCacheSharedDict as SharedDict, schedule
|
||||
|
||||
logger.info('Use shared_redis implementation')
|
||||
except Exception as e:
|
||||
|
||||
else:
|
||||
# Second: try to use uwsgi
|
||||
|
||||
logger.error("can't connect redis DB, try to init uWSGI cache")
|
||||
try:
|
||||
import uwsgi
|
||||
except:
|
||||
# Third : fall back to shared_simple
|
||||
import uwsgi # type: ignore
|
||||
|
||||
logger.info('Use shared_uwsgi implementation')
|
||||
uwsgi.cache_update('dummy', b'dummy')
|
||||
if uwsgi.cache_get('dummy') != b'dummy':
|
||||
logger.error('found issues when using uWSGI, fall back to python dictionary')
|
||||
from .shared_simple import SimpleSharedDict as SharedDict, schedule
|
||||
|
||||
except ModuleNotFoundError:
|
||||
# Third: fall back to shared_simple
|
||||
|
||||
logger.error("uWSGI is not available, fall back to python dictionary")
|
||||
from .shared_simple import SimpleSharedDict as SharedDict, schedule
|
||||
|
||||
logger.info('Use shared_simple implementation')
|
||||
else:
|
||||
# Make sure uwsgi is okay
|
||||
try:
|
||||
uwsgi.cache_update('dummy', b'dummy')
|
||||
if uwsgi.cache_get('dummy') != b'dummy':
|
||||
raise Exception() # pylint: disable=raise-missing-from
|
||||
except:
|
||||
# there is exception on a get/set test: disable all scheduling
|
||||
logger.exception(
|
||||
'uwsgi.ini configuration error, add this line to your uwsgi.ini\n'
|
||||
'cache2 = name=searxngcache,items=2000,blocks=2000,blocksize=4096,bitmap=1\n'
|
||||
)
|
||||
from .shared_simple import SimpleSharedDict as SharedDict
|
||||
|
||||
def schedule(delay, func, *args):
|
||||
return False
|
||||
|
||||
else:
|
||||
# use uwsgi
|
||||
from .shared_uwsgi import UwsgiCacheSharedDict as SharedDict, schedule
|
||||
|
||||
logger.info('Use shared_uwsgi implementation')
|
||||
|
||||
storage = SharedDict()
|
||||
|
|
Loading…
Add table
Reference in a new issue