forked from NAStool/nas-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
167 lines (139 loc) · 4.72 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import signal
import sys
import warnings
from pyvirtualdisplay import Display
# 添加第三方库入口
with open(os.path.join(os.path.dirname(__file__),
"third_party.txt"), "r") as f:
third_party = f.readlines()
for third_party_lib in third_party:
sys.path.append(os.path.join(os.path.dirname(__file__),
"third_party",
third_party_lib.strip()).replace("\\", "/"))
# 运行环境判断
is_windows_exe = getattr(sys, 'frozen', False) and (os.name == "nt")
if is_windows_exe:
# 托盘相关库
import threading
from windows.trayicon import trayicon
# 初始化环境变量
os.environ["NASTOOL_CONFIG"] = os.path.join(os.path.dirname(sys.executable),
"config",
"config.yaml").replace("\\", "/")
os.environ["NASTOOL_LOG"] = os.path.join(os.path.dirname(sys.executable),
"config",
"logs").replace("\\", "/")
try:
config_dir = os.path.join(os.path.dirname(sys.executable),
"config").replace("\\", "/")
if not os.path.exists(config_dir):
os.makedirs(config_dir)
feapder_tmpdir = os.path.join(os.path.dirname(__file__),
"feapder",
"network",
"proxy_file").replace("\\", "/")
if not os.path.exists(feapder_tmpdir):
os.makedirs(feapder_tmpdir)
except Exception as err:
print(str(err))
# 启动虚拟显示
is_docker = os.path.exists('/.dockerenv')
if is_docker:
try:
display = Display(visible=False, size=(1920, 1080))
display.start()
os.environ['NASTOOL_DISPLAY'] = 'YES'
except Exception as err:
print(str(err))
else:
display = None
from config import CONFIG
import log
from web.main import App
from app.brushtask import BrushTask
from app.db import init_db, update_db
from app.helper import IndexerHelper
from app.rsschecker import RssChecker
from app.scheduler import run_scheduler
from app.sync import run_monitor
from check_config import update_config, check_config
from version import APP_VERSION
warnings.filterwarnings('ignore')
def sigal_handler(num, stack):
"""
信号处理
"""
if is_docker:
log.warn('捕捉到退出信号:%s,开始退出...' % num)
# 停止虚拟显示
if display:
display.stop()
# 退出主进程
sys.exit()
def get_run_config(cfg):
"""
获取运行配置
"""
_web_host = "::"
_web_port = 3000
_ssl_cert = None
_ssl_key = None
app_conf = cfg.get_config('app')
if app_conf:
if app_conf.get("web_host"):
_web_host = app_conf.get("web_host").replace('[', '').replace(']', '')
_web_port = int(app_conf.get('web_port')) if str(app_conf.get('web_port', '')).isdigit() else 3000
_ssl_cert = app_conf.get('ssl_cert')
_ssl_key = app_conf.get('ssl_key')
app_arg = dict(host=_web_host, port=_web_port, debug=False, threaded=True, use_reloader=False)
if _ssl_cert:
app_arg['ssl_context'] = (_ssl_cert, _ssl_key)
return app_arg
# 退出事件
signal.signal(signal.SIGINT, sigal_handler)
signal.signal(signal.SIGTERM, sigal_handler)
# 初始化
def init_system():
# 配置
log.console('NAStool 当前版本号:%s' % APP_VERSION)
# 数据库初始化
init_db()
# 数据库更新
update_db()
# 升级配置文件
update_config()
# 检查配置文件
check_config()
# 启动附属服务
def start_service():
log.console("开始启动进程...")
# 启动定时服务
run_scheduler()
# 启动监控服务
run_monitor()
# 启动刷流服务
BrushTask()
# 启动自定义订阅服务
RssChecker()
# 加载索引器配置
IndexerHelper()
# 系统初始化
init_system()
# 启动服务
start_service()
# 本地运行
if __name__ == '__main__':
# Windows启动托盘
if is_windows_exe:
homepage = CONFIG.get_config('app').get('domain')
if not homepage:
homepage = "http://localhost:%s" % str(CONFIG.get_config('app').get('web_port'))
log_path = os.environ.get("NASTOOL_LOG")
def traystart():
trayicon(homepage, log_path)
if len(os.popen("tasklist| findstr %s" % os.path.basename(sys.executable), 'r').read().splitlines()) <= 2:
p1 = threading.Thread(target=traystart, daemon=True)
p1.start()
# gunicorn 启动
App.run(**get_run_config(CONFIG))