feat: 增加退出cursor
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import os
|
||||
|
||||
from exit_cursor import ExitCursor
|
||||
|
||||
os.environ["PYTHONVERBOSE"] = "0"
|
||||
os.environ["PYINSTALLER_VERBOSE"] = "0"
|
||||
|
||||
@@ -11,7 +13,7 @@ from logger import logging
|
||||
from browser_utils import BrowserManager
|
||||
from get_email_code import EmailVerificationHandler
|
||||
from logo import print_logo
|
||||
|
||||
import psutil
|
||||
|
||||
|
||||
def handle_turnstile(tab):
|
||||
@@ -219,10 +221,12 @@ class EmailGenerator:
|
||||
}
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print_logo()
|
||||
browser_manager = None
|
||||
try:
|
||||
ExitCursor()
|
||||
# 初始化浏览器
|
||||
browser_manager = BrowserManager()
|
||||
browser = browser_manager.init_browser()
|
||||
|
||||
68
exit_cursor.py
Normal file
68
exit_cursor.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import psutil
|
||||
from logger import logging
|
||||
import time
|
||||
|
||||
def ExitCursor(timeout=5):
|
||||
"""
|
||||
温和地关闭 Cursor 进程
|
||||
|
||||
Args:
|
||||
timeout (int): 等待进程自然终止的超时时间(秒)
|
||||
Returns:
|
||||
bool: 是否成功关闭所有进程
|
||||
"""
|
||||
try:
|
||||
logging.info("开始退出Cursor...")
|
||||
cursor_processes = []
|
||||
# 收集所有 Cursor 进程
|
||||
for proc in psutil.process_iter(['pid', 'name']):
|
||||
try:
|
||||
if proc.info['name'].lower() in ['cursor.exe', 'cursor']:
|
||||
cursor_processes.append(proc)
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
continue
|
||||
|
||||
if not cursor_processes:
|
||||
logging.info("未发现运行中的 Cursor 进程")
|
||||
return True
|
||||
|
||||
# 温和地请求进程终止
|
||||
for proc in cursor_processes:
|
||||
try:
|
||||
if proc.is_running():
|
||||
proc.terminate() # 发送终止信号
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
continue
|
||||
|
||||
# 等待进程自然终止
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < timeout:
|
||||
still_running = []
|
||||
for proc in cursor_processes:
|
||||
try:
|
||||
if proc.is_running():
|
||||
still_running.append(proc)
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
continue
|
||||
|
||||
if not still_running:
|
||||
logging.info("所有 Cursor 进程已正常关闭")
|
||||
return True
|
||||
|
||||
# 等待一小段时间再检查
|
||||
time.sleep(0.5)
|
||||
|
||||
# 如果超时后仍有进程在运行
|
||||
if still_running:
|
||||
process_list = ", ".join([str(p.pid) for p in still_running])
|
||||
logging.warning(f"以下进程未能在规定时间内关闭: {process_list}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"关闭 Cursor 进程时发生错误: {str(e)}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
ExitCursor()
|
||||
@@ -11,8 +11,17 @@ logging.basicConfig(
|
||||
filename=os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log"),
|
||||
level=logging.DEBUG,
|
||||
format="%(asctime)s - %(levelname)s - %(message)s",
|
||||
encoding='utf-8',
|
||||
)
|
||||
|
||||
# 创建控制台处理器
|
||||
console_handler = logging.StreamHandler()
|
||||
console_handler.setLevel(logging.INFO)
|
||||
console_handler.setFormatter(logging.Formatter("%(message)s"))
|
||||
|
||||
# 将控制台处理器添加到日志记录器
|
||||
logging.getLogger().addHandler(console_handler)
|
||||
|
||||
def main_task():
|
||||
"""
|
||||
Main task execution function. Simulates a workflow and handles errors.
|
||||
|
||||
Reference in New Issue
Block a user