summaryrefslogtreecommitdiff
path: root/searx/shared/shared_uwsgi.py
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2021-01-05 11:22:48 +0100
committerAlexandre Flament <alex@al-f.net>2021-01-12 11:47:17 +0100
commit6e2872f43625aba71eba019e16f7fbd74743f590 (patch)
tree858625b7b0410503c41e8ccca14b28202d13c961 /searx/shared/shared_uwsgi.py
parent9c581466e136f7cb82d5ffe6c052fbd9e93ab39f (diff)
[enh] add searx.shared
shared dictionary between the workers (UWSGI or werkzeug) scheduler: run a task once every x seconds (UWSGI or werkzeug)
Diffstat (limited to 'searx/shared/shared_uwsgi.py')
-rw-r--r--searx/shared/shared_uwsgi.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/searx/shared/shared_uwsgi.py b/searx/shared/shared_uwsgi.py
new file mode 100644
index 000000000..136bf687e
--- /dev/null
+++ b/searx/shared/shared_uwsgi.py
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+
+import time
+import uwsgi # pylint: disable=E0401
+from . import shared_abstract
+
+
+_last_signal = 10
+
+
+class UwsgiCacheSharedDict(shared_abstract.SharedDict):
+
+ def get_int(self, key):
+ value = uwsgi.cache_get(key)
+ if value is None:
+ return value
+ else:
+ return int.from_bytes(value, 'big')
+
+ def set_int(self, key, value):
+ b = value.to_bytes(4, 'big')
+ uwsgi.cache_update(key, b)
+
+ def get_str(self, key):
+ value = uwsgi.cache_get(key)
+ if value is None:
+ return value
+ else:
+ return value.decode('utf-8')
+
+ def set_str(self, key, value):
+ b = value.encode('utf-8')
+ uwsgi.cache_update(key, b)
+
+
+def schedule(delay, func, *args):
+ """
+ Can be implemented using a spooler.
+ https://uwsgi-docs.readthedocs.io/en/latest/PythonDecorators.html
+
+ To make the uwsgi configuration simple, use the alternative implementation.
+ """
+ global _last_signal
+
+ def sighandler(signum):
+ now = int(time.time())
+ uwsgi.lock()
+ try:
+ updating = uwsgi.cache_get('updating')
+ if updating is not None:
+ updating = int.from_bytes(updating, 'big')
+ if now - updating < delay:
+ return
+ uwsgi.cache_update('updating', now.to_bytes(4, 'big'))
+ finally:
+ uwsgi.unlock()
+ func(*args)
+
+ signal_num = _last_signal
+ _last_signal += 1
+ uwsgi.register_signal(signal_num, 'worker', sighandler)
+ uwsgi.add_timer(signal_num, delay)