forked from Lesin/reposync
87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Issue同步独立启动脚本
|
|
"""
|
|
import os
|
|
import sys
|
|
import asyncio
|
|
from src.dao.issue_sync import issue_sync_dao
|
|
from src.service.issue_sync_cronjob import start_issue_sync_cronjob
|
|
from src.utils.issue_config import issue_sync_config
|
|
from src.utils.logger import logger
|
|
|
|
def load_env_config():
|
|
"""加载环境配置"""
|
|
ini_path = os.path.join(os.path.dirname(__file__), 'env.ini')
|
|
if os.path.exists(ini_path):
|
|
print(f"正在读取环境配置文件: {ini_path}")
|
|
with open(ini_path, 'r', encoding='utf-8') as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line or line.startswith('#'):
|
|
continue
|
|
if line.startswith('export '):
|
|
line = line[len('export '):]
|
|
if '=' in line:
|
|
key, value = line.split('=', 1)
|
|
key = key.strip()
|
|
value = value.strip().strip('"').strip("'")
|
|
os.environ[key] = value
|
|
print(f"设置环境变量: {key}={value}")
|
|
else:
|
|
print(f"警告: env.ini 文件不存在: {ini_path}")
|
|
|
|
def init_database():
|
|
"""初始化数据库"""
|
|
try:
|
|
print("正在初始化数据库表...")
|
|
issue_sync_dao.create_tables()
|
|
print("数据库表初始化完成")
|
|
return True
|
|
except Exception as e:
|
|
print(f"数据库初始化失败: {e}")
|
|
return False
|
|
|
|
def validate_config():
|
|
"""验证配置"""
|
|
try:
|
|
if not issue_sync_config.validate_config():
|
|
print("配置验证失败,请检查环境变量配置")
|
|
return False
|
|
print("配置验证通过")
|
|
return True
|
|
except Exception as e:
|
|
print(f"配置验证失败: {e}")
|
|
return False
|
|
|
|
async def main():
|
|
"""主函数"""
|
|
print("=== Issue同步服务启动 ===")
|
|
|
|
# 加载环境配置
|
|
load_env_config()
|
|
|
|
# 验证配置
|
|
if not validate_config():
|
|
print("配置验证失败,退出")
|
|
sys.exit(1)
|
|
|
|
# 初始化数据库
|
|
if not init_database():
|
|
print("数据库初始化失败,退出")
|
|
sys.exit(1)
|
|
|
|
# 启动同步任务
|
|
print("启动Issue同步定时任务...")
|
|
try:
|
|
await start_issue_sync_cronjob()
|
|
except KeyboardInterrupt:
|
|
print("\n收到中断信号,正在停止服务...")
|
|
except Exception as e:
|
|
logger.error(f"同步服务异常: {e}")
|
|
print(f"同步服务异常: {e}")
|
|
finally:
|
|
print("Issue同步服务已停止")
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main()) |