.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.search.checker.background Namespace Reference

Classes

class  CheckerErr
 
class  CheckerOk
 
class  CheckerOther
 
class  EngineErr
 
class  EngineOk
 

Functions

Tuple[int, int] _get_interval (Any every, str error_msg)
 
CheckerResult get_result ()
 
 _set_result (CheckerResult result)
 
 _timestamp ()
 
 run ()
 
 _signal_handler (int _signum, Any _frame)
 
 initialize ()
 

Variables

str REDIS_RESULT_KEY = 'SearXNG_checker_result'
 
str REDIS_LOCK_KEY = 'SearXNG_checker_lock'
 
 CheckerResult = Union['CheckerOk', 'CheckerErr', 'CheckerOther']
 
 EngineResult = Union['EngineOk', 'EngineErr']
 

Function Documentation

◆ _get_interval()

Tuple[int, int] searx.search.checker.background._get_interval ( Any every,
str error_msg )
protected

Definition at line 65 of file background.py.

65def _get_interval(every: Any, error_msg: str) -> Tuple[int, int]:
66 if isinstance(every, int):
67 return (every, every)
68
69 if (
70 not isinstance(every, (tuple, list))
71 or len(every) != 2 # type: ignore
72 or not isinstance(every[0], int)
73 or not isinstance(every[1], int)
74 ):
75 raise SearxSettingsException(error_msg, None)
76 return (every[0], every[1])
77
78

Referenced by searx.search.checker.background.initialize().

+ Here is the caller graph for this function:

◆ _set_result()

searx.search.checker.background._set_result ( CheckerResult result)
protected

Definition at line 91 of file background.py.

91def _set_result(result: CheckerResult):
92 client = get_redis_client()
93 if client is None:
94 # without Redis, the function does nothing
95 return
96 client.set(REDIS_RESULT_KEY, json.dumps(result))
97
98

Referenced by searx.search.checker.background.run().

+ Here is the caller graph for this function:

◆ _signal_handler()

searx.search.checker.background._signal_handler ( int _signum,
Any _frame )
protected

Definition at line 129 of file background.py.

129def _signal_handler(_signum: int, _frame: Any):
130 t = threading.Thread(target=run)
131 t.daemon = True
132 t.start()
133
134

◆ _timestamp()

searx.search.checker.background._timestamp ( )
protected

Definition at line 99 of file background.py.

99def _timestamp():
100 return int(time.time() / 3600) * 3600
101
102

Referenced by searx.search.checker.background.run().

+ Here is the caller graph for this function:

◆ get_result()

CheckerResult searx.search.checker.background.get_result ( )

Definition at line 79 of file background.py.

79def get_result() -> CheckerResult:
80 client = get_redis_client()
81 if client is None:
82 # without Redis, the checker is disabled
83 return {'status': 'disabled'}
84 serialized_result: Optional[bytes] = client.get(REDIS_RESULT_KEY)
85 if serialized_result is None:
86 # the Redis key does not exist
87 return {'status': 'unknown'}
88 return json.loads(serialized_result)
89
90

◆ initialize()

searx.search.checker.background.initialize ( )

Definition at line 135 of file background.py.

135def initialize():
136 if hasattr(signal, 'SIGUSR1'):
137 # Windows doesn't support SIGUSR1
138 logger.info('Send SIGUSR1 signal to pid %i to start the checker', os.getpid())
139 signal.signal(signal.SIGUSR1, _signal_handler)
140
141 # special case when debug is activate
142 if searx_debug and settings['checker']['off_when_debug']:
143 logger.info('debug mode: checker is disabled')
144 return
145
146 # check value of checker.scheduling.every now
147 scheduling = settings['checker']['scheduling']
148 if scheduling is None or not scheduling:
149 logger.info('Checker scheduler is disabled')
150 return
151
152 # make sure there is a Redis connection
153 if get_redis_client() is None:
154 logger.error('The checker requires Redis')
155 return
156
157 # start the background scheduler
158 every_range = _get_interval(scheduling.get('every', (300, 1800)), 'checker.scheduling.every is not a int or list')
159 start_after_range = _get_interval(
160 scheduling.get('start_after', (300, 1800)), 'checker.scheduling.start_after is not a int or list'
161 )
162 t = threading.Thread(
163 target=scheduler_function,
164 args=(start_after_range[0], start_after_range[1], every_range[0], every_range[1], run),
165 name='checker_scheduler',
166 )
167 t.daemon = True
168 t.start()

References searx.search.checker.background._get_interval().

+ Here is the call graph for this function:

◆ run()

searx.search.checker.background.run ( )

Definition at line 103 of file background.py.

103def run():
104 try:
105 # use a Redis lock to make sure there is no checker running at the same time
106 # (this should not happen, this is a safety measure)
107 with get_redis_client().lock(REDIS_LOCK_KEY, blocking_timeout=60, timeout=3600):
108 logger.info('Starting checker')
109 result: CheckerOk = {'status': 'ok', 'engines': {}, 'timestamp': _timestamp()}
110 for name, processor in PROCESSORS.items():
111 logger.debug('Checking %s engine', name)
112 checker = Checker(processor)
113 checker.run()
114 if checker.test_results.successful:
115 result['engines'][name] = {'success': True}
116 else:
117 result['engines'][name] = {'success': False, 'errors': checker.test_results.errors}
118
119 _set_result(result)
120 logger.info('Check done')
121 except redis.exceptions.LockError:
122 _set_result({'status': 'error', 'timestamp': _timestamp()})
123 logger.exception('Error while running the checker')
124 except Exception: # pylint: disable=broad-except
125 _set_result({'status': 'error', 'timestamp': _timestamp()})
126 logger.exception('Error while running the checker')
127
128

References searx.search.checker.background._set_result(), and searx.search.checker.background._timestamp().

+ Here is the call graph for this function:

Variable Documentation

◆ CheckerResult

searx.search.checker.background.CheckerResult = Union['CheckerOk', 'CheckerErr', 'CheckerOther']

Definition at line 25 of file background.py.

◆ EngineResult

searx.search.checker.background.EngineResult = Union['EngineOk', 'EngineErr']

Definition at line 49 of file background.py.

◆ REDIS_LOCK_KEY

str searx.search.checker.background.REDIS_LOCK_KEY = 'SearXNG_checker_lock'

Definition at line 22 of file background.py.

◆ REDIS_RESULT_KEY

str searx.search.checker.background.REDIS_RESULT_KEY = 'SearXNG_checker_result'

Definition at line 21 of file background.py.