增加查看Gunicorn的日志的按钮和功能

This commit is contained in:
2025-08-29 21:18:47 +08:00
parent 59b1f0e92a
commit 901b978cc8
10 changed files with 736 additions and 463 deletions

View File

@@ -691,45 +691,63 @@ class GunicornTestThread(QThread):
self.progress_updated.emit(30)
# 检查wsgi.py文件
project_name = os.path.basename(self.django_path.rstrip('/'))
wsgi_path = f"{self.django_path.rstrip('/')}/{project_name}/wsgi.py"
stdin, stdout, stderr = self.ssh_client.exec_command(f"ls -la {wsgi_path}")
exit_status = stdout.channel.recv_exit_status()
# 检查wsgi.py文件 - 先尝试项目根目录
stdin, stdout, stderr = self.ssh_client.exec_command(f"find {self.django_path} -name wsgi.py -type f")
wsgi_files = stdout.read().decode().strip().split('\n')
if exit_status != 0:
# 尝试在Django项目根目录下查找wsgi.py文件
stdin, stdout, stderr = self.ssh_client.exec_command(f"find {self.django_path} -name wsgi.py")
exit_status = stdout.channel.recv_exit_status()
if exit_status == 0:
wsgi_path = stdout.read().decode().strip()
logger.info(f"找到wsgi.py文件: {wsgi_path}")
else:
self.result_ready.emit(False, f"wsgi.py文件不存在: {wsgi_path}")
logger.error(f"wsgi.py文件不存在: {wsgi_path}")
return
if not wsgi_files or wsgi_files == ['']:
self.result_ready.emit(False, f"未找到wsgi.py文件")
logger.error(f"未找到wsgi.py文件")
return
# 使用第一个找到的wsgi.py文件
wsgi_file = wsgi_files[0].strip()
logger.info(f"找到wsgi.py文件: {wsgi_file}")
# 获取项目目录的相对路径
project_dir = os.path.dirname(wsgi_file)
project_name = os.path.basename(project_dir)
# 如果wsgi.py在子目录中调整项目名
if project_dir != self.django_path.rstrip('/'):
# wsgi.py在子目录中使用子目录名作为项目名
project_name = os.path.basename(project_dir)
else:
# wsgi.py在项目根目录中使用manage.py所在目录名作为项目名
project_name = os.path.basename(self.django_path.rstrip('/'))
self.progress_updated.emit(50)
# 测试Gunicorn启动
stdin, stdout, stderr = self.ssh_client.exec_command(f"cd {self.django_path} && gunicorn --workers 2 --bind 0.0.0.0:8001 {project_name}.wsgi:application --timeout 10 &")
time.sleep(5) # 等待Gunicorn启动
test_command = f"cd {self.django_path} && timeout 10 gunicorn --workers 1 --bind 0.0.0.0:8001 {project_name}.wsgi:application --timeout 5"
stdin, stdout, stderr = self.ssh_client.exec_command(test_command)
time.sleep(3) # 等待Gunicorn启动
# 检查Gunicorn是否运行
stdin, stdout, stderr = self.ssh_client.exec_command("ps aux | grep gunicorn")
output = stdout.read().decode()
if "gunicorn" in output and "--bind 0.0.0.0:8001" in output:
# 停止测试进程
stdin, stdout, stderr = self.ssh_client.exec_command("pkill -f 'gunicorn --bind 0.0.0.0:8001'")
# 清理测试进程
stdin, stdout, stderr = self.ssh_client.exec_command("pkill -f 'gunicorn.*8001'")
if "gunicorn" in output and "8001" in output:
self.progress_updated.emit(100)
self.result_ready.emit(True, "Gunicorn测试成功")
logger.info("Gunicorn测试成功")
self.result_ready.emit(True, f"Gunicorn测试成功 - 项目: {project_name}")
logger.info(f"Gunicorn测试成功 - 项目: {project_name}")
else:
self.result_ready.emit(False, "Gunicorn测试失败")
logger.error("Gunicorn测试失败")
# 尝试更简单的测试方式
self.progress_updated.emit(80)
test_command2 = f"cd {self.django_path} && python3 -c \"import {project_name}.wsgi; print('WSGI导入成功')\""
stdin, stdout, stderr = self.ssh_client.exec_command(test_command2)
exit_status = stdout.channel.recv_exit_status()
if exit_status == 0:
self.result_ready.emit(True, f"WSGI配置验证成功 - 项目: {project_name}")
logger.info(f"WSGI配置验证成功 - 项目: {project_name}")
else:
error_output = stderr.read().decode()
self.result_ready.emit(False, f"Gunicorn测试失败: {error_output}")
logger.error(f"Gunicorn测试失败: {error_output}")
except Exception as e:
error_msg = str(e)