background.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. import json
  3. import random
  4. import time
  5. import threading
  6. import os
  7. import signal
  8. from searx import logger, settings, searx_debug
  9. from searx.exceptions import SearxSettingsException
  10. from searx.search.processors import processors
  11. from searx.search.checker import Checker
  12. from searx.shared import schedule, storage
  13. CHECKER_RESULT = 'CHECKER_RESULT'
  14. running = threading.Lock()
  15. def _get_interval(every, error_msg):
  16. if isinstance(every, int):
  17. every = (every, every)
  18. if not isinstance(every, (tuple, list))\
  19. or len(every) != 2\
  20. or not isinstance(every[0], int)\
  21. or not isinstance(every[1], int):
  22. raise SearxSettingsException(error_msg, None)
  23. return every
  24. def _get_every():
  25. every = settings.get('checker', {}).get('scheduling', {}).get('every', (300, 1800))
  26. return _get_interval(every, 'checker.scheduling.every is not a int or list')
  27. def get_result():
  28. serialized_result = storage.get_str(CHECKER_RESULT)
  29. if serialized_result is not None:
  30. return json.loads(serialized_result)
  31. def _set_result(result, include_timestamp=True):
  32. if include_timestamp:
  33. result['timestamp'] = int(time.time() / 3600) * 3600
  34. storage.set_str(CHECKER_RESULT, json.dumps(result))
  35. def run():
  36. if not running.acquire(blocking=False):
  37. return
  38. try:
  39. logger.info('Starting checker')
  40. result = {
  41. 'status': 'ok',
  42. 'engines': {}
  43. }
  44. for name, processor in processors.items():
  45. logger.debug('Checking %s engine', name)
  46. checker = Checker(processor)
  47. checker.run()
  48. if checker.test_results.successful:
  49. result['engines'][name] = {'success': True}
  50. else:
  51. result['engines'][name] = {'success': False, 'errors': checker.test_results.errors}
  52. _set_result(result)
  53. logger.info('Check done')
  54. except Exception:
  55. _set_result({'status': 'error'})
  56. logger.exception('Error while running the checker')
  57. finally:
  58. running.release()
  59. def _run_with_delay():
  60. every = _get_every()
  61. delay = random.randint(0, every[1] - every[0])
  62. logger.debug('Start checker in %i seconds', delay)
  63. time.sleep(delay)
  64. run()
  65. def _start_scheduling():
  66. every = _get_every()
  67. if schedule(every[0], _run_with_delay):
  68. run()
  69. def _signal_handler(signum, frame):
  70. t = threading.Thread(target=run)
  71. t.daemon = True
  72. t.start()
  73. def initialize():
  74. if hasattr(signal, 'SIGUSR1'):
  75. # Windows doesn't support SIGUSR1
  76. logger.info('Send SIGUSR1 signal to pid %i to start the checker', os.getpid())
  77. signal.signal(signal.SIGUSR1, _signal_handler)
  78. # disabled by default
  79. _set_result({'status': 'disabled'}, include_timestamp=False)
  80. # special case when debug is activate
  81. if searx_debug and settings.get('checker', {}).get('off_when_debug', True):
  82. logger.info('debug mode: checker is disabled')
  83. return
  84. # check value of checker.scheduling.every now
  85. scheduling = settings.get('checker', {}).get('scheduling', None)
  86. if scheduling is None or not scheduling:
  87. logger.info('Checker scheduler is disabled')
  88. return
  89. #
  90. _set_result({'status': 'unknown'}, include_timestamp=False)
  91. start_after = scheduling.get('start_after', (300, 1800))
  92. start_after = _get_interval(start_after, 'checker.scheduling.start_after is not a int or list')
  93. delay = random.randint(start_after[0], start_after[1])
  94. logger.info('Start checker in %i seconds', delay)
  95. t = threading.Timer(delay, _start_scheduling)
  96. t.daemon = True
  97. t.start()