xx
This commit is contained in:
@@ -49,13 +49,25 @@ class AutoCursorService:
|
||||
# 子进程控制
|
||||
self.registration_process = None
|
||||
self.reg_enabled = False
|
||||
self.check_interval = 60 # 检查API状态的间隔(秒)
|
||||
self.email_check_threshold = 30 # 当可用邮箱少于这个数时获取新邮箱
|
||||
self.email_fetch_count = 2 # 获取邮箱的次数(每次15个)
|
||||
|
||||
# 从配置中获取自动服务参数
|
||||
auto_service_config = getattr(self.config, "auto_service_config", None)
|
||||
if auto_service_config:
|
||||
self.check_interval = getattr(auto_service_config, "check_interval", 60)
|
||||
self.upload_interval = getattr(auto_service_config, "upload_interval", 300)
|
||||
self.email_check_threshold = getattr(auto_service_config, "email_check_threshold", 30)
|
||||
self.email_fetch_count = getattr(auto_service_config, "email_fetch_count", 2)
|
||||
else:
|
||||
self.check_interval = 60 # 检查API状态的间隔(秒)
|
||||
self.upload_interval = 300 # 上传账号间隔(秒)
|
||||
self.email_check_threshold = 30 # 当可用邮箱少于这个数时获取新邮箱
|
||||
self.email_fetch_count = 2 # 获取邮箱的次数(每次15个)
|
||||
|
||||
# 处理邮箱的最小数量阈值,只要有这么多邮箱就会立即处理
|
||||
self.min_email_to_process = 1
|
||||
|
||||
# 运行状态控制
|
||||
self.running = True
|
||||
self.upload_interval = 300 # 上传账号间隔(秒)
|
||||
|
||||
# 设置信号处理
|
||||
signal.signal(signal.SIGINT, self._handle_signal)
|
||||
@@ -236,6 +248,11 @@ class AutoCursorService:
|
||||
|
||||
if pending_count >= self.email_check_threshold:
|
||||
logger.info(f"可用邮箱数量充足 ({pending_count} >= {self.email_check_threshold})")
|
||||
# 确保注册进程已启动
|
||||
if pending_count >= self.min_email_to_process and self.reg_enabled:
|
||||
if not self.registration_process or self.registration_process.poll() is not None:
|
||||
logger.info("有足够邮箱但注册进程未运行,启动注册进程")
|
||||
self.start_registration_process()
|
||||
return 0
|
||||
|
||||
# 需要获取新邮箱
|
||||
@@ -251,6 +268,15 @@ class AutoCursorService:
|
||||
imported = await self.import_email_accounts(accounts)
|
||||
total_imported += imported
|
||||
|
||||
# 每次获取到新邮箱并成功导入后,立即确保注册进程在运行
|
||||
if imported > 0 and self.reg_enabled:
|
||||
new_count = pending_count + imported
|
||||
if new_count >= self.min_email_to_process:
|
||||
logger.info(f"已获取到 {imported} 个新邮箱,总可用邮箱数 {new_count},确保注册进程运行")
|
||||
if not self.registration_process or self.registration_process.poll() is not None:
|
||||
self.start_registration_process()
|
||||
pending_count = new_count # 更新计数器
|
||||
|
||||
if imported < 15: # 每次API应返回15个账号
|
||||
logger.warning(f"获取到的邮箱少于预期 ({imported} < 15),可能没有更多邮箱可用")
|
||||
break
|
||||
@@ -267,24 +293,36 @@ class AutoCursorService:
|
||||
logger.info("注册进程已在运行中")
|
||||
return
|
||||
|
||||
logger.info("启动注册进程")
|
||||
try:
|
||||
# 使用subprocess启动main.py
|
||||
if sys.platform.startswith("win"):
|
||||
self.registration_process = subprocess.Popen(
|
||||
["python", "main.py"],
|
||||
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
|
||||
)
|
||||
else:
|
||||
self.registration_process = subprocess.Popen(
|
||||
["python3", "main.py"]
|
||||
)
|
||||
# 先异步检查可用邮箱数量
|
||||
async def check_and_start():
|
||||
pending_count = await self.count_pending_accounts()
|
||||
if pending_count < self.min_email_to_process:
|
||||
logger.warning(f"可用邮箱数量不足 ({pending_count} < {self.min_email_to_process}),暂不启动注册进程")
|
||||
return
|
||||
|
||||
logger.info(f"注册进程已启动,PID: {self.registration_process.pid}")
|
||||
logger.info(f"有 {pending_count} 个可用邮箱,启动注册进程")
|
||||
try:
|
||||
# 使用subprocess启动main.py
|
||||
if sys.platform.startswith("win"):
|
||||
self.registration_process = subprocess.Popen(
|
||||
["python", "main.py"],
|
||||
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
|
||||
)
|
||||
else:
|
||||
self.registration_process = subprocess.Popen(
|
||||
["python3", "main.py"]
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"启动注册进程时出错: {e}")
|
||||
self.registration_process = None
|
||||
logger.info(f"注册进程已启动,PID: {self.registration_process.pid}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"启动注册进程时出错: {e}")
|
||||
self.registration_process = None
|
||||
|
||||
# 创建临时事件循环来运行异步检查
|
||||
loop = asyncio.new_event_loop()
|
||||
loop.run_until_complete(check_and_start())
|
||||
loop.close()
|
||||
|
||||
def stop_registration_process(self):
|
||||
"""停止注册进程"""
|
||||
@@ -366,8 +404,11 @@ class AutoCursorService:
|
||||
if self.reg_enabled:
|
||||
# 开启注册时,先检查并获取邮箱
|
||||
await self.check_and_fetch_emails()
|
||||
# 然后启动注册进程
|
||||
self.start_registration_process()
|
||||
# 检查是否有可用邮箱,有则启动注册进程
|
||||
pending_count = await self.count_pending_accounts()
|
||||
if pending_count >= self.min_email_to_process:
|
||||
logger.info(f"有 {pending_count} 个可用邮箱,启动注册进程")
|
||||
self.start_registration_process()
|
||||
else:
|
||||
# 关闭注册时,停止注册进程
|
||||
self.stop_registration_process()
|
||||
@@ -375,6 +416,12 @@ class AutoCursorService:
|
||||
# 2. 如果注册已开启,检查并获取邮箱
|
||||
if self.reg_enabled:
|
||||
await self.check_and_fetch_emails()
|
||||
# 确保注册进程正在运行
|
||||
if not self.registration_process or self.registration_process.poll() is not None:
|
||||
pending_count = await self.count_pending_accounts()
|
||||
if pending_count >= self.min_email_to_process:
|
||||
logger.info(f"发现 {pending_count} 个未处理的邮箱,重新启动注册进程")
|
||||
self.start_registration_process()
|
||||
|
||||
# 3. 定期上传账号
|
||||
current_time = time.time()
|
||||
|
||||
Reference in New Issue
Block a user