summaryrefslogtreecommitdiff
path: root/searx/shared/shared_uwsgi.py
blob: 4a6b0a155ee89fd26cd6457f3c93e24270922b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# SPDX-License-Identifier: AGPL-3.0-or-later

import time
from typing import Optional
import uwsgi  # pylint: disable=E0401
from . import shared_abstract


_last_signal = 10


class UwsgiCacheSharedDict(shared_abstract.SharedDict):
    def get_int(self, key: str) -> Optional[int]:
        value = uwsgi.cache_get(key)
        if value is None:
            return value
        else:
            return int.from_bytes(value, 'big')

    def set_int(self, key: str, value: int):
        b = value.to_bytes(4, 'big')
        uwsgi.cache_update(key, b)

    def get_str(self, key: str) -> Optional[str]:
        value = uwsgi.cache_get(key)
        if value is None:
            return value
        else:
            return value.decode('utf-8')

    def set_str(self, key: str, value: str):
        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())
        key = 'scheduler_call_time_signal_' + str(signum)
        uwsgi.lock()
        try:
            updating = uwsgi.cache_get(key)
            if updating is not None:
                updating = int.from_bytes(updating, 'big')
                if now - updating < delay:
                    return
            uwsgi.cache_update(key, 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)
    return True