2021-01-05 10:24:39 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-05-05 14:47:02 +00:00
|
|
|
# lint: pylint
|
2021-09-07 11:34:35 +00:00
|
|
|
# pylint: disable=missing-module-docstring
|
2022-07-15 16:38:32 +00:00
|
|
|
# pyright: basic
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import os
|
|
|
|
import signal
|
2022-07-15 16:38:32 +00:00
|
|
|
from typing import Dict, Union, List, Any, Tuple, Optional
|
2022-01-24 08:46:32 +00:00
|
|
|
from typing_extensions import TypedDict, Literal
|
2021-01-05 10:24:39 +00:00
|
|
|
|
2022-07-15 16:38:32 +00:00
|
|
|
import redis.exceptions
|
|
|
|
|
2021-01-05 10:24:39 +00:00
|
|
|
from searx import logger, settings, searx_debug
|
2022-11-11 20:58:32 +00:00
|
|
|
from searx.redisdb import client as get_redis_client
|
2021-01-05 10:24:39 +00:00
|
|
|
from searx.exceptions import SearxSettingsException
|
2021-05-05 11:08:54 +00:00
|
|
|
from searx.search.processors import PROCESSORS
|
2021-01-05 10:24:39 +00:00
|
|
|
from searx.search.checker import Checker
|
2022-07-15 16:38:32 +00:00
|
|
|
from searx.search.checker.scheduler import scheduler_function
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
|
2022-07-15 16:38:32 +00:00
|
|
|
REDIS_RESULT_KEY = 'SearXNG_checker_result'
|
|
|
|
REDIS_LOCK_KEY = 'SearXNG_checker_lock'
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
|
2022-01-24 08:46:32 +00:00
|
|
|
CheckerResult = Union['CheckerOk', 'CheckerErr', 'CheckerOther']
|
|
|
|
|
|
|
|
|
|
|
|
class CheckerOk(TypedDict):
|
|
|
|
"""Checking the engines succeeded"""
|
|
|
|
|
|
|
|
status: Literal['ok']
|
|
|
|
engines: Dict[str, 'EngineResult']
|
|
|
|
timestamp: int
|
|
|
|
|
|
|
|
|
|
|
|
class CheckerErr(TypedDict):
|
|
|
|
"""Checking the engines failed"""
|
|
|
|
|
|
|
|
status: Literal['error']
|
|
|
|
timestamp: int
|
|
|
|
|
|
|
|
|
|
|
|
class CheckerOther(TypedDict):
|
|
|
|
"""The status is unknown or disabled"""
|
|
|
|
|
|
|
|
status: Literal['unknown', 'disabled']
|
|
|
|
|
|
|
|
|
|
|
|
EngineResult = Union['EngineOk', 'EngineErr']
|
|
|
|
|
|
|
|
|
|
|
|
class EngineOk(TypedDict):
|
|
|
|
"""Checking the engine succeeded"""
|
|
|
|
|
|
|
|
success: Literal[True]
|
|
|
|
|
|
|
|
|
|
|
|
class EngineErr(TypedDict):
|
|
|
|
"""Checking the engine failed"""
|
|
|
|
|
|
|
|
success: Literal[False]
|
|
|
|
errors: Dict[str, List[str]]
|
|
|
|
|
|
|
|
|
|
|
|
def _get_interval(every: Any, error_msg: str) -> Tuple[int, int]:
|
2021-01-05 10:24:39 +00:00
|
|
|
if isinstance(every, int):
|
2022-01-24 08:46:32 +00:00
|
|
|
return (every, every)
|
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
if (
|
|
|
|
not isinstance(every, (tuple, list))
|
2022-01-24 08:46:32 +00:00
|
|
|
or len(every) != 2 # type: ignore
|
2021-12-27 08:26:22 +00:00
|
|
|
or not isinstance(every[0], int)
|
|
|
|
or not isinstance(every[1], int)
|
|
|
|
):
|
2021-01-05 10:24:39 +00:00
|
|
|
raise SearxSettingsException(error_msg, None)
|
2022-01-24 08:46:32 +00:00
|
|
|
return (every[0], every[1])
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
|
2022-01-24 08:46:32 +00:00
|
|
|
def get_result() -> CheckerResult:
|
2022-07-15 16:38:32 +00:00
|
|
|
client = get_redis_client()
|
|
|
|
if client is None:
|
|
|
|
# without Redis, the checker is disabled
|
|
|
|
return {'status': 'disabled'}
|
|
|
|
serialized_result: Optional[bytes] = client.get(REDIS_RESULT_KEY)
|
|
|
|
if serialized_result is None:
|
|
|
|
# the Redis key does not exist
|
|
|
|
return {'status': 'unknown'}
|
|
|
|
return json.loads(serialized_result)
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
|
2022-01-24 08:46:32 +00:00
|
|
|
def _set_result(result: CheckerResult):
|
2022-07-15 16:38:32 +00:00
|
|
|
client = get_redis_client()
|
|
|
|
if client is None:
|
|
|
|
# without Redis, the function does nothing
|
|
|
|
return
|
|
|
|
client.set(REDIS_RESULT_KEY, json.dumps(result))
|
2021-01-11 17:43:12 +00:00
|
|
|
|
|
|
|
|
2022-01-24 08:46:32 +00:00
|
|
|
def _timestamp():
|
|
|
|
return int(time.time() / 3600) * 3600
|
|
|
|
|
|
|
|
|
2021-01-05 10:24:39 +00:00
|
|
|
def run():
|
|
|
|
try:
|
2022-07-15 16:38:32 +00:00
|
|
|
# use a Redis lock to make sure there is no checker running at the same time
|
|
|
|
# (this should not happen, this is a safety measure)
|
|
|
|
with get_redis_client().lock(REDIS_LOCK_KEY, blocking_timeout=60, timeout=3600):
|
|
|
|
logger.info('Starting checker')
|
|
|
|
result: CheckerOk = {'status': 'ok', 'engines': {}, 'timestamp': _timestamp()}
|
|
|
|
for name, processor in PROCESSORS.items():
|
|
|
|
logger.debug('Checking %s engine', name)
|
|
|
|
checker = Checker(processor)
|
|
|
|
checker.run()
|
|
|
|
if checker.test_results.successful:
|
|
|
|
result['engines'][name] = {'success': True}
|
|
|
|
else:
|
|
|
|
result['engines'][name] = {'success': False, 'errors': checker.test_results.errors}
|
|
|
|
|
|
|
|
_set_result(result)
|
|
|
|
logger.info('Check done')
|
|
|
|
except redis.exceptions.LockError:
|
|
|
|
_set_result({'status': 'error', 'timestamp': _timestamp()})
|
|
|
|
logger.exception('Error while running the checker')
|
2021-05-05 14:47:02 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2022-01-24 08:46:32 +00:00
|
|
|
_set_result({'status': 'error', 'timestamp': _timestamp()})
|
2021-01-11 17:43:12 +00:00
|
|
|
logger.exception('Error while running the checker')
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
|
2022-01-24 08:46:32 +00:00
|
|
|
def _signal_handler(_signum: int, _frame: Any):
|
2021-01-05 10:24:39 +00:00
|
|
|
t = threading.Thread(target=run)
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
|
|
|
def initialize():
|
2021-03-14 21:58:59 +00:00
|
|
|
if hasattr(signal, 'SIGUSR1'):
|
|
|
|
# Windows doesn't support SIGUSR1
|
|
|
|
logger.info('Send SIGUSR1 signal to pid %i to start the checker', os.getpid())
|
|
|
|
signal.signal(signal.SIGUSR1, _signal_handler)
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
# special case when debug is activate
|
2022-07-15 16:38:32 +00:00
|
|
|
if searx_debug and settings['checker']['off_when_debug']:
|
2021-01-05 10:24:39 +00:00
|
|
|
logger.info('debug mode: checker is disabled')
|
|
|
|
return
|
|
|
|
|
|
|
|
# check value of checker.scheduling.every now
|
2022-07-15 16:38:32 +00:00
|
|
|
scheduling = settings['checker']['scheduling']
|
2021-01-05 10:24:39 +00:00
|
|
|
if scheduling is None or not scheduling:
|
|
|
|
logger.info('Checker scheduler is disabled')
|
|
|
|
return
|
|
|
|
|
2022-07-15 16:38:32 +00:00
|
|
|
# make sure there is a Redis connection
|
|
|
|
if get_redis_client() is None:
|
|
|
|
logger.error('The checker requires Redis')
|
|
|
|
return
|
2021-01-11 17:43:12 +00:00
|
|
|
|
2022-07-15 16:38:32 +00:00
|
|
|
# start the background scheduler
|
|
|
|
every_range = _get_interval(scheduling.get('every', (300, 1800)), 'checker.scheduling.every is not a int or list')
|
|
|
|
start_after_range = _get_interval(
|
|
|
|
scheduling.get('start_after', (300, 1800)), 'checker.scheduling.start_after is not a int or list'
|
|
|
|
)
|
|
|
|
t = threading.Thread(
|
|
|
|
target=scheduler_function,
|
|
|
|
args=(start_after_range[0], start_after_range[1], every_range[0], every_range[1], run),
|
|
|
|
name='checker_scheduler',
|
|
|
|
)
|
2021-01-05 10:24:39 +00:00
|
|
|
t.daemon = True
|
|
|
|
t.start()
|