| 
									
										
										
										
											2022-01-03 17:43:20 +01:00
										 |  |  | # 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' | 
					
						
							|  |  |  |   >>> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-27 18:43:14 +02:00
										 |  |  | import os | 
					
						
							|  |  |  | import pwd | 
					
						
							| 
									
										
										
										
											2022-01-03 17:43:20 +01:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2022-01-17 17:19:41 +01:00
										 |  |  | import redis | 
					
						
							| 
									
										
										
										
											2022-01-03 17:43:20 +01:00
										 |  |  | from searx import get_setting | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-27 18:43:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 17:43:20 +01:00
										 |  |  | logger = logging.getLogger('searx.shared.redis') | 
					
						
							| 
									
										
										
										
											2022-01-07 17:29:32 +01:00
										 |  |  | _client = None | 
					
						
							| 
									
										
										
										
											2022-01-03 17:43:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def client(): | 
					
						
							| 
									
										
										
										
											2022-01-07 17:29:32 +01:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2022-01-03 17:43:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def init(): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         c = client() | 
					
						
							|  |  |  |         logger.info("connected redis DB --> %s", c.acl_whoami()) | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     except redis.exceptions.ConnectionError as exc: | 
					
						
							| 
									
										
										
										
											2022-05-27 18:43:14 +02:00
										 |  |  |         _pw = pwd.getpwuid(os.getuid()) | 
					
						
							|  |  |  |         logger.error("[%s (%s)] can't connect redis DB ...", _pw.pw_name, _pw.pw_uid) | 
					
						
							| 
									
										
										
										
											2022-01-03 17:43:20 +01:00
										 |  |  |         logger.error("  %s", exc) | 
					
						
							|  |  |  |     return False |