This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[enh] add checker #2419
Merged
Merged
[enh] add checker #2419
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8cbc9f2
[enh] add checker
dalf 16a889d
[enh] checker: add rosebud test
dalf ca0889d
[enh] checker: wikidata & ddd: add specific tests
dalf 9c58146
[fix] do not colorize output on dumb terminals
return42 6e2872f
[enh] add searx.shared
dalf 3a9f513
[enh] checker: background check
dalf 45bfab7
|mod] checker: improve searx-checker command line
dalf f3e1bd3
[mod] checker: minor adjustements on the default tests
dalf 87bafbc
[mod] checker: add status and timestamp to the result
dalf a0c8b41
[mod] searx.shared: minor tweaks
dalf 7f0c508
[fix] checker: fix typo unknown instead of unknow
dalf 912c7e9
[fix] checker: don't run the checker when uwsgi is not properly confi…
dalf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ pygments==2.1.3 | |
python-dateutil==2.8.1 | ||
pyyaml==5.3.1 | ||
requests[socks]==2.25.1 | ||
pycld3==0.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
from .impl import Checker | ||
from .background import initialize, get_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
import sys | ||
import io | ||
import os | ||
import argparse | ||
import logging | ||
|
||
import searx.search | ||
import searx.search.checker | ||
from searx.search import processors | ||
from searx.engines import engine_shortcuts | ||
|
||
|
||
# configure logging | ||
root = logging.getLogger() | ||
handler = logging.StreamHandler(sys.stdout) | ||
for h in root.handlers: | ||
root.removeHandler(h) | ||
root.addHandler(handler) | ||
|
||
# color only for a valid terminal | ||
if sys.stdout.isatty() and os.environ.get('TERM') not in ['dumb', 'unknown']: | ||
RESET_SEQ = "\033[0m" | ||
COLOR_SEQ = "\033[1;%dm" | ||
BOLD_SEQ = "\033[1m" | ||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = map(lambda i: COLOR_SEQ % (30 + i), range(8)) | ||
else: | ||
RESET_SEQ = "" | ||
COLOR_SEQ = "" | ||
BOLD_SEQ = "" | ||
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = "", "", "", "", "", "", "", "" | ||
|
||
# equivalent of 'python -u' (unbuffered stdout, stderr) | ||
stdout = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0), write_through=True) | ||
stderr = io.TextIOWrapper(open(sys.stderr.fileno(), 'wb', 0), write_through=True) | ||
|
||
|
||
# iterator of processors | ||
def iter_processor(engine_name_list): | ||
if len(engine_name_list) > 0: | ||
for name in engine_name_list: | ||
name = engine_shortcuts.get(name, name) | ||
processor = processors.get(name) | ||
if processor is not None: | ||
yield name, processor | ||
else: | ||
stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{RED}Engine does not exist{RESET_SEQ}') | ||
else: | ||
for name, processor in searx.search.processors.items(): | ||
yield name, processor | ||
|
||
|
||
# actual check & display | ||
def run(engine_name_list, verbose): | ||
searx.search.initialize() | ||
for name, processor in iter_processor(engine_name_list): | ||
stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}Checking\n') | ||
if not sys.stdout.isatty(): | ||
stderr.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}Checking\n') | ||
checker = searx.search.checker.Checker(processor) | ||
checker.run() | ||
if checker.test_results.succesfull: | ||
stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{GREEN}OK{RESET_SEQ}\n') | ||
if verbose: | ||
stdout.write(f' {"found languages":15}: {" ".join(sorted(list(checker.test_results.languages)))}\n') | ||
else: | ||
stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{RESET_SEQ}{RED}Error{RESET_SEQ}') | ||
if not verbose: | ||
errors = [test_name + ': ' + error for test_name, error in checker.test_results] | ||
stdout.write(f'{RED}Error {str(errors)}{RESET_SEQ}\n') | ||
else: | ||
stdout.write('\n') | ||
stdout.write(f' {"found languages":15}: {" ".join(sorted(list(checker.test_results.languages)))}\n') | ||
for test_name, logs in checker.test_results.logs.items(): | ||
for log in logs: | ||
stdout.write(f' {test_name:15}: {RED}{" ".join(log)}{RESET_SEQ}\n') | ||
|
||
|
||
# call by setup.py | ||
def main(): | ||
parser = argparse.ArgumentParser(description='Check searx engines.') | ||
parser.add_argument('engine_name_list', metavar='engine name', type=str, nargs='*', | ||
help='engines name or shortcut list. Empty for all engines.') | ||
parser.add_argument('--verbose', '-v', | ||
action='store_true', dest='verbose', | ||
help='Display details about the test results', | ||
default=False) | ||
args = parser.parse_args() | ||
run(args.engine_name_list, args.verbose) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
import json | ||
import random | ||
import time | ||
import threading | ||
import os | ||
import signal | ||
|
||
from searx import logger, settings, searx_debug | ||
from searx.exceptions import SearxSettingsException | ||
from searx.search.processors import processors | ||
from searx.search.checker import Checker | ||
from searx.shared import schedule, storage | ||
|
||
|
||
CHECKER_RESULT = 'CHECKER_RESULT' | ||
running = threading.Lock() | ||
|
||
|
||
def _get_interval(every, error_msg): | ||
if isinstance(every, int): | ||
every = (every, every) | ||
if not isinstance(every, (tuple, list))\ | ||
or len(every) != 2\ | ||
or not isinstance(every[0], int)\ | ||
or not isinstance(every[1], int): | ||
raise SearxSettingsException(error_msg, None) | ||
return every | ||
|
||
|
||
def _get_every(): | ||
every = settings.get('checker', {}).get('scheduling', {}).get('every', (300, 1800)) | ||
return _get_interval(every, 'checker.scheduling.every is not a int or list') | ||
|
||
|
||
def get_result(): | ||
serialized_result = storage.get_str(CHECKER_RESULT) | ||
if serialized_result is not None: | ||
return json.loads(serialized_result) | ||
|
||
|
||
def _set_result(result): | ||
result['timestamp'] = int(time.time() / 3600) * 3600 | ||
storage.set_str(CHECKER_RESULT, json.dumps(result)) | ||
|
||
|
||
def run(): | ||
if not running.acquire(blocking=False): | ||
return | ||
try: | ||
logger.info('Starting checker') | ||
result = { | ||
'status': 'ok', | ||
'engines': {} | ||
} | ||
for name, processor in processors.items(): | ||
logger.debug('Checking %s engine', name) | ||
checker = Checker(processor) | ||
checker.run() | ||
if checker.test_results.succesfull: | ||
result['engines'][name] = {'success': True} | ||
else: | ||
result['engines'][name] = {'success': False, 'errors': checker.test_results.errors} | ||
|
||
_set_result(result) | ||
logger.info('Check done') | ||
except Exception: | ||
_set_result({'status': 'error'}) | ||
logger.exception('Error while running the checker') | ||
finally: | ||
running.release() | ||
|
||
|
||
def _run_with_delay(): | ||
every = _get_every() | ||
delay = random.randint(0, every[1] - every[0]) | ||
logger.debug('Start checker in %i seconds', delay) | ||
time.sleep(delay) | ||
run() | ||
|
||
|
||
def _start_scheduling(): | ||
every = _get_every() | ||
schedule(every[0], _run_with_delay) | ||
run() | ||
|
||
|
||
def _signal_handler(signum, frame): | ||
t = threading.Thread(target=run) | ||
t.daemon = True | ||
t.start() | ||
|
||
|
||
def initialize(): | ||
logger.info('Send SIGUSR1 signal to pid %i to start the checker', os.getpid()) | ||
signal.signal(signal.SIGUSR1, _signal_handler) | ||
|
||
# disabled by default | ||
_set_result({'status': 'disabled'}) | ||
|
||
# special case when debug is activate | ||
if searx_debug and settings.get('checker', {}).get('off_when_debug', True): | ||
logger.info('debug mode: checker is disabled') | ||
return | ||
|
||
# check value of checker.scheduling.every now | ||
scheduling = settings.get('checker', {}).get('scheduling', None) | ||
if scheduling is None or not scheduling: | ||
logger.info('Checker scheduler is disabled') | ||
return | ||
|
||
# | ||
_set_result({'status': 'unknown'}) | ||
|
||
start_after = scheduling.get('start_after', (300, 1800)) | ||
start_after = _get_interval(start_after, 'checker.scheduling.start_after is not a int or list') | ||
delay = random.randint(start_after[0], start_after[1]) | ||
logger.info('Start checker in %i seconds', delay) | ||
t = threading.Timer(delay, _start_scheduling) | ||
t.daemon = True | ||
t.start() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a makefile target which uses python from
./local
If you think it has its value, cherry pick from 190fa23
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are different options / parameters, Makefile is not very convenient ?
python -m searx.search.checker duckduckgo "google images"
What about an entrypoint ?
searx/setup.py
Lines 52 to 56 in 14a395a