offline.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. import threading
  3. from time import time
  4. from searx import logger
  5. from searx.metrology.error_recorder import record_exception, record_error
  6. from searx.search.processors.abstract import EngineProcessor
  7. logger = logger.getChild('search.processor.offline')
  8. class OfflineProcessor(EngineProcessor):
  9. engine_type = 'offline'
  10. def _record_stats_on_error(self, result_container, start_time):
  11. engine_time = time() - start_time
  12. result_container.add_timing(self.engine_name, engine_time, engine_time)
  13. with threading.RLock():
  14. self.engine.stats['errors'] += 1
  15. def _search_basic(self, query, params):
  16. return self.engine.search(query, params)
  17. def search(self, query, params, result_container, start_time, timeout_limit):
  18. try:
  19. search_results = self._search_basic(query, params)
  20. if search_results:
  21. result_container.extend(self.engine_name, search_results)
  22. engine_time = time() - start_time
  23. result_container.add_timing(self.engine_name, engine_time, engine_time)
  24. with threading.RLock():
  25. self.engine.stats['engine_time'] += engine_time
  26. self.engine.stats['engine_time_count'] += 1
  27. except ValueError as e:
  28. record_exception(self.engine_name, e)
  29. self._record_stats_on_error(result_container, start_time)
  30. logger.exception('engine {0} : invalid input : {1}'.format(self.engine_name, e))
  31. except Exception as e:
  32. record_exception(self.engine_name, e)
  33. self._record_stats_on_error(result_container, start_time)
  34. result_container.add_unresponsive_engine(self.engine_name, 'unexpected crash', str(e))
  35. logger.exception('engine {0} : exception : {1}'.format(self.engine_name, e))
  36. else:
  37. if getattr(threading.current_thread(), '_timeout', False):
  38. record_error(self.engine_name, 'Timeout')