-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverboards_aio.py
805 lines (674 loc) · 23.7 KB
/
serverboards_aio.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
import sys
import os
import json
import time
import traceback
# from contextlib import contextmanager
sys.path.append(os.path.join(os.path.dirname(__file__),
'env/lib64/python3.6/site-packages/'))
sys.path.append(os.path.join(os.path.dirname(__file__),
'env/lib64/python3.5/site-packages/'))
import curio
_debug = False
_log_errors = False
plugin_id = os.environ.get("PLUGIN_ID")
real_print = print
RED = "\033[0;31m"
BLUE = "\033[0;34m"
GREY = "\033[1;30m"
RESET = "\033[1;m"
def real_debug(*msg):
if not _debug:
return
try:
real_print("\r", *msg, file=sys.stderr)
except Exception:
pass
async def maybe_await(res):
# if async, wait for response ## cr_running only on async funcs
if not res:
return res
if hasattr(res, "cr_running"):
res = await res
return res
class RPC:
def __init__(self):
self.stdin = curio.file.AsyncFile(sys.stdin)
self.stdout = curio.file.AsyncFile(sys.stdout)
self.__call_id = 1
self.__methods = {}
self.__call_group = curio.TaskGroup()
# queues to wait for response of each of the remote calls
# indexed by method call id
self.__wait_for_response = {}
# subscriptions to events
self.__subscriptions = {}
self.__subscriptions_by_id = {}
self.__subscription_id = 1
self.__run_queue = curio.queue.UniversalQueue()
self.__running = False
self.__running_calls = []
self.__waiting_calls = []
self.__background_tasks = 0
async def stop(self):
self.__running = False
try:
await self.__call_group.cancel_remaining()
await self.__call_group.join(wait=all)
await self.__run_queue.put("QUIT")
if self.stdin:
await self.stdin.close()
except curio.errors.TaskTimeout:
real_print(RED, "Timeout at serverboards stop.", RESET)
def register(self, name, function):
self.__methods[name] = function
return self
async def __send(self, js):
jss = json.dumps(js)
if _debug:
real_debug(">>> %s" % jss)
await self.stdout.write(jss + "\n")
await self.stdout.flush()
async def __parse_request(self, req):
method = req.get("method")
id = req.get("id")
if method:
params = req.get("params")
await self.__call_group.spawn(self.__parse_call,
id, method, params)
elif id:
# got result
q = self.__wait_for_response.get(id)
if not q:
real_debug("Invalid response id, not waiting for it", q)
raise Exception("invalid-response-id")
await q.put(req)
else:
real_debug("unknown request", req)
raise Exception("unknown-request")
async def __parse_call(self, id, method, params):
try:
self.__running_calls.append(method)
fn = self.__methods.get(method)
if not fn:
if id:
await self.__send({
"error": "not-found %s" % method,
"id": id
})
return
if isinstance(params, list):
res = fn(*params)
else:
res = fn(**params)
res = await maybe_await(res)
if id:
await self.__send({"result": res, "id": id})
except Exception as e:
if _debug or _log_errors:
traceback.print_exc(file=sys.stderr)
if id:
await self.__send({"error": str(e), "id": id})
finally:
self.__running_calls.remove(method)
async def call(self, method, *args, **kwargs):
self.__waiting_calls.append(method)
id = self.__call_id
self.__call_id += 1
q = curio.Queue()
self.__wait_for_response[id] = q
await self.__send({
"method": method,
"params": args or kwargs,
"id": id
})
# await for answer in the answer queue
res = await q.get()
del self.__wait_for_response[id]
self.__waiting_calls.remove(method)
error = res.get("error")
if error:
raise Exception(error)
return res.get("result")
async def event(self, method, *args, **kwargs):
await self.__send({
"method": method,
"params": args or kwargs,
})
def method_list(self):
return list(self.__methods.keys())
async def loop(self):
self.__running = True
self.__run_tasks_task = await curio.spawn(self.__run_tasks)
if not self.stdin:
return
try:
async for line in self.stdin:
line = line.strip()
if _debug:
try:
real_debug("\r<<< %s\n" % line)
except Exception:
pass
if line.startswith('# wait'): # special command!
await curio.sleep(float(line[7:]))
elif line == '# quit':
await self.stop()
return
else:
await self.__parse_request(json.loads(line))
if self.__running is False:
return
except BrokenPipeError:
return
except curio.TaskCancelled:
real_debug("EOF main loop. Task cancelled.")
return
except curio.CancelledError:
real_debug("EOF main loop. Cancelled file read.")
return
except Exception as e:
real_debug("Unexpected exception at main loop.", e)
log_traceback()
raise
finally:
await curio.timeout_after(2, self.stop)
async def subscribe(self, eventname, callback):
"""
Subscribes to an event
If the other side launches the given event, it calls the callback.
It returns an id that can be used for unsubscription.
"""
# subscribes to the real event, not the filtering
event = eventname.split('[', 1)[0]
id = self.__subscription_id
self.__subscription_id += 1
subs = self.__subscriptions.get(event, []).concat(callback)
self.__subscriptions[event] = subs
self.__subscriptions_by_id[id] = (event, callback)
await self.call("event.subscribe", eventname)
return id
async def unsubscribe(self, id):
if id not in self.__subscriptions_ids:
return False
(event, callback) = self.__subscriptions_ids[id]
subs = self.__subscriptions[event]
subs = [x for x in subs if x != callback]
if subs:
self.__subscriptions[event] = subs
else:
del self.__subscriptions[event]
await self.call("event.unsubscribe", event)
del self.__subscriptions_ids[id]
return True
def run_async(self, method, *args, result=True, **kwargs):
q = None
if self.__running and result:
q = curio.queue.UniversalQueue()
self.__run_queue.put((method, args, kwargs, q))
if q:
res = q.get()
q.task_done()
q.join()
return res
# real_debug("And return", method)
return None
async def __run_tasks(self):
while True:
if not self.__running:
return
res = await self.__run_queue.get()
if res == "QUIT":
return
async def run_in_task(method, args, kwargs, q):
# real_debug("Run in task", method)
self.__background_tasks += 1
try:
res = method(*args, **kwargs)
res = await maybe_await(res)
if q:
await q.put(res)
except BrokenPipeError as e:
real_debug(
RED, "Exception at task %s: %s" % (method, e), RESET)
return # finished! Normally write to closed stdout
except curio.TaskCancelled as e:
real_debug(
RED, "Exception at task %s: %s" % (method, e), RESET)
return
except Exception as e:
real_debug(
RED, "Exception at task %s: %s" % (method, e), RESET)
if q:
await q.put(e)
except SystemExit as e:
# real_debug(RED, "exit %s: %s" % (method, e), RESET)
await curio.spawn(self.stop)
raise
finally:
# real_debug(GREY, "Finally", method, RESET)
self.__background_tasks -= 1
await curio.spawn(run_in_task, *res, daemon=True) # no join
def status(self):
return {
"running": self.__running_calls,
"waiting": self.__waiting_calls,
"background": self.__background_tasks,
}
rpc = RPC()
def rpc_method(fnname):
if isinstance(fnname, str):
def register(fn):
rpc.register(fnname, fn)
return fn
return register
rpc.register(fnname.__name__, fnname)
return fnname
@rpc_method("dir")
def list_all_methods():
return rpc.method_list()
@rpc_method("status")
def status():
return rpc.status()
async def call(method, *args, **kwargs):
return await rpc.call(method, *args, **kwargs)
async def call_event(method, *args, **kwargs):
return await rpc.event(method, *args, **kwargs)
def loop(**kwargs):
curio.run(rpc.loop, **kwargs)
def __simple_hash__(*args, **kwargs):
hs = ";".join(str(x) for x in args)
hs += ";"
hs += ";".join(
"%s=%s" % (
__simple_hash__(k),
__simple_hash__(kwargs[k])
) for k in sorted(kwargs.keys()))
return hash(hs)
def cache_ttl(ttl=10, maxsize=50, hashf=__simple_hash__):
"""
Simple decorator, not very efficient, for a time based cache.
Params:
ttl -- seconds this entry may live. After this time, next use is
evicted.
maxsize -- If trying to add more than maxsize elements, older will be
evicted.
hashf -- Hash function for the arguments. Defaults to same data as
keys, but may require customization.
"""
def wrapper(f):
data = {}
async def wrapped(*args, **kwargs):
nonlocal data
currentt = time.time()
if len(data) >= maxsize:
# first take out all expired
data = {
k: (timeout, v)
for k, (timeout, v) in data.items()
if timeout > currentt
}
if len(data) >= maxsize:
# not enough, expire oldest
oldest_k = None
oldest_t = currentt + ttl
for k, (timeout, v) in data.items():
if timeout < oldest_t:
oldest_k = k
oldest_t = timeout
del data[oldest_k]
assert len(data) < maxsize
if not args and not kwargs:
hs = None
else:
hs = hashf(*args, **kwargs)
timeout, value = data.get(hs, (currentt, None))
if timeout <= currentt or not value:
# recalculate
value = await maybe_await(f(*args, **kwargs))
# store
data[hs] = (currentt + ttl, value)
return value
def invalidate_cache():
nonlocal data
data = {}
wrapped.invalidate_cache = invalidate_cache
return wrapped
return wrapper
class WriteTo:
def __init__(self, fn, **extra):
self.fn = fn
self.extra = extra
async def __call__(self, *args, **extra):
nextra = {**{"level": 1}, **self.extra, **extra}
if not args: # if no data, add extras for contexts.
return WriteTo(self.fn, **nextra)
await self.fn(*args, **nextra)
async def write(self, data, *args, **extra):
if data.endswith('\n'):
data = data[:-1]
await self.fn(data, *args, **{**{"level": 1}, **self.extra, **extra})
def flush(*args, **kwargs):
pass
# @contextmanager
# async def context(self, level=2, **extra):
# value = io.StringIO()
# await value
# value.seek(0)
# await self.fn(value.read(),
# **{**{"level": level}, **self.extra, **extra})
class WriteToSync:
def __init__(self, fn, **extra):
self.fn = fn
self.extra = extra
def __call__(self, *args, **extra):
nextra = {**{"level": 1}, **self.extra, **extra}
if not args: # if no data, add extras for contexts.
return WriteToSync(self.fn, **nextra)
run_async(self.fn, *args, result=False, **nextra)
def write(self, data, *args, **extra):
if data.endswith('\n'):
data = data[:-1]
run_async(self.fn, data, *args,
result=False, **{**{"level": 1}, **self.extra, **extra})
def flush(*args, **kwargs):
pass
def log_(type):
def decorate_log(extra, level=2):
"""
Helper that decorates the given log messages with data of which
function, line and file calls the log.
"""
import inspect
callerf = inspect.stack()[level]
caller = {
"plugin_id": plugin_id,
"function": callerf[3],
"file": callerf[1],
"line": callerf[2],
"pid": os.getpid(),
}
caller.update(extra)
return caller
log_method = "log.%s" % type
async def log_inner(*msg, level=0, file=None, **extra):
if not msg:
return
# if _debug:
# real_debug("\r", *msg, "\n\r")
msg = ' '.join(str(x) for x in msg)
if not msg.strip():
return
# print("Inner msg", repr(msg),
# decorate_log(extra, level=level + 2),
# file=sys.stderr)
if file is not None:
await maybe_await(file.write(msg + "\n"))
if not msg:
return
return await rpc.event(
log_method,
str(msg),
decorate_log(extra, level=level + 2)
)
return log_inner
error = WriteTo(log_("error"))
debug = WriteTo(log_("debug"))
info = WriteTo(log_("info"))
warning = WriteTo(log_("warning"))
error_sync = WriteToSync(log_("error"))
print = WriteToSync(log_("debug"))
# all normal prints go to debug channel
# sys.stdout = WriteToSync(log_("debug"))
# sys.stdout = sys.stderr
def log_traceback(exc=None):
"""
Logs the given traceback to the error log.
"""
if exc:
run_async(error, "Got exception: %s" % exc, level=1, result=False)
traceback.print_exc(file=error_sync)
def test_mode(test_function, mock_data={}):
"""
Starts test mode with smock mocking library.
Once the mock mode starts, there is no way to stop it, but restart the
program. Use under some --test flag.
"""
from smock import mock_res
print = real_print
async def __mock_send(req):
method = req.get('method')
if method:
id = req.get('id')
params = req["params"]
if not id:
if method == 'log.error':
real_print(
RED, "ERROR: ", params[0], GREY, *params[1:], RESET)
return
if method == 'log.info':
real_print(
BLUE, "INFO: ", params[0], GREY, *params[1:], RESET)
return
if method == 'log.debug':
real_print(
BLUE, "DEBUG: ", params[0], GREY, *params[1:], RESET)
return
if method == 'log.warning':
real_print(
BLUE, "WARNING: ", params[0], GREY, *params[1:], RESET)
return
print(">>>", method, params)
return
try:
print(">>>", method, params)
if isinstance(params, (list, tuple)):
args = params
kwargs = {}
else:
args = []
kwargs = params
res = mock_res(method, mock_data, args=args, kwargs=kwargs)
resp = {
"result": res,
"id": req.get("id")
}
if isinstance(res, (int, str)):
print("<<<", json.dumps(res, indent=2))
else:
print("<<<", json.dumps(res._MockWrapper__data, indent=2))
await rpc._RPC__parse_request(resp)
except Exception as e:
await error("Error (%s) mocking call: %s" % (e, req))
traceback.print_exc()
resp = {
"error": str(e),
"id": req.get("id")
}
print("<<<", json.dumps({"error": str(e)}, indent=2))
await rpc._RPC__parse_request(resp)
return
print(">>>", req)
# print(dir(serverboards.rpc))
rpc._RPC__send = __mock_send
async def exit_wrapped():
exit_code = 1
try:
await test_function()
print("OK!")
exit_code = 0
except Exception:
print("Exception!")
traceback.print_exc(file=sys.stderr)
sys.exit(exit_code)
run_async(exit_wrapped)
set_debug(True, True)
loop(with_monitor=True)
def run_async(method, *args, **kwargs):
"""
Bridge to call async from sync code and a run later facility
This function allows to run later in the coro loop any required
function that may require communication or async behaviour.
This also allows to call an async function from sync code, for example,
it is used in the print wrapper on the Serverboards API to call print
as normal code (sync) but send the proper message to log the data on
Serveboards CORE.
The call will not be processed straight away, buut may be delayed until
the process gets into some specific points in the serverboards loop.
"""
return rpc.run_async(method, *args, **kwargs)
def set_debug(on=None, log_errors=None):
"""
Set debug mode.
If no args are given, it enables debug mode. It can also receive
`log_errors = True` to only log tracebacks of failing functions.
"""
global _debug, _log_errors
if on is None and log_errors is None:
_debug = True
if on is not None:
_debug = on
if log_errors is not None:
_log_errors = log_errors
class RPCWrapper:
"""
Wraps any module or function to be able to be called.
This allows to do a simple `service.get(uuid)`, given that before you did a
`service = RPCWrapper("service")`.
There are already some instances ready for importing as:
`from serverboards import service, issues, rules, action`
"""
def __init__(self, module):
self.module = module
def __getattr__(self, sub):
return RPCWrapper(self.module + '.' + sub)
async def __call__(self, *args, **kwargs):
if args and kwargs:
return await rpc.call(self.module, *args, kwargs)
return await rpc.call(self.module, *args, **kwargs)
action = RPCWrapper("action")
auth = RPCWrapper("auth")
group = RPCWrapper("group")
perm = RPCWrapper("perm")
user = RPCWrapper("user")
dashboard = RPCWrapper("dashboard")
event = RPCWrapper("event")
issues = RPCWrapper("issues")
logs = RPCWrapper("logs")
notifications = RPCWrapper("notifications")
plugin = RPCWrapper("plugin")
plugin.component = RPCWrapper("plugin.component")
project = RPCWrapper("project")
rules = RPCWrapper("rules")
rules_v2 = RPCWrapper("rules_v2")
service = RPCWrapper("service")
settings = RPCWrapper("settings")
async def sync(f, *args, **kwargs):
"""
Runs a sync function in an async environment.
It may generate a new thread, so f must be thread safe.
Not using curio run_in_thread as it was cancelling threads and
not working, maybe due to not finished lib.
"""
import threading
q = curio.queue.UniversalQueue()
def run_in_thread():
try:
res = f(*args, **kwargs)
except Exception as e:
res = e
except Exception:
log_traceback()
res = Exception("unknown")
q.put(res)
thread = threading.Thread(target=run_in_thread)
thread.start() # another thread
res = await q.get()
thread.join()
await q.task_done()
await q.join()
if isinstance(res, Exception):
raise res
return res
def async(f, *args, **kwargs):
"""
Runs an async function in a sync environment.
Defers the call to the main thread loop, and waits for the response.
It MUST be called from another thread.
"""
ret = rpc.run_async(f, *args, **kwargs)
if isinstance(ret, Exception):
raise ret
return ret
class Plugin:
"""
Wraps a plugin to easily call the methods in it.
It has no recovery in it.
Can specify to ensure it is dead (kill_and_restart=True) before use. This
is only useful at tests.
"""
class Method:
def __init__(self, plugin, method):
self.plugin = plugin
self.method = method
async def __call__(self, *args, **kwargs):
return await self.plugin.call(self.method, *args, **kwargs)
def __init__(self, plugin_id, restart=True):
self.plugin_id = plugin_id
self.restart = restart
self.uuid = None
def __getattr__(self, method):
return Plugin.Method(self, method)
async def start(self):
self.uuid = await rpc.call("plugin.start", self.plugin_id)
return self
async def stop(self):
"""
Stops the plugin.
"""
if not self.uuid: # not running
return self
await rpc.call("plugin.stop", self.uuid)
self.uuid = None
return self
RETRY_EVENTS = ["exit", "unknown_plugin at plugin.call", "unknown_plugin"]
async def call(self, method, *args, **kwargs):
"""
Call a method by name.
This is also a workaround calling methods called `call` and `stop`.
"""
if not self.uuid:
await self.call_retry(method, *args, **kwargs)
try:
return await rpc.call(
"plugin.call",
self.uuid,
method,
args or kwargs,
)
except Exception as e:
# if exited or plugin call returns unknown method (refered to the
# method to call at the plugin), restart and try again.
if (str(e) in Plugin.RETRY_EVENTS) and self.restart:
await self.call_retry(method, *args, **kwargs)
else:
raise
async def call_retry(self, method, *args, **kwargs):
# if error because exitted, and may restart,
# restart and try again (no loop)
await debug("Restarting plugin", self.plugin_id)
await self.start()
return await rpc.call(
"plugin.call",
self.uuid,
method,
args or kwargs
)
async def __aenter__(self):
return self
async def __aexit__(self, _type, _value, _traceback):
try:
await self.stop()
except Exception as ex:
if str(ex) != "cant_stop at plugin.stop":
raise