base.py 1.39 KB
Newer Older
1
2
3
import time
import os

kaiyou's avatar
kaiyou committed
4
5
6
7
8
9
10
11

def dummy(context):
    """ Dummy check used for ticking
    """
    return {
        "up": True,
        "message": None
    }
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


def send_then_receive(send_function, lookup_function):
    """ Generate a canary, send it using the send function, then
    look it up using the lookup function.
    """

    def decorator(original_check):

        def actual_check(context, account_from, account_to,
                         dest=None, timeout=120.0):
            if dest is None:
                dest = account_to["username"]
            # Generate, store and send a canary
            canary = os.urandom(8).hex()
            result = {"canary": canary, **context}
            send_function(result, account_from, dest, canary)
            # Wait for the canary to be received or timeout
            start_time = time.time()
            delay = 0
            found = False
            while delay < timeout and not found:
                found = lookup_function(result, account_to, dest, canary)
                delay = int(time.time() - start_time)
                time.sleep(10)
            result["up"] = found
            result["message"] = f"message delivered in {delay} seconds" \
                if found else f"message not delivered"
            print(result)
            return result

        actual_check.__name__ = original_check.__name__
        return actual_check

    return decorator