forked from Rongronggg9/RSS-to-Telegram-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
154 lines (128 loc) · 5.56 KB
/
log.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
# RSS to Telegram Bot
# Copyright (C) 2021-2024 Rongrong <i@rong.moe>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
from typing_extensions import Awaitable
import asyncio
import logging
import colorlog
import os
import signal
from . import env
getLogger = colorlog.getLogger
DEBUG = colorlog.DEBUG
INFO = colorlog.INFO
WARNING = colorlog.WARNING
ERROR = colorlog.ERROR
CRITICAL = colorlog.CRITICAL
logger_level_muted = colorlog.INFO if env.DEBUG else colorlog.WARNING
logger_level_shut_upped = colorlog.ERROR if env.DEBUG else colorlog.CRITICAL
getLogger('aiohttp_retry').setLevel(logger_level_muted)
getLogger('asyncio').setLevel(logger_level_muted)
getLogger('telethon').setLevel(logger_level_muted)
getLogger('aiosqlite').setLevel(logger_level_muted)
getLogger('tortoise').setLevel(logger_level_muted)
getLogger('asyncpg').setLevel(logger_level_muted)
getLogger('PIL').setLevel(logger_level_muted)
getLogger('matplotlib').setLevel(logger_level_muted)
getLogger('matplotlib.font_manager').setLevel(logger_level_shut_upped)
_logger = getLogger('RSStT.watchdog')
async def exit_handler(prerequisite: Awaitable = None):
try:
if prerequisite and env.bot.is_connected():
try:
await asyncio.wait_for(prerequisite, timeout=10)
except asyncio.TimeoutError:
_logger.critical('Failed to gracefully exit: prerequisite timed out')
except Exception as e:
_logger.critical('Failed to gracefully exit:', exc_info=e)
finally:
exit(1)
def shutdown(prerequisite: Awaitable = None):
if not env.loop.is_running():
exit(1)
env.loop.call_later(20, lambda: os.kill(os.getpid(), signal.SIGKILL)) # double insurance
asyncio.gather(env.loop.create_task(exit_handler(prerequisite)), return_exceptions=True)
class _Watchdog:
def __init__(self, delay: int = 10 * 60):
self._watchdog = env.loop.call_later(delay, self._exit_bot, delay)
@staticmethod
def _exit_bot(delay):
msg = f'Never heard from the bot for {delay} seconds. Exiting...'
_logger.critical(msg)
coro = None
if env.bot is not None:
coro = env.loop.create_task(env.bot.send_message(env.ERROR_LOGGING_CHAT, f'WATCHDOG: {msg}'))
shutdown(prerequisite=coro)
def feed(self, delay: int = 15 * 60):
self._watchdog.cancel()
self._watchdog = env.loop.call_later(delay, self._exit_bot, delay)
# flit log from apscheduler.scheduler
class _APSCFilter(logging.Filter):
def __init__(self):
super().__init__()
self.count = 0
self.watchdog = _Watchdog()
def filter(self, record: logging.LogRecord) -> bool:
msg = record.msg % record.args
if 'skipped: maximum number of running instances reached' in msg:
self.count += 1
if self.count != 0 and self.count % 5 == 0:
coro = env.bot.send_message(
env.ERROR_LOGGING_CHAT,
f'RSS monitor tasks have conflicted too many times ({self.count})!\n'
+ ('Please store the log and restart.\n(sometimes it may be caused by too many subscriptions)'
if self.count < 15 else
'Now the bot will restart.')
+ '\n\n' + msg
)
if self.count >= 15:
_logger.critical(f'RSS monitor tasks have conflicted too many times ({self.count})! Exiting...')
shutdown(prerequisite=coro)
else:
env.loop.create_task(coro)
return True
if ' executed successfully' in msg:
return False
if 'Running job "Monitor.run_periodic_task' in msg:
self.count = 0
self.watchdog.feed()
return False
return True
class _AiohttpAccessFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
msg = record.msg % record.args
if record.levelno <= logging.INFO and 'Mozilla' not in msg:
return False
return True
class _TelethonClientUpdatesFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
msg = record.msg % record.args
if 'Fatal error' in msg:
coro = env.bot.send_message(
env.ERROR_LOGGING_CHAT,
msg + '\n\n' + 'Now the bot will restart.'
)
_logger.critical('Telethon client fatal error! Exiting...')
shutdown(prerequisite=coro)
return True
def init():
apsc_filter = _APSCFilter()
getLogger('apscheduler').setLevel(colorlog.WARNING)
getLogger('apscheduler.executors.default').setLevel(colorlog.INFO)
getLogger('apscheduler.scheduler').addFilter(apsc_filter)
getLogger('apscheduler.executors.default').addFilter(apsc_filter)
getLogger('aiohttp.access').addFilter(_AiohttpAccessFilter())
getLogger('telethon.client.updates').addFilter(_TelethonClientUpdatesFilter())