123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- from hub import methods, Global
- import asyncio
- import threading
- import traceback
- class LineManage(object):
- """"""
- line_dict = {}
- @classmethod
- def run_forever(cls):
- tasks = [cls.check_loop()]
- _loop = asyncio.new_event_loop()
- asyncio.set_event_loop(_loop)
- loop = asyncio.get_event_loop()
- loop.run_until_complete(asyncio.wait(tasks))
- @classmethod
- def run_background(cls, is_back_run=True):
- t1 = threading.Thread(target=cls.run_forever)
- t1.start()
- @classmethod
- async def check_loop(cls):
-
-
- while True:
- try:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- now_ts = methods.now_ts()
-
- for username in list(cls.line_dict.keys()):
- try:
-
- _ws, last_live_at, _id = cls.line_dict.get(username)
- methods.debug_log(f"LineManage.check_loop87", f"#username: {username}"
- f" | #last_live_at: {last_live_at}"
- f" | #now_ts: {now_ts}")
-
- if now_ts - last_live_at >= 3 * 60:
- cls.line_dict.pop(username)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- except Exception as exception:
-
- if not cls.check_line_is_live(username):
- cls.line_dict.pop(username)
- if exception.__class__.__name__ == 'RuntimeError':
- methods.debug_log(f"LineManage.check_loop194", f"d2: {cls.get_line_state()}")
- else:
- methods.debug_log("LineManage.check_loop194", f"#e: {exception.__class__.__name__}")
- methods.debug_log("LineManage.check_loop194", f"#t: {traceback.format_exc()}")
-
- methods.debug_log(f"LineManage.check_loop198", f"wait 1 minutes check again")
- await asyncio.sleep(60)
-
-
- except Exception as exception:
- methods.debug_log(f"LineManage.check_loop208", f"#e: {exception.__class__.__name__}")
- methods.debug_log(f"LineManage.check_loop208", f"#t: {traceback.format_exc()}")
- methods.debug_log(f"LineManage.check_loop208", f"wait 1 minutes try again!")
- await asyncio.sleep(60)
- @classmethod
- def get_line_total(cls):
- count = 0
- for k, v in cls.line_dict.items():
- count += 1
- return count
- @classmethod
- def check_line_is_live(cls, line_id):
- d1 = {
- 0: 'CONNECTING',
- 1: 'CONNECTED',
- 2: 'DISCONNECTED',
- }
- _ws, _, _ = cls.line_dict.get(line_id)
- if _ws and d1.get(_ws.client_state.value) != 'DISCONNECTED':
- return True
- else:
- return False
- @classmethod
- def get_line_state(cls):
- d1 = {
- 0: 'CONNECTING',
- 1: 'CONNECTED',
- 2: 'DISCONNECTED',
- }
- d2 = dict()
- for line_id, line in cls.line_dict.items():
- state = d1.get(line.client_state.value)
- _id = line_id[-6:]
- d2[_id] = state
- return d2
-
-
-
-
-
-
-
|