Skip to content

Commit

Permalink
Merge pull request #686 from return42/lib_redis
Browse files Browse the repository at this point in the history
Add redis DB and connector
  • Loading branch information
return42 committed Jan 11, 2022
2 parents f400413 + dca8394 commit 977e9a4
Show file tree
Hide file tree
Showing 10 changed files with 491 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ test.shell:
utils/lib_nvm.sh \
utils/lib_static.sh \
utils/lib_go.sh \
utils/lib_redis.sh \
utils/filtron.sh \
utils/searx.sh \
utils/morty.sh \
Expand Down
30 changes: 30 additions & 0 deletions docs/admin/engines/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,36 @@ Global Settings
``default_http_headers``:
Set additional HTTP headers, see `#755 <https://github.com/searx/searx/issues/715>`__


.. _settings redis:

``redis:``
----------

.. _Redis.from_url(url): https://redis-py.readthedocs.io/en/stable/connections.html#redis.client.Redis.from_url

``url``
URL to connect redis database, see `Redis.from_url(url)`_ & :ref:`redis db`::

redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[[username]:[password]]@/path/to/socket.sock?db=0

.. admonition:: Tip for developers

To set up a redis instance simply use::

$ ./manage redis.build
$ sudo -H ./manage redis.install

To get access rights to this instance, your developer account needs to be
added to the *searxng-redis* group::

$ sudo -H ./manage redis.addgrp "${USER}"
# don't forget to logout & login to get member of group

.. _settings outgoing:

``outgoing:``
-------------

Expand Down
8 changes: 8 additions & 0 deletions docs/src/searx.shared.redisdb.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. _redis db:

========
Redis DB
========

.. automodule:: searx.shared.redisdb
:members:
10 changes: 7 additions & 3 deletions manage
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_static.sh"
# shellcheck source=utils/lib_go.sh
source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_go.sh"

# shellcheck source=utils/lib_redis.sh
source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_redis.sh"

# config

PYOBJECTS="searx"
Expand Down Expand Up @@ -74,9 +77,10 @@ docker.:
gecko.driver:
download & install geckodriver if not already installed (required for
robot_tests)
EOF
nvm.help
cat <<EOF
redis:
build : build redis binaries at $(redis._get_dist)
install : create user (${REDIS_USER}) and install systemd service (${REDIS_SERVICE_NAME})
help : show more redis commands
node.:
env : download & install npm dependencies locally
clean : drop locally npm installations
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ uvloop==0.16.0
httpx-socks[asyncio]==0.7.2
langdetect==1.0.9
setproctitle==1.2.2
redis==4.1.0
4 changes: 4 additions & 0 deletions searx/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ server:
X-Robots-Tag: noindex, nofollow
Referrer-Policy: no-referrer

redis:
# https://redis-py.readthedocs.io/en/stable/connections.html#redis.client.Redis.from_url
url: unix:///usr/local/searxng-redis/run/redis.sock?db=0

ui:
# Custom static path - leave it blank if you didn't change
static_path: ""
Expand Down
3 changes: 3 additions & 0 deletions searx/settings_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def apply_schema(settings, schema, path_list):
'method': SettingsValue(('POST', 'GET'), 'POST'),
'default_http_headers': SettingsValue(dict, {}),
},
'redis': {
'url': SettingsValue(str, 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'),
},
'ui': {
'static_path': SettingsDirectoryValue(str, os.path.join(searx_dir, 'static')),
'templates_path': SettingsDirectoryValue(str, os.path.join(searx_dir, 'templates')),
Expand Down
47 changes: 47 additions & 0 deletions searx/shared/redisdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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 logging
import redis
from searx import get_setting

logger = logging.getLogger('searx.shared.redis')
_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'))
return _client


def init():
try:
c = client()
logger.info("connected redis DB --> %s", c.acl_whoami())
return True
except redis.exceptions.ConnectionError as exc:
logger.error("can't connet redis DB ...")
logger.error(" %s", exc)
return False

0 comments on commit 977e9a4

Please sign in to comment.