测试Gunicorn通过,网页可以打开
This commit is contained in:
51
threads.py
51
threads.py
@@ -778,12 +778,13 @@ class GunicornTestThread(QThread):
|
||||
result_ready = Signal(bool, str)
|
||||
progress_updated = Signal(int)
|
||||
|
||||
def __init__(self, ssh_client, django_path, port="8000"):
|
||||
def __init__(self, ssh_client, django_path, port="8000", host="127.0.0.1"):
|
||||
super().__init__()
|
||||
self.ssh_client = ssh_client
|
||||
self.django_path = django_path
|
||||
self.port = port
|
||||
logger.info(f"GunicornTestThread初始化 - Django路径: {django_path}, 端口: {port}")
|
||||
self.host = host
|
||||
logger.info(f"GunicornTestThread初始化 - Django路径: {django_path}, 端口: {port}, 主机: {host}")
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
@@ -845,11 +846,36 @@ class GunicornTestThread(QThread):
|
||||
logger.info(f"端口{self.port}未被占用")
|
||||
|
||||
# 测试Gunicorn启动,使用构造函数中传入的端口参数,指定4个worker
|
||||
test_command = f"cd {self.django_path} && gunicorn --workers 4 --bind 0.0.0.0:{self.port} {project_name}.wsgi:application --timeout 5"
|
||||
test_command = f"cd {self.django_path} && gunicorn --workers 4 --bind 0.0.0.0:{self.port} {project_name}.wsgi:application --timeout 5 --log-level debug --access-logfile - --error-logfile -"
|
||||
logger.info(f"执行Gunicorn测试命令: {test_command}")
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command(test_command)
|
||||
time.sleep(3) # 等待Gunicorn启动
|
||||
|
||||
# 捕获Gunicorn启动日志
|
||||
gunicorn_stdout = stdout.read().decode()
|
||||
gunicorn_stderr = stderr.read().decode()
|
||||
|
||||
# 记录Gunicorn启动日志
|
||||
if gunicorn_stdout:
|
||||
logger.info(f"Gunicorn标准输出: {gunicorn_stdout[:500]}..." if len(gunicorn_stdout) > 500 else f"Gunicorn标准输出: {gunicorn_stdout}")
|
||||
if gunicorn_stderr:
|
||||
logger.error(f"Gunicorn错误输出: {gunicorn_stderr[:500]}..." if len(gunicorn_stderr) > 500 else f"Gunicorn错误输出: {gunicorn_stderr}")
|
||||
|
||||
# 检查错误日志中的关键词
|
||||
error_keywords = ["ImportError", "ModuleNotFoundError", "DatabaseError", "ConnectionError", "OperationalError", "ProgrammingError", "Exception", "Error", "Failed"]
|
||||
found_errors = []
|
||||
|
||||
for keyword in error_keywords:
|
||||
if keyword in gunicorn_stderr:
|
||||
found_errors.append(keyword)
|
||||
logger.error(f"在Gunicorn错误日志中发现关键词: {keyword}")
|
||||
|
||||
if found_errors:
|
||||
error_msg = f"Gunicorn启动失败,发现错误关键词: {', '.join(found_errors)}"
|
||||
logger.error(error_msg)
|
||||
self.result_ready.emit(False, error_msg)
|
||||
return
|
||||
|
||||
# 检查Gunicorn进程状态
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command("ps aux | grep gunicorn")
|
||||
output = stdout.read().decode()
|
||||
@@ -899,10 +925,25 @@ class GunicornTestThread(QThread):
|
||||
logger.error(f"Gunicorn主进程存在但Worker进程未启动,可能是Django代码/配置错误")
|
||||
# 尝试手动启动Gunicorn并获取详细错误日志
|
||||
logger.info(f"尝试手动启动Gunicorn以获取详细错误日志")
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command(f"cd {self.django_path} && gunicorn --workers 4 --bind 0.0.0.0:{self.port} {project_name}.wsgi:application --timeout 5 --error-logfile -")
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command(f"cd {self.django_path} && gunicorn --workers 4 --bind 0.0.0.0:{self.port} {project_name}.wsgi:application --timeout 5 --log-level debug --access-logfile - --error-logfile -")
|
||||
time.sleep(2)
|
||||
error_output = stderr.read().decode()
|
||||
logger.error(f"手动启动Gunicorn错误日志: {error_output}")
|
||||
|
||||
# 检查错误日志中的关键词
|
||||
error_keywords = ["ImportError", "ModuleNotFoundError", "DatabaseError", "ConnectionError", "OperationalError", "ProgrammingError", "Exception", "Error", "Failed"]
|
||||
found_errors = []
|
||||
|
||||
for keyword in error_keywords:
|
||||
if keyword in error_output:
|
||||
found_errors.append(keyword)
|
||||
logger.error(f"在手动启动Gunicorn错误日志中发现关键词: {keyword}")
|
||||
|
||||
if found_errors:
|
||||
error_msg = f"Gunicorn Worker进程未启动,发现错误关键词: {', '.join(found_errors)}"
|
||||
logger.error(error_msg)
|
||||
self.result_ready.emit(False, error_msg)
|
||||
return
|
||||
|
||||
# 清理测试进程
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command(f"pkill -f 'gunicorn.*{self.port}'")
|
||||
@@ -932,7 +973,7 @@ class GunicornTestThread(QThread):
|
||||
logger.info(f"端口{self.port}处于LISTEN状态,开始本地请求测试")
|
||||
|
||||
# 本地发起请求测试
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command(f"curl -s http://127.0.0.1:{self.port}")
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command(f"curl -s http://{self.host}:{self.port}")
|
||||
curl_output = stdout.read().decode()
|
||||
curl_exit_status = stdout.channel.recv_exit_status()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user