@@ -0,0 +1,62 @@
1+# SPDX-License-Identifier: AGPL-3.0-or-later
2+
3+import time
4+import uwsgi # pylint: disable=E0401
5+from . import shared_abstract
6+
7+
8+_last_signal = 10
9+
10+
11+class UwsgiCacheSharedDict(shared_abstract.SharedDict):
12+
13+ def get_int(self, key):
14+ value = uwsgi.cache_get(key)
15+ if value is None:
16+ return value
17+ else:
18+ return int.from_bytes(value, 'big')
19+
20+ def set_int(self, key, value):
21+ b = value.to_bytes(4, 'big')
22+ uwsgi.cache_update(key, b)
23+
24+ def get_str(self, key):
25+ value = uwsgi.cache_get(key)
26+ if value is None:
27+ return value
28+ else:
29+ return value.decode('utf-8')
30+
31+ def set_str(self, key, value):
32+ b = value.encode('utf-8')
33+ uwsgi.cache_update(key, b)
34+
35+
36+def schedule(delay, func, *args):
37+ """
38+ Can be implemented using a spooler.
39+ https://uwsgi-docs.readthedocs.io/en/latest/PythonDecorators.html
40+
41+ To make the uwsgi configuration simple, use the alternative implementation.
42+ """
43+ global _last_signal
44+
45+ def sighandler(signum):
46+ now = int(time.time())
47+ uwsgi.lock()
48+ try:
49+ updating = uwsgi.cache_get('updating')
50+ if updating is not None:
51+ updating = int.from_bytes(updating, 'big')
52+ if now - updating < delay:
53+ return
54+ uwsgi.cache_update('updating', now.to_bytes(4, 'big'))
55+ finally:
56+ uwsgi.unlock()
57+ func(*args)
58+
59+ signal_num = _last_signal
60+ _last_signal += 1
61+ uwsgi.register_signal(signal_num, 'worker', sighandler)
62+ uwsgi.add_timer(signal_num, delay)