forked from cdhigh/KindleEar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (65 loc) · 2.74 KB
/
main.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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# KindleEar web application entrance
# Visit <https://github.com/cdhigh/KindleEar> for the latest version
# Author: cdhigh <https://github.com/cdhigh>
__Version__ = '3.0.0d'
import os, sys, builtins, logging
appDir = os.path.dirname(os.path.abspath(__file__))
#logName = None if (DATABASE_URL == 'datastore') else 'gunicorn.error'
log = logging.getLogger()
builtins.__dict__['default_log'] = log
builtins.__dict__['appDir'] = appDir
builtins.__dict__['appVer'] = __Version__
sys.path.insert(0, os.path.join(appDir, 'application', 'lib'))
#合并config.py配置信息到os.environ,如果对应环境变量存在,则不会覆盖
def set_env():
import config
cfgMap = {}
keys = ['APP_ID', 'APP_DOMAIN', 'SERVER_LOCATION', 'DATABASE_URL', 'TASK_QUEUE_SERVICE',
'TASK_QUEUE_BROKER_URL', 'KE_TEMP_DIR', 'DOWNLOAD_THREAD_NUM', 'ALLOW_SIGNUP',
'SECRET_KEY', 'ADMIN_NAME', 'POCKET_CONSUMER_KEY', 'HIDE_MAIL_TO_LOCAL', 'LOG_LEVEL']
for key in keys:
cfgMap[key] = os.getenv(key) if key in os.environ else getattr(config, key)
os.environ[key] = cfgMap[key]
return cfgMap
#设置logging的level
def set_log_level(level):
level = logging._nameToLevel.get(level.upper(), logging.WARNING) if isinstance(level, str) else level
for handler in logging.root.handlers:
handler.setLevel(level)
cfgMap = set_env()
from application import init_app
app = init_app(__name__, cfgMap, set_env, debug=False)
celery_app = app.extensions.get("celery", None)
set_log_level(cfgMap.get('LOG_LEVEL'))
def main():
if len(sys.argv) == 2 and sys.argv[1] == 'debug':
default_log.setLevel(logging.DEBUG)
app.run(host='0.0.0.0', debug=False)
return 0
elif len(sys.argv) >= 3:
act = sys.argv[1]
param = sys.argv[2]
if (act == 'deliver') and (param == 'check'):
from application.view.deliver import MultiUserDelivery
print(MultiUserDelivery())
return 0
elif (act == 'deliver') and (param == 'now'):
from application.work.worker import WorkerAllNow
print(WorkerAllNow())
return 0
elif (act == 'log') and (param == 'purge'):
from application.view.logs import RemoveLogs
print(RemoveLogs())
return 0
print(f'\nKindleEar Application {appVer}')
print('\nUsage: main.py commands')
print('\ncommands:')
print(' debug \t Run the application in debug mode')
print(' deliver check\t Start delivery if time set is matched')
print(' deliver now \t Force start delivery task')
print(' log purge \t remove logs older than one month')
print('\n')
if __name__ == "__main__":
sys.exit(main())