forked from pajbot/pajbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·74 lines (51 loc) · 1.63 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
#!/usr/bin/env python3
import logging
import os
import signal
import sys
from pajbot.bot import Bot
from pajbot.utils import parse_args
try:
basestring
except NameError:
basestring = str
# XXX: What does this achieve exactly?
os.chdir(os.path.dirname(os.path.realpath(__file__)))
log = logging.getLogger(__name__)
def run(args):
from pajbot.utils import load_config
config = load_config(args.config)
if "main" not in config:
log.error("Missing section [main] in config")
sys.exit(1)
if "sql" in config:
log.error(
"The [sql] section in config is no longer used. See the example config for the new format under [main]."
)
sys.exit(1)
if "db" not in config["main"]:
log.error("Missing required db config in the [main] section.")
sys.exit(1)
pajbot = Bot(config, args)
pajbot.connect()
def on_sigterm(signal, frame):
pajbot.quit_bot()
sys.exit(0)
signal.signal(signal.SIGTERM, on_sigterm)
try:
pajbot.start()
except KeyboardInterrupt:
pajbot.quit_bot()
def handle_exceptions(exctype, value, tb):
log.error("Logging an uncaught exception", exc_info=(exctype, value, tb))
if __name__ == "__main__":
from pajbot.utils import init_logging, dump_threads
def on_sigusr1(signal, frame):
log.info("Process was interrupted with SIGUSR1, dumping all thread stack traces")
dump_threads()
# dump all stack traces on SIGUSR1
signal.signal(signal.SIGUSR1, on_sigusr1)
sys.excepthook = handle_exceptions
args = parse_args()
init_logging("pajbot")
run(args)