Commit b08059a1 authored by ibuler's avatar ibuler

[Bugfix] 修复无法登陆后引起的异常,增加上传录像后删除记录

parent d151c04c
...@@ -85,9 +85,12 @@ class ProxyServer: ...@@ -85,9 +85,12 @@ class ProxyServer:
width=width, height=height) width=width, height=height)
if not chan: if not chan:
self.client.send(warning(wr(msg, before=1, after=0))) self.client.send(warning(wr(msg, before=1, after=0)))
server = None
else:
server = Server(chan, asset, system_user)
self.connecting = False self.connecting = False
self.client.send(b'\r\n') self.client.send(b'\r\n')
return Server(chan, asset, system_user) return server
def watch_win_size_change(self): def watch_win_size_change(self):
while self.client.request.change_size_event.wait(): while self.client.request.change_size_event.wait():
......
...@@ -89,9 +89,13 @@ class CommandRecorder: ...@@ -89,9 +89,13 @@ class CommandRecorder:
class ServerReplayRecorder(ReplayRecorder): class ServerReplayRecorder(ReplayRecorder):
time_start = None
storage = None
def __init__(self, app): def __init__(self, app):
super().__init__(app) super().__init__(app)
self.file = None self.file = None
self.file_path = None
def record(self, data): def record(self, data):
""" """
...@@ -103,84 +107,72 @@ class ServerReplayRecorder(ReplayRecorder): ...@@ -103,84 +107,72 @@ class ServerReplayRecorder(ReplayRecorder):
},...] },...]
:return: :return:
""" """
# Todo: <liuzheng712@gmail.com>
if len(data['data']) > 0: if len(data['data']) > 0:
# print(json.dumps( timedelta = data['timestamp'] - self.time_start
# data['data'].decode('utf-8', 'replace'))) data = json.dumps(data['data'].decode('utf-8', 'replace'))
self.file.write( self.file.write('"{}":{},'.format(timedelta, data))
'"' + str(data['timestamp'] - self.starttime) + '":' + json.dumps(
data['data'].decode('utf-8', 'replace')) + ',')
def session_start(self, session_id): def session_start(self, session_id):
self.starttime = time.time() self.time_start = time.time()
self.file = open(os.path.join( filename = session_id+'.replay.gz'
self.app.config['LOG_DIR'], session_id + '.replay' self.file_path = os.path.join(self.app.config['LOG_DIR'], filename)
), 'a') self.file = gzip.open(self.file_path, 'at')
self.file.write('{') self.file.write('{')
def session_end(self, session_id): def session_end(self, session_id):
self.file.write('"0":""}') self.file.write('"0":""}')
self.file.close() self.file.close()
with open(os.path.join(self.app.config['LOG_DIR'], session_id + '.replay'), 'rb') as f_in, \
gzip.open(os.path.join(self.app.config['LOG_DIR'], session_id + '.replay.gz'), 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
if self.upload_replay(session_id): if self.upload_replay(session_id):
logger.info("Succeed to push {}'s {}".format(session_id, "record")) logger.info("Succeed to push {}'s {}".format(session_id, "record"))
else: else:
logger.error("Failed to push {}'s {}".format(session_id, "record")) logger.error("Failed to push {}'s {}".format(session_id, "record"))
self.upload_replay(session_id)
def upload_replay(self, session_id): def upload_replay(self, session_id):
configs = self.app.service.load_config_from_server() configs = self.app.service.load_config_from_server()
logger.debug("upload_replay print config: {}".format(configs)) logger.debug("upload_replay print config: {}".format(configs))
self.client = jms_storage.init(configs["REPLAY_STORAGE"]) self.storage = jms_storage.init(configs["REPLAY_STORAGE"])
if not self.client: if not self.storage:
self.client = jms_storage.jms(self.app.service) self.storage = jms_storage.jms(self.app.service)
if self.push_storage(3, session_id): if self.push_file(3, session_id):
if self.finish_replay(3, session_id): os.unlink(self.file_path)
return True return True
else: else:
return False return False
else:
return False
def push_to_storage(self, session_id): def push_to_storage(self, session_id):
return self.client.upload_file( dt = time.strftime('%Y-%m-%d', time.localtime(self.time_start))
os.path.join(self.app.config['LOG_DIR'], session_id + '.replay.gz'), target = dt + '/' + session_id + '.replay.gz'
time.strftime('%Y-%m-%d', time.localtime(self.starttime)) + '/' + session_id + '.replay.gz') return self.storage.upload_file(self.file_path, target)
def push_file(self, times, session_id):
if times < 0:
if self.storage.type() == 'jms':
return False
else:
msg = "Failed push session {}'s replay log to storage".format(session_id)
logger.error(msg)
self.storage = jms_storage.jms(self.app.service)
return self.push_file(3, session_id)
def push_storage(self, times, session_id):
if times > 0:
if self.push_to_storage(session_id): if self.push_to_storage(session_id):
logger.info("success push session: {}'s replay log to storage ".format(session_id)) logger.info("Success push session: {}'s replay log to storage ".format(session_id))
return True return True
else: else:
logger.error( msg = "Failed push session {}'s replay log to storage, try {} times".format(session_id, times)
"failed report session {}'s replay log to storage, try {} times".format(session_id, times)) logger.error(msg)
return self.push_storage(times - 1, session_id) return self.push_file(times - 1, session_id)
else:
logger.error("failed report session {}'s replay log storage, try to push to local".format(session_id))
if self.client.type() == 'jms':
return False
else:
self.client = jms_storage.jms(self.app.service)
return self.push_storage(3, session_id)
def finish_replay(self, times, session_id): def finish_replay(self, times, session_id):
if times > 0: if times < 0:
logger.error("Failed finished session {}'s replay".format(session_id))
return False
if self.app.service.finish_replay(session_id): if self.app.service.finish_replay(session_id):
logger.info("success report session {}'s replay log ".format(session_id)) logger.info("Success finish session {}'s replay ".format(session_id))
return True return True
else: else:
logger.error("failed report session {}'s replay log, try {} times".format(session_id, times)) logger.error("Failed finish session {}'s replay, try {} times".format(session_id, times))
return self.finish_replay(times - 1, session_id) return self.finish_replay(times - 1, session_id)
else:
logger.error("failed report session {}'s replay log".format(session_id))
return False
# def __del__(self):
# print("GC: Server replay recorder has been gc")
# del self.file
class ServerCommandRecorder(CommandRecorder, metaclass=Singleton): class ServerCommandRecorder(CommandRecorder, metaclass=Singleton):
......
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