-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
50 lines (36 loc) · 1.04 KB
/
app.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
# encoding:utf-8
import signal
import sys
import time
from common.log import logger
from config import conf, load_config
from channel.wechat.wechat_channel import WechatChannel
def sigterm_handler_wrap(_signo):
old_handler = signal.getsignal(_signo)
def func(_signo, _stack_frame):
logger.info("signal {} received, exiting...".format(_signo))
conf().save_user_datas()
if callable(old_handler): # check old_handler
return old_handler(_signo, _stack_frame)
sys.exit(0)
signal.signal(_signo, func)
def start_channel():
channel = WechatChannel()
channel.startup()
def run():
try:
# load config
load_config()
# ctrl + c
sigterm_handler_wrap(signal.SIGINT)
# kill signal
sigterm_handler_wrap(signal.SIGTERM)
# create channel
start_channel()
while True:
time.sleep(1)
except Exception as e:
logger.error("App startup failed!")
logger.exception(e)
if __name__ == "__main__":
run()