创建了 create_terminal 方法;没有子进程时,自动调用 create_terminal 重新创建
This commit is contained in:
@@ -123,25 +123,24 @@ def read_and_forward_pty_output(fd=None, pid=None, room_id=None, namespace=None)
|
||||
pass
|
||||
|
||||
class VSCLikeNameSpace(Namespace):
|
||||
def on_connect(self):
|
||||
"""new client connected"""
|
||||
# 为每个客户端生成唯一的会话ID和房间ID
|
||||
session_id = str(uuid.uuid4())
|
||||
room_id = f"terminal_{session_id}"
|
||||
def create_terminal(self):
|
||||
"""创建新的终端并更新session配置"""
|
||||
terminal_config = session.get('terminal_config', {})
|
||||
|
||||
# 加入房间
|
||||
join_room(room_id)
|
||||
# 如果没有房间ID,创建一个新的
|
||||
if not terminal_config.get('room_id'):
|
||||
session_id = str(uuid.uuid4())
|
||||
room_id = f"terminal_{session_id}"
|
||||
join_room(room_id)
|
||||
terminal_config.update({
|
||||
**TERM_INIT_CONFIG.copy(),
|
||||
'session_id': session_id,
|
||||
'room_id': room_id
|
||||
})
|
||||
else:
|
||||
room_id = terminal_config['room_id']
|
||||
|
||||
# 确保terminal_config存在于session中,每个客户端有独立配置
|
||||
terminal_config = {
|
||||
**TERM_INIT_CONFIG.copy(),
|
||||
'session_id': session_id,
|
||||
'room_id': room_id
|
||||
}
|
||||
session['terminal_config'] = terminal_config
|
||||
session.modified = True
|
||||
|
||||
current_app.logger.debug(f"Client connected, session_id: {session_id}, room_id: {room_id}")
|
||||
current_app.logger.debug(f"Creating new terminal for room: {room_id}")
|
||||
|
||||
# create child process attached to a pty we can read from and write to
|
||||
(child_pid, fd) = pty.fork()
|
||||
@@ -200,11 +199,36 @@ class VSCLikeNameSpace(Namespace):
|
||||
|
||||
# 记录日志
|
||||
current_app.logger.debug(f"Child process created: {child_pid} for room: {room_id}")
|
||||
current_app.logger.debug(f"Client joined room: {room_id}")
|
||||
|
||||
# 启动后台任务读取pty输出,传入正确的room_id
|
||||
socketio.start_background_task(read_and_forward_pty_output, fd, child_pid, room_id, self)
|
||||
current_app.logger.debug("background task running")
|
||||
current_app.logger.debug("Background task running")
|
||||
|
||||
return terminal_config
|
||||
|
||||
def on_connect(self):
|
||||
"""new client connected"""
|
||||
# 为每个客户端生成唯一的会话ID和房间ID
|
||||
session_id = str(uuid.uuid4())
|
||||
room_id = f"terminal_{session_id}"
|
||||
|
||||
# 加入房间
|
||||
join_room(room_id)
|
||||
|
||||
# 确保terminal_config存在于session中,每个客户端有独立配置
|
||||
terminal_config = {
|
||||
**TERM_INIT_CONFIG.copy(),
|
||||
'session_id': session_id,
|
||||
'room_id': room_id
|
||||
}
|
||||
session['terminal_config'] = terminal_config
|
||||
session.modified = True
|
||||
|
||||
current_app.logger.debug(f"Client connected, session_id: {session_id}, room_id: {room_id}")
|
||||
|
||||
# 创建终端
|
||||
self.create_terminal()
|
||||
current_app.logger.debug("background task running")
|
||||
|
||||
def on_pty_input(self, data):
|
||||
"""write to the child pty, which now is the ssh process from this machine to the 'domain' configured
|
||||
@@ -227,36 +251,50 @@ class VSCLikeNameSpace(Namespace):
|
||||
try:
|
||||
terminal_config = session.get('terminal_config', {})
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
|
||||
# 如果没有子进程,重新创建终端
|
||||
if not child_pid:
|
||||
current_app.logger.error("No child process found")
|
||||
return
|
||||
current_app.logger.debug("No child process found, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
if not child_pid:
|
||||
current_app.logger.error("Failed to create new terminal")
|
||||
return
|
||||
|
||||
child_process = psutil.Process(child_pid)
|
||||
if child_process.status() not in ('running', 'sleeping'):
|
||||
current_app.logger.debug("Child process not running, cleaning up")
|
||||
terminal_config.pop('child_pid', None)
|
||||
terminal_config.pop('fd', None)
|
||||
session.modified = True
|
||||
return
|
||||
# 检查子进程是否存在且运行中
|
||||
try:
|
||||
child_process = psutil.Process(child_pid)
|
||||
if child_process.status() not in ('running', 'sleeping'):
|
||||
current_app.logger.debug("Child process not running, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
if not child_pid:
|
||||
current_app.logger.error("Failed to create new terminal")
|
||||
return
|
||||
except psutil.NoSuchProcess:
|
||||
current_app.logger.debug("Child process not found, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
if not child_pid:
|
||||
current_app.logger.error("Failed to create new terminal")
|
||||
return
|
||||
|
||||
# 获取文件描述符并写入数据
|
||||
fd = terminal_config.get('fd')
|
||||
if fd:
|
||||
try:
|
||||
os.write(fd, input_data.encode())
|
||||
except (OSError, IOError) as e:
|
||||
# File descriptor closed or invalid, clean up
|
||||
current_app.logger.debug(f"Error writing to file descriptor: {e}")
|
||||
terminal_config.pop('child_pid', None)
|
||||
terminal_config.pop('fd', None)
|
||||
session.modified = True
|
||||
except psutil.NoSuchProcess:
|
||||
# Process no longer exists, clean up
|
||||
terminal_config = session.get('terminal_config', {})
|
||||
terminal_config.pop('child_pid', None)
|
||||
terminal_config.pop('fd', None)
|
||||
session.modified = True
|
||||
# File descriptor closed or invalid, create new terminal
|
||||
current_app.logger.debug(f"Error writing to file descriptor: {e}, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error in pty_input handling: {e}")
|
||||
# 发生异常时尝试重新创建终端
|
||||
try:
|
||||
self.create_terminal()
|
||||
except Exception as create_err:
|
||||
current_app.logger.error(f"Failed to recreate terminal: {create_err}")
|
||||
|
||||
|
||||
def on_resize(self, data):
|
||||
@@ -281,39 +319,51 @@ class VSCLikeNameSpace(Namespace):
|
||||
try:
|
||||
terminal_config = session.get('terminal_config', {})
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
|
||||
# 如果没有子进程,重新创建终端
|
||||
if not child_pid:
|
||||
current_app.logger.error("No child process found for resize")
|
||||
return
|
||||
current_app.logger.debug("No child process found for resize, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
if not child_pid:
|
||||
current_app.logger.error("Failed to create new terminal for resize")
|
||||
return
|
||||
|
||||
# 检查子进程是否存在且运行中
|
||||
child_process = psutil.Process(child_pid)
|
||||
if child_process.status() not in ('running', 'sleeping'):
|
||||
current_app.logger.debug("Child process not running, cleaning up resize")
|
||||
terminal_config.pop('child_pid', None)
|
||||
terminal_config.pop('fd', None)
|
||||
session.modified = True
|
||||
return
|
||||
try:
|
||||
child_process = psutil.Process(child_pid)
|
||||
if child_process.status() not in ('running', 'sleeping'):
|
||||
current_app.logger.debug("Child process not running for resize, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
if not child_pid:
|
||||
current_app.logger.error("Failed to create new terminal for resize")
|
||||
return
|
||||
except psutil.NoSuchProcess:
|
||||
current_app.logger.debug("Child process not found for resize, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
child_pid = terminal_config.get('child_pid')
|
||||
if not child_pid:
|
||||
current_app.logger.error("Failed to create new terminal for resize")
|
||||
return
|
||||
|
||||
# 获取文件描述符并调整窗口大小
|
||||
fd = terminal_config.get('fd')
|
||||
if fd:
|
||||
# 检查文件描述符是否有效
|
||||
try:
|
||||
os.fstat(fd)
|
||||
set_winsize(fd, rows, cols)
|
||||
except (OSError, IOError) as e:
|
||||
# 文件描述符无效,清理资源
|
||||
current_app.logger.debug(f"Error resizing terminal: {e}")
|
||||
terminal_config.pop('child_pid', None)
|
||||
terminal_config.pop('fd', None)
|
||||
session.modified = True
|
||||
except psutil.NoSuchProcess:
|
||||
# 进程不存在,清理资源
|
||||
terminal_config = session.get('terminal_config', {})
|
||||
terminal_config.pop('child_pid', None)
|
||||
terminal_config.pop('fd', None)
|
||||
session.modified = True
|
||||
# 文件描述符无效,创建新终端
|
||||
current_app.logger.debug(f"Error resizing terminal: {e}, creating new terminal")
|
||||
terminal_config = self.create_terminal()
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error in resize handling: {e}")
|
||||
# 发生异常时尝试重新创建终端
|
||||
try:
|
||||
self.create_terminal()
|
||||
except Exception as create_err:
|
||||
current_app.logger.error(f"Failed to recreate terminal for resize: {create_err}")
|
||||
|
||||
def on_disconnect(self):
|
||||
terminal_config = session.get('terminal_config', {})
|
||||
|
||||
Reference in New Issue
Block a user