Skip to content

Commit 87d988b

Browse files
committed
fix(parsing/tgraph): unclosed session when exit
Signed-off-by: Rongrong <[email protected]>
1 parent 6a45a7e commit 87d988b

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import uvloop
77

88
uvloop.install()
9-
except ImportError: # uvloop do not support Windows
9+
except ImportError: # uvloop does not support Windows
1010
uvloop = None
1111

1212
from functools import partial
@@ -43,7 +43,10 @@ def init():
4343
api_keys = {env.API_ID: env.API_HASH}
4444

4545
# pre tasks
46-
pre_tasks.append(loop.create_task(db.init()))
46+
pre_tasks.extend((
47+
loop.create_task(db.init()),
48+
loop.create_task(tgraph.init()),
49+
))
4750

4851
if env.PORT:
4952
# enable redirect server for Railway, Heroku, etc
@@ -232,12 +235,14 @@ async def pre():
232235

233236

234237
async def post():
235-
await db.close()
238+
await asyncio.gather(db.close(), tgraph.close())
236239

237240

238241
def main():
239242
init()
240243

244+
loop.run_until_complete(pre())
245+
241246
logger.info(f"RSS-to-Telegram-Bot ({', '.join(env.VERSION.split())}) started!\n"
242247
f"MANAGER: {env.MANAGER}\n"
243248
f"T_PROXY (for Telegram): {env.TELEGRAM_PROXY or 'not set'}\n"
@@ -250,8 +255,6 @@ def main():
250255
logger.warning('Bot manager privileged mode is enabled! '
251256
'Use with caution and should be disabled in production!')
252257

253-
loop.run_until_complete(pre())
254-
255258
scheduler = AsyncIOScheduler(event_loop=loop)
256259
scheduler.add_job(func=command.monitor.run_monitor_task,
257260
trigger=CronTrigger(minute='*', second=env.CRON_SECOND, timezone='UTC'),

src/parsing/tgraph.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@
2323

2424
logger = log.getLogger('RSStT.tgraph')
2525

26+
apis: Optional[APIs] = None
27+
28+
29+
async def init():
30+
global apis
31+
if env.TELEGRAPH_TOKEN:
32+
apis = APIs(env.TELEGRAPH_TOKEN)
33+
await apis.init()
34+
if not apis.valid:
35+
logger.error('Set up Telegraph failed, fallback to non-Telegraph mode.')
36+
apis = None
37+
38+
39+
async def close():
40+
global apis
41+
if apis:
42+
await apis.close()
43+
apis = None
44+
2645

2746
class Telegraph(aiograph.Telegraph):
2847
def __init__(self, token=None):
@@ -71,7 +90,6 @@ def __init__(self, tokens: Union[str, list[str]]):
7190
self.tokens = tokens
7291
self._accounts: list[Telegraph] = []
7392
self._curr_id = 0
74-
env.loop.run_until_complete(self.init())
7593

7694
async def init(self):
7795
for token in self.tokens:
@@ -96,6 +114,10 @@ async def init(self):
96114
except Exception as e:
97115
logger.warning('Cannot set up one of Telegraph accounts: ' + str(e), exc_info=e)
98116

117+
async def close(self):
118+
for account in self._accounts:
119+
await account.close()
120+
99121
@property
100122
def valid(self):
101123
return bool(self._accounts)
@@ -113,13 +135,6 @@ def get_account(self) -> Telegraph:
113135
return self._accounts[curr_id]
114136

115137

116-
apis = None
117-
if env.TELEGRAPH_TOKEN:
118-
apis = APIs(env.TELEGRAPH_TOKEN)
119-
if not apis.valid:
120-
logger.error('Cannot set up Telegraph, fallback to non-Telegraph mode.')
121-
apis = None
122-
123138
TELEGRAPH_ALLOWED_TAGS: Final = {
124139
'a', 'aside', 'b', 'blockquote', 'br', 'code', 'em', 'figcaption', 'figure',
125140
'h3', 'h4', 'hr', 'i', 'iframe', 'img', 'li', 'ol', 'p', 'pre', 's',

0 commit comments

Comments
 (0)