forked from mozilla-conduit/lando-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystems.py
74 lines (58 loc) · 2.09 KB
/
systems.py
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
65
66
67
68
69
70
71
72
73
74
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import logging
import sys
import time
logger = logging.getLogger(__name__)
class Subsystem:
name = None
def __init__(self, app=None):
self.flask_app = None
if app is not None:
self.init_app(app)
def init_app(self, app):
self.flask_app = app
if "SUBSYSTEMS" not in self.flask_app.config:
self.flask_app.config["SUBSYSTEMS"] = {}
self.flask_app.config["SUBSYSTEMS"][self.name] = self
def ready(self):
"""Return True if ready, a message describing the problem otherwise.
If `None` is returned it indicates that this Subsystem does not
require a ready check.
"""
return None
def healthy(self):
"""Return True if healthy, a message describing the problem otherwise.
If `None` is returned it indicates that this Subsystem does not
require a health check.
"""
return None
def ensure_ready(self):
for attempt in range(30):
try:
ready = self.ready()
except Exception:
logger.exception(
"Subsystem {} threw an exception".format(self.name),
extra={"subsystem": self.name},
)
ready = False
if ready is None:
return
elif ready is True:
break
logger.warning(
"Subsystem {} is not ready, sleeping.".format(self.name),
extra={"subsystem": self.name, "reason": ready},
)
time.sleep(1 + attempt)
else:
logger.error(
"Subsystem {} is not ready, giving up.".format(self.name),
extra={"subsystem": self.name},
)
sys.exit(1)
logger.info(
"Subsystem {} is ready.".format(self.name), extra={"subsystem": self.name}
)