redisdb.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Implementation of the redis client (redis-py_).
  4. .. _redis-py: https://github.com/redis/redis-py
  5. This implementation uses the :ref:`settings redis` setup from ``settings.yml``.
  6. A redis DB connect can be tested by::
  7. >>> from searx.shared import redisdb
  8. >>> redisdb.init()
  9. True
  10. >>> db = redisdb.client()
  11. >>> db.set("foo", "bar")
  12. True
  13. >>> db.get("foo")
  14. b'bar'
  15. >>>
  16. """
  17. import os
  18. import pwd
  19. import logging
  20. import redis
  21. from searx import get_setting
  22. OLD_REDIS_URL_DEFAULT_URL = 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'
  23. """This was the default Redis URL in settings.yml."""
  24. _CLIENT = None
  25. logger = logging.getLogger('searx.shared.redisdb')
  26. def client() -> redis.Redis:
  27. return _CLIENT
  28. def initialize():
  29. global _CLIENT # pylint: disable=global-statement
  30. redis_url = get_setting('redis.url')
  31. if not redis_url:
  32. return False
  33. try:
  34. # create a client, but no connection is done
  35. _CLIENT = redis.Redis.from_url(redis_url)
  36. # log the parameters as seen by the redis lib, without the password
  37. kwargs = _CLIENT.get_connection_kwargs().copy()
  38. kwargs.pop('password', None)
  39. kwargs = ' '.join([f'{k}={v!r}' for k, v in kwargs.items()])
  40. logger.info("connecting to Redis %s", kwargs)
  41. # check the connection
  42. _CLIENT.ping()
  43. # no error: the redis connection is working
  44. logger.info("connected to Redis")
  45. return True
  46. except redis.exceptions.RedisError as e:
  47. _CLIENT = None
  48. _pw = pwd.getpwuid(os.getuid())
  49. logger.exception("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid)
  50. if redis_url == OLD_REDIS_URL_DEFAULT_URL and isinstance(e, redis.exceptions.ConnectionError):
  51. logger.info(
  52. "You can safely ignore the above Redis error if you don't use Redis. "
  53. "You can remove this error by setting redis.url to false in your settings.yml."
  54. )
  55. return False