-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
204 lines (162 loc) · 5.37 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from enum import IntEnum
from dotenv import load_dotenv
from sanic import Sanic
from sanic.response import json, file, text
from sanic.websocket import WebSocketProtocol
import asyncio
import logging
import os
import time
import ujson
class ShardState(IntEnum):
UNKNOWN = 0
UNHEALTHY = 1
OFFLINE = 2
ONLINE = 3
STARTING = 4
STOPPING = 5
RESUMING = 6
# TODO: are these good states, needs to check with discord.py
# {
# "bot": {
# "len": 1,
# "0": {
# "state": 0
# "last_healthy": 0
# }
# }
# }
State = dict()
Feeds = list()
app = Sanic()
HealthTask = None
health_timeout = None
health_check_interval = None
authorization = None
app.static('/assets/', './assets/')
def validate_shard_in_cluster(cluster: str, id: int):
if cluster not in State:
return json({
'error': f'cluster \'{cluster}\' does not exist'
}, status=400)
if id < 0 or id > State[cluster]['len']-1:
return json({
'error': f'shard \'{id}\' falls outside the range of valid shards on cluster \'{cluster}\''
}, status=400)
return None
def require_auth(request):
if authorization and request.headers['Authorization'] != authorization:
return json({'error': 'this endpoint requires authorization'}, status=401)
return None
@app.route('/api/health/<cluster>/<id:int>', methods=['POST'])
async def health(request, cluster: str, id: int):
auth = require_auth(request)
if auth is not None: return auth
valid = validate_shard_in_cluster(cluster, id)
if valid is not None: return valid
if str(id) not in State[cluster]:
return json({
'error': f'shard \'{id}\' is not known to the system yet, please populate your status'
}, status=409)
State[cluster][str(id)]['last_healthy'] = time.time()
msg = {
'op': 'health_ping',
'payload': {
'cluster': cluster,
'shard': id
}
}
if State[cluster][str(id)]['state'] == ShardState.UNHEALTHY:
# assume shards won't health-check unless they are online :)
State[cluster][str(id)]['state'] = ShardState.ONLINE
msg['payload']['state'] = ShardState.ONLINE
payload = ujson.dumps(msg)
deliver(payload)
return text('', status=204)
@app.route('/api/status/<cluster>/<id:int>/<state:int>', methods=['POST'])
async def status(request, cluster: str, id: int, state: int):
auth = require_auth(request)
if auth is not None: return auth
valid = validate_shard_in_cluster(cluster, id)
if valid is not None: return valid
State[cluster][str(id)] = {
'state': state,
'last_healthy': time.time()
}
payload = ujson.dumps({
'op': 'state_update',
'payload': {
'cluster': cluster,
'shard': id,
'state': state
}
})
deliver(payload)
return text('', status=204)
@app.websocket('/api/feed')
async def feed(request, ws):
Feeds.append(ws)
ws.connection_lost = lambda exc: Feeds.remove(ws)
hello = {
'op': 'hello',
'payload': State
}
await ws.send(ujson.dumps(hello))
while True:
# Keep the socket open indefinitely
# TODO: is there a better solution to this
await asyncio.sleep(15)
@app.route('/')
async def index(request):
return await file('./index.html')
@app.route('/plain')
async def noscript(request):
return text(str(State))
def deliver(payload):
for client in Feeds:
asyncio.ensure_future(client.send(payload))
@app.listener('after_server_start')
async def hook_health(app, loop):
asyncio.ensure_future(_health_loop())
logging.info('health loop running')
async def _health_loop():
while True:
logging.debug('running health checks')
await _run_health_checks()
logging.debug('health checks completed, sleeping')
await asyncio.sleep(health_check_interval)
async def _run_health_checks():
now = time.time()
for (cluster, shards) in State.items():
for (id, shard) in shards.items():
if id == 'len': continue
if shard['state'] == ShardState.UNHEALTHY: # this shard is already sick
continue # TODO: alerting for continued sickness?
if (now - shard['last_healthy']) > health_timeout:
shard['state'] = ShardState.UNHEALTHY
payload = ujson.dumps({
'op': 'state_update',
'payload': {
'cluster': cluster,
'shard': id,
'state': shard['state']
}
})
deliver(payload)
def configure_state():
# GLANCE_BOTS=main:100;patron_1:6;patron_2:6
bots_raw = os.getenv('GLANCE_BOTS')
for bot in bots_raw.split(';'):
(cluster, shards) = bot.split(':')
shards = int(shards)
State[cluster] = dict()
State[cluster]['len'] = shards
if __name__ == '__main__':
load_dotenv()
configure_state()
health_timeout = int(os.getenv('GLANCE_HEALTH_TIMEOUT') or '30')
health_check_interval = int(os.getenv('GLANCE_HEALTH_TIMER_INTERVAL') or '15')
authorization = os.getenv('GLANCE_PASSPHRASE')
app.run(host=os.getenv('GLANCE_HOST') or '0.0.0.0',
port=int(os.getenv('GLANCE_PORT') or '3000')
)