-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaioservice.py
484 lines (415 loc) · 15.7 KB
/
aioservice.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
import sys
import json
import os
sys.path.append("./aioservices")
from services import Services, failed_services
_SERVICES_GROUP = {service.name: service for service in Services}
_SERVICES_CONFIG = "services.config"
_SERVICES_STATUS = {"loaded": set(), "failed": set()}
_SERVICE_LOGGER = None
def service(name=None):
global _SERVICES_GROUP
if not name:
return _SERVICES_GROUP
elif name in _SERVICES_GROUP:
return _SERVICES_GROUP[name]
def list():
global _SERVICES_GROUP
for service in _SERVICES_GROUP.values():
print(f"{service}")
def status(name=None):
global _SERVICES_STATUS
if not name:
for _service in _SERVICES_STATUS["loaded"]:
print(f"[ \033[92mOK\x1b[0m ] {_service} loaded")
for _service in _SERVICES_STATUS["failed"]:
print(f"[ \u001b[31;1mERROR\u001b[0m ] {_service} not loaded:", end="")
print(f" Error: {_service.info.__class__.__name__}")
else:
if service(name) in _SERVICES_STATUS["loaded"]:
print(f"[ \033[92mOK\x1b[0m ] {service(name)} loaded")
elif service(name) in _SERVICES_STATUS["failed"]:
print(f"[ \u001b[31;1mERROR\u001b[0m ] {service(name)} not loaded:", end="")
print(f" Error: {service(name).info.__class__.__name__}")
def call(name):
if callable(service(name)):
service(name)()
def _suid(_aioctl, name):
name = f"{name}.service"
_name = name
_id = 0
if _aioctl.group():
while name in _aioctl.group().tasks:
_id += 1
name = f"{_name}@{_id}"
return name
def _get_env_var(value):
import aioctl
import re
if isinstance(value, str):
m = re.search(r"\${.*}", value)
if not m:
return value
var_rep = m.group(0)
var_key = var_rep.replace("${", "").replace("}", "")
return value.replace(var_rep, aioctl.getenv(var_key, var_rep))
elif isinstance(value, type([])):
return [_get_env_var(val) for val in value]
elif isinstance(value, dict):
return {k: _get_env_var(v) for k, v in value.items()}
else:
return value
def load(name=None, debug=False, log=None, debug_log=False, config=False):
global _SERVICES_GROUP, _SERVICES_STATUS, _SERVICE_LOGGER
import aioctl
if log:
_SERVICE_LOGGER = log
if not name:
if config:
_servs_config = get_config()
for service in _SERVICES_GROUP.values():
if config:
if service.name in _servs_config.keys():
if _servs_config[service.name]["enabled"]:
service.enabled = True
if "args" in _servs_config[service.name]:
service.args = [
_get_env_var(arg)
for arg in _servs_config[service.name]["args"]
]
if "kwargs" in _servs_config[service.name]:
service.kwargs.update(
**{
k: _get_env_var(v)
for k, v in _servs_config[service.name][
"kwargs"
].items()
}
)
if "schedule" in service.kwargs and hasattr(
service, "schedule"
):
service.schedule = service.kwargs.pop("schedule")
else:
service.enabled = False
# catch failed load services
if service.name in failed_services or not service.loaded:
_SERVICES_STATUS["failed"].add(service)
if debug:
print(
f"[ \u001b[31;1mERROR\u001b[0m ] {service} not loaded:", end=""
)
print(f" Error: {service.info.__class__.__name__}")
if debug_log and log:
_err = f"{service} not loaded:"
_err += f" Error: {service.info.__class__.__name__}"
log.error(f"[aioservice] [ \u001b[31;1mERROR\u001b[0m ] {_err}")
continue
if service.enabled:
aioctl.add(
service.task,
service,
*service.args,
**service.kwargs,
name=f"{service.name}.service",
_id=f"{service.name}.service",
log=log,
)
if aioctl._SCHEDULE:
if hasattr(service, "schedule"):
import aioschedule
aioschedule.schedule(
f"{service.name}.service", **service.schedule
)
if debug:
print(f"[ \033[92mOK\x1b[0m ] {service} loaded")
if debug_log and log:
log.info(f"[aioservice] [ \033[92mOK\x1b[0m ] {service} loaded")
_SERVICES_STATUS["loaded"].add(service)
else:
if name in _SERVICES_GROUP.keys():
service = _SERVICES_GROUP[name]
if config and name not in failed_services:
_serv_config = get_config(name)
if _serv_config:
if "args" in _serv_config:
service.args = [
_get_env_var(arg) for arg in _serv_config["args"]
]
if "kwargs" in _serv_config:
service.kwargs.update(
**{
k: _get_env_var(v)
for k, v in _serv_config["kwargs"].items()
}
)
if "schedule" in service.kwargs and hasattr(
service, "schedule"
):
service.schedule = service.kwargs.pop("schedule")
# catch failed load services
if service.name in failed_services or not service.loaded:
_SERVICES_STATUS["failed"].add(service)
if debug:
print(
f"[ \u001b[31;1mERROR\u001b[0m ] {service} not loaded:", end=""
)
print(f" Error: {service.info.__class__.__name__}")
if debug_log and log:
_err = f"{service} not loaded:"
_err += f" Error: {service.info.__class__.__name__}"
log.error(f"[aioservice] [ \u001b[31;1mERROR\u001b[0m ] {_err}")
return False
_name = _suid(aioctl, service.name)
aioctl.add(
service.task,
service,
*service.args,
**service.kwargs,
name=_name,
_id=_name,
log=log,
)
if aioctl._SCHEDULE:
if hasattr(service, "schedule"):
aioctl.stop(_name, stop_sch=False)
import aioschedule
aioschedule.schedule(_name, **service.schedule)
if debug:
print(f"[ \033[92mOK\x1b[0m ] {service} loaded")
if debug_log and log:
log.info(f"[aioservice] [ \033[92mOK\x1b[0m ] {service} loaded")
_SERVICES_STATUS["loaded"].add(service)
else:
if "./aioservices/services" not in sys.path:
sys.path.append("./aioservices/services")
try:
from services import modules
end = "py"
if f"{name}_service.{end}" not in modules:
end = "mpy"
if f"{name}_service.{end}" not in modules:
return False
_tmp = __import__(f"{name}_service", [], [], ["service"])
_tmp.service.path = f"./aioservices/services/{name}_service.{end}"
_SERVICES_GROUP[name] = _tmp.service
load(
name,
debug=True,
log=_SERVICE_LOGGER,
debug_log=True,
config=config,
)
except Exception as e:
sys.print_exception(e)
def unload(name):
global _SERVICES_GROUP, _SERVICES_STATUS
import aioctl
try:
aioctl.stop(f"{name}.service*")
aioctl.delete(f"{name}.service*")
if name in _SERVICES_GROUP:
uns = _SERVICES_GROUP.pop(name)
if uns in _SERVICES_STATUS["loaded"]:
_SERVICES_STATUS["loaded"].remove(uns)
elif uns in _SERVICES_STATUS["failed"]:
_SERVICES_STATUS["failed"].remove(uns)
return True
except Exception as e:
sys.print_exception(e)
def init(debug=True, log=None, debug_log=False, config=True, init_schedule=True):
import aioctl
for service in _SERVICES_GROUP.values():
if hasattr(service, "type"):
if service.type != "core.service":
load(
service.name,
debug=debug,
log=log,
debug_log=debug_log,
config=config,
)
if aioctl._SCHEDULE and init_schedule:
import aioschedule
if aioschedule.group():
if "schedule_loop" not in aioctl.group().tasks:
aioctl.add(aioschedule.schedule_loop)
async def boot(debug=True, log=None, debug_log=False, config=True):
import aioctl
import asyncio
core_services = [
srv for srv in _SERVICES_GROUP.values() if srv.type == "core.service"
]
core_hp_services = []
core_lp_services = []
if config:
_servs_config = get_config()
for srv in core_services:
if srv.name in _servs_config:
srv.args = _servs_config[srv.name].get("args", srv.args)
srv.kwargs.update(**_servs_config[srv.name].get("kwargs", srv.kwargs))
else:
if debug:
print(
f"[aioservice] No config found for {srv.name}.service"
+ ", using default"
)
if debug_log and log:
log.info(
f"[aioservice] No config found for {srv.name}.service"
+ ", using default"
)
# check if any requirement for core.service
serv_rq = [srv for srv in core_services if "require" in srv.kwargs]
serv_core = [srv for srv in core_services if srv not in serv_rq]
# true -> from aioclass import PQeue
if serv_rq:
from aioclass import PQueue
# -> add core services
# -> psolve
# -> get a priority core hp list, load core_lp
pq = PQueue()
pq.add(*serv_core, *serv_rq)
pl, hp, lp = pq.psolve()
if debug:
print("[ \033[92mOK\x1b[0m ] Priority core services solved... ")
print("[ Booting ] Order: ", end="")
for s, p in pl:
print(f"--> {s} ", end="")
print("")
if debug_log and log:
log.info(
"[aioservice] [ \033[92mOK\x1b[0m ] Priority "
+ "core services solved..."
)
_boot_ord = "[ Booting ] Order: "
for s, p in pl:
_boot_ord += f"--> {s} "
log.info(_boot_ord)
core_hp_services = hp
core_lp_services = lp
else:
core_lp_services = serv_core
# -> await core_hp serialy
# -> gather core_lp concurrently
# false ->
# -> all are core_lp
for service in core_hp_services:
if isinstance(service.info, Exception):
res = service.info
else:
load(
service.name,
debug=debug,
log=log,
debug_log=debug_log,
config=config,
)
if debug:
print(f"[ \033[92mOK\x1b[0m ] Booting {service.name}.service... ")
if debug_log and log:
log.info(
"[aioservice] [ \033[92mOK\x1b[0m ] Booting "
+ f"{service.name}.service..."
)
res = await aioctl.group().tasks[f"{service.name}.service"].task
if res:
if not issubclass(res.__class__, Exception):
if debug:
print("[ \033[92mOK\x1b[0m ] " + f"{service.name}.service {res} ")
if debug_log and log:
log.info(
"[aioservice] [ \033[92mOK\x1b[0m ] "
+ f"{service.name}.service {res}"
)
else:
if debug:
print(
f"[ \u001b[31;1mERROR\u001b[0m ] {service.name}:",
end="",
)
print(f" Error: {res.__class__.__name__}: {res}")
if debug_log and log:
_err = f"{service.name}:"
_err += f" Error: {res.__class__.__name__}: {res}"
log.error(f"[aioservice] [ \u001b[31;1mERROR\u001b[0m ] {_err}")
for service in core_lp_services:
load(
service.name,
debug=debug,
log=log,
debug_log=debug_log,
config=config,
)
if core_lp_services:
await asyncio.gather(*aioctl.tasks())
asyncio.new_event_loop()
def config(name, enable, *args, **kwargs):
global _SERVICES_CONFIG
_exists = False
try:
# exists
os.stat(_SERVICES_CONFIG)
_exists = True
except Exception:
# create new one
pass
if _exists: # load
_service_config = get_config()
if name not in _service_config:
_service_config[name] = {}
_service_config[name]["enabled"] = enable
if args:
_service_config[name]["args"] = args
if kwargs:
if "kwargs" in _service_config[name]:
_service_config[name]["kwargs"].update(**kwargs)
else:
_service_config[name]["kwargs"] = kwargs
else: # create
_service_config = {}
_service_config[name] = {}
_service_config[name]["enabled"] = enable
if args:
_service_config[name]["args"] = args
if kwargs:
_service_config[name]["kwargs"] = kwargs
try:
with open(_SERVICES_CONFIG, "w") as servs_conf:
json.dump(_service_config, servs_conf)
return True
except Exception as e:
sys.print_exception(e, sys.stdout)
return
# create
def enable(name):
config(name, True)
def disable(name):
config(name, False)
def get_config(name=None):
global _SERVICES_CONFIG
try:
with open(_SERVICES_CONFIG, "r") as servs_conf:
service_config = json.load(servs_conf)
except Exception as e:
sys.print_exception(e, sys.stdout)
return
if not name:
return service_config
else:
if name in service_config:
return service_config[name]
else:
return False
def traceback(name=None, rtn=False):
global _SERVICES_GROUP
if name in _SERVICES_GROUP:
_tb = _SERVICES_GROUP[name].info
if issubclass(_tb.__class__, Exception):
if rtn:
return True
print(f"{name}: Traceback")
sys.print_exception(_tb)
else:
if rtn:
return False