Commit a697a601 authored by ibuler's avatar ibuler

[Update] 修改会话新建

parent 0981f8e6
...@@ -9,6 +9,7 @@ import threading ...@@ -9,6 +9,7 @@ import threading
import json import json
import signal import signal
import copy import copy
from collections import defaultdict
import psutil import psutil
...@@ -88,19 +89,19 @@ class Coco: ...@@ -88,19 +89,19 @@ class Coco:
# @ignore_error # @ignore_error
def heartbeat(self): def heartbeat(self):
sessions = list(Session.sessions.keys()) sessions = list(Session.sessions.keys())
p = psutil.Process(os.getpid()) # p = psutil.Process(os.getpid())
cpu_used = p.cpu_percent(interval=1.0) # cpu_used = p.cpu_percent(interval=1.0)
memory_used = int(p.memory_info().rss / 1024 / 1024) # memory_used = int(p.memory_info().rss / 1024 / 1024)
connections = len(p.connections()) # connections = len(p.connections())
threads = p.num_threads() # threads = p.num_threads()
session_online = len(sessions) # session_online = len(sessions)
data = { data = {
"cpu_used": cpu_used, # "cpu_used": cpu_used,
"memory_used": memory_used, # "memory_used": memory_used,
"connections": connections, # "connections": connections,
"threads": threads, # "threads": threads,
"boot_time": p.create_time(), # "boot_time": p.create_time(),
"session_online": session_online, # "session_online": session_online,
"sessions": sessions, "sessions": sessions,
} }
tasks = app_service.terminal_heartbeat(data) tasks = app_service.terminal_heartbeat(data)
...@@ -134,23 +135,38 @@ class Coco: ...@@ -134,23 +135,38 @@ class Coco:
def monitor_sessions_replay(self): def monitor_sessions_replay(self):
interval = 10 interval = 10
log_dir = os.path.join(config['LOG_DIR']) log_dir = os.path.join(config['LOG_DIR'])
max_try = 5
upload_failed = defaultdict(int)
def func(): def func():
while not self.stop_evt.is_set(): while not self.stop_evt.is_set():
active_sessions = [sid for sid in Session.sessions]
for filename in os.listdir(log_dir): for filename in os.listdir(log_dir):
suffix = filename.split('.')[-1]
if suffix != 'gz':
continue
session_id = filename.split('.')[0] session_id = filename.split('.')[0]
full_path = os.path.join(log_dir, filename)
if len(session_id) != 36: if len(session_id) != 36:
continue continue
full_path = os.path.join(log_dir, filename)
stat = os.stat(full_path)
# 是否是一天前的,因为现在多个coco共享了日志目录,
# 不能单纯判断session是否关闭
if stat.st_mtime > time.time() - 24*60*60:
continue
# 失败次数过多
if session_id in upload_failed \
and upload_failed[session_id] >= max_try:
continue
recorder = get_replay_recorder() recorder = get_replay_recorder()
if session_id not in active_sessions:
recorder.file_path = full_path recorder.file_path = full_path
ok = recorder.upload_replay(session_id, 1) ok = recorder.upload_replay(session_id, 1)
if not ok and os.path.getsize(full_path) == 0: if ok:
upload_failed.pop(session_id, None)
elif not ok and os.path.getsize(full_path) == 0:
os.unlink(full_path) os.unlink(full_path)
else:
upload_failed[session_id] += 1
time.sleep(1) time.sleep(1)
time.sleep(interval) time.sleep(interval)
thread = threading.Thread(target=func) thread = threading.Thread(target=func)
......
...@@ -344,7 +344,7 @@ defaults = { ...@@ -344,7 +344,7 @@ defaults = {
'LANGUAGE_CODE': 'zh', 'LANGUAGE_CODE': 'zh',
'SECURITY_MAX_IDLE_TIME': 60, 'SECURITY_MAX_IDLE_TIME': 60,
'ASSET_LIST_PAGE_SIZE': 'auto', 'ASSET_LIST_PAGE_SIZE': 'auto',
'SFTP_ROOT': 'tmp', 'SFTP_ROOT': '/tmp',
} }
......
...@@ -72,6 +72,12 @@ class ProxyServer: ...@@ -72,6 +72,12 @@ class ProxyServer:
self.server.close() self.server.close()
return return
session = Session.new_session(self.client, self.server) session = Session.new_session(self.client, self.server)
if not session:
msg = _("Connect with api server failed")
logger.error(msg)
self.client.send_unicode(msg)
self.server.close()
try: try:
session.bridge() session.bridge()
finally: finally:
......
...@@ -48,7 +48,14 @@ class Session: ...@@ -48,7 +48,14 @@ class Session:
session.set_command_recorder(command_recorder) session.set_command_recorder(command_recorder)
session.set_replay_recorder(replay_recorder) session.set_replay_recorder(replay_recorder)
cls.sessions[session.id] = session cls.sessions[session.id] = session
app_service.create_session(session.to_json()) _session = None
for i in range(5):
_session = app_service.create_session(session.to_json())
if _session:
break
time.sleep(0.2)
if _session is None:
return None
return session return session
@classmethod @classmethod
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment