fake_uwsgi.worker_id()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
"""
2
This is module attempts to fake out the uwsgi module provided by uWSGI when running
3
a Python application in uWSGI.
4
5
To use this put the following in the module that requires the uwsgi module:
6
7
try:
8
    # The following import will fail if not running in uWSGI
9
    import uwsgi  # pylint: disable=import-error
10
except ImportError:
11
    import fake_uwsgi as uwsgi  # pylint: disable-msg=ungrouped-imports
12
"""
13
14
import os
15
import time
16
17
__version__ = "0.1.0"
18
19
opt = numproc = LOGVAR = IMPORT_TIME = None  # pylint: disable=invalid-name
20
21
22
def setup_fake_uwsgi():
23
    """
24
    Setup the fake uWSGI.
25
    This function is called upon import by the module itself.
26
    """
27
    global opt  # pylint: disable=invalid-name,global-statement
28
    global numproc  # pylint: disable=invalid-name,global-statement
29
    global LOGVAR  # pylint: disable=global-statement
30
    global IMPORT_TIME  # pylint: disable=global-statement
31
32
    os.environ["INSTALL_PATH"] = os.path.abspath(os.path.join(__file__, os.pardir))
33
    os.environ["APP_RUN_MODE"] = "development"
34
    opt = {"mode": b"development", "vassal-name": b"fake-uwsgi"}
35
36
    numproc = 4
37
38
    LOGVAR = {}
39
    IMPORT_TIME = time.time()
40
41
42
def log(*args, **kwargs):
43
    """A wrapper that invokes the built-in print function with the args and kwargs
44
    passed to this function.
45
    """
46
    print(*args, **kwargs)
47
48
49
def set_logvar(*args, **kwargs):
50
    """Set the log variables.
51
    If a single argument is passed it will use that as the key and set the value to
52
    None.
53
    If two arguments are passed, example set_logvar("key", "value") it will set the key
54
    (first argument) to the value (second argument)
55
    If kwargs are provided it will update the log variable dictionary.
56
    """
57
    global LOGVAR  # pylint: disable=global-statement,global-variable-not-assigned
58
59
    if len(args) == 1:
60
        key = args[0]
61
        LOGVAR[key] = None
62
    elif len(args) == 2:
63
        key = args[0]
64
        val = args[1]
65
        LOGVAR[key] = val
66
    elif len(kwargs.keys()) != 0:
67
        LOGVAR.update(kwargs)
68
69
70
def get_logvar(key):
71
    """Get the log variable given the key.
72
73
    Args:
74
        key (*): The key for which the value is required
75
76
    Returns:
77
        *: Returns the value in the type it was set. None if its not set.
78
    """
79
    return LOGVAR.get(key, None)
80
81
82
def worker_id():
83
    """Return a predefined worker ID.
84
85
    Returns:
86
        int: The static work id
87
    """
88
    return 123
89
90
91
def workers():
92
    """
93
    Return a generated list of worker data.
94
95
    Returns:
96
        list: The worker data
97
    """
98
    global IMPORT_TIME  # pylint: disable=global-statement,global-variable-not-assigned
99
    return [
100
        {
101
            "apps": [
102
                {
103
                    "id": 0,
104
                    "modifier1": 0,
105
                    "mountpoint": "/",
106
                    "startup_time": 0,
107
                    "interpreter": 36657355585954,
108
                    "callable": 427548492337761,
109
                    "requests": 0,
110
                    "exceptions": 0,
111
                    "chdir": "",
112
                }
113
            ],
114
            "avg_rt": 0,
115
            "delta_requests": 0,
116
            "exceptions": 0,
117
            "id": worker_id(),
118
            "last_spawn": IMPORT_TIME,
119
            "pid": 42744,
120
            "requests": total_requests() / 2,
121
            "respawn_count": 0,
122
            "rss": 0,
123
            "running_time": time.time() - IMPORT_TIME,
124
            "signals": 0,
125
            "status": "idle",
126
            "tx": 0,
127
            "vsz": 0,
128
        },
129
        {
130
            "apps": [
131
                {
132
                    "id": 0,
133
                    "modifier1": 0,
134
                    "mountpoint": "/",
135
                    "startup_time": 0,
136
                    "interpreter": 95994467934864,
137
                    "callable": 684895773596472,
138
                    "requests": 0,
139
                    "exceptions": 0,
140
                    "chdir": "",
141
                }
142
            ],
143
            "avg_rt": 0,
144
            "delta_requests": 0,
145
            "exceptions": 0,
146
            "id": worker_id(),
147
            "last_spawn": IMPORT_TIME + 1,
148
            "pid": 42745,
149
            "requests": total_requests() / 2,
150
            "respawn_count": 0,
151
            "rss": 0,
152
            "running_time": time.time() - IMPORT_TIME + 1,
153
            "signals": 0,
154
            "status": "busy",
155
            "tx": 0,
156
            "vsz": 0,
157
        },
158
    ]
159
160
161
def total_requests():
162
    """Return a predefined value of total requests
163
164
    Returns:
165
        int: The number of total requests processed. This is not a realistic value.
166
    """
167
    return 564
168
169
170
setup_fake_uwsgi()
171