forked from qd-today/qd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·98 lines (83 loc) · 3.12 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
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 Binux <roy@binux.me>
import asyncio
import json
import logging
import os
import platform
import sys
import tornado.log
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop, PeriodicCallback
import config
from db import DB, db_converter
from db.basedb import engine
from libs.log import Log
from web.app import Application
from worker import BatchWorker, QueueWorker
def start_server():
if sys.getdefaultencoding() != 'utf-8':
import importlib
importlib.reload(sys)
# init logging
logger = Log().getlogger()
logger_QD = Log('QD.Run').getlogger()
if config.debug:
channel = logging.StreamHandler(sys.stderr)
channel.setFormatter(tornado.log.LogFormatter())
channel.setLevel(logging.WARNING)
logger_QD.addHandler(channel)
if not config.accesslog:
tornado.log.access_log.disabled = True
else:
tornado.log.access_log = Log('tornado.access').getlogger()
# tornado.log.app_log = Log('tornado.application').getlogger()
if len(sys.argv) > 2 and sys.argv[1] == '-p' and sys.argv[2].isdigit():
port = int(sys.argv[2])
else:
port = config.port
if platform.system() == 'Windows':
config.multiprocess = False
if config.multiprocess and config.autoreload:
config.autoreload = False
try:
database = DB()
converter = db_converter.DBconverter(database)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
run = asyncio.ensure_future(converter.ConvertNewType(database) , loop=loop)
loop.run_until_complete(run)
default_version = json.load(open(os.path.join(os.path.dirname(__file__), 'version.json'),'r', encoding='utf-8'))['version']
App= Application(database, default_version)
http_server = HTTPServer(App, xheaders=True)
http_server.bind(port, config.bind)
if config.multiprocess:
http_server.start(num_processes=0)
else:
http_server.start()
io_loop = IOLoop.instance()
try:
if config.worker_method.upper() == 'QUEUE':
worker = QueueWorker(database)
io_loop.add_callback(worker)
elif config.worker_method.upper() == 'BATCH':
worker = BatchWorker(database)
PeriodicCallback(worker, config.check_task_loop).start()
else:
raise Exception('worker_method must be Queue or Batch, please check config!')
except Exception as e:
logger.exception('worker start error!')
raise KeyboardInterrupt()
logger_QD.info("Http Server started on %s:%s", config.bind, port)
io_loop.start()
except KeyboardInterrupt :
logger_QD.info("Http Server is being manually interrupted... ")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
run = asyncio.ensure_future(engine.dispose() , loop=loop)
loop.run_until_complete(run)
logger_QD.info("Http Server is ended. ")
if __name__ == "__main__":
start_server()