Skip to content

Commit

Permalink
Task gathering new logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dariNirvana committed Feb 9, 2024
1 parent 7ffbc47 commit 3ce8972
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
17 changes: 3 additions & 14 deletions telegraph-downloader/tgbot_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,11 @@ async def main():

while True:
try:
done, pending = await asyncio.wait({subprocess_task, komga_task}, return_when=asyncio.FIRST_COMPLETED)
done, _ = await asyncio.wait({subprocess_task, komga_task}, return_when=asyncio.FIRST_COMPLETED)

if subprocess_task in done:
logger.error('MAIN: subprocess_task has exited. Restarting...')
subprocess_task.cancel()
await subprocess_task
subprocess_task = asyncio.create_task(run_subprocess())

if komga_task in done:
logger.info('MAIN: komga_task has completed.')
if subprocess_task in pending:
logger.error('MAIN: subprocess_task is still running. It will continue.')
else:
logger.error('MAIN: subprocess_task has exited. Restarting...')
subprocess_task.cancel()
await subprocess_task
if subprocess_task.exception() is not None: # Check if the task has exited unexpectedly
logger.error('MAIN: subprocess_task has exited unexpectedly. Restarting...')
subprocess_task = asyncio.create_task(run_subprocess())

except asyncio.CancelledError:
Expand Down
16 changes: 10 additions & 6 deletions telegraph-downloader/tgbot_service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os, time
import os
from env import *
from logger import logger
from urlextract import URLExtract
from telegram import Update, Chat
from telegram import Update, Chat, request
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
Expand Down Expand Up @@ -96,7 +96,10 @@ async def task_epub_complete(update: Update, context: ContextTypes.DEFAULT_TYPE)
#->fallback handler start
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
logger.info("BOT SERVICE: Conversation canceled or Fallback is triggered.")

try:
request.HTTPXRequest(proxy=proxy_url)
except:
pass
return ConversationHandler.END
#<-fallback handler end

Expand Down Expand Up @@ -134,13 +137,14 @@ def main() -> None:
'''
application.add_handler(tgraph_komga_handler)
#application.add_handler(tgraph_epub_handler)
#application.add_error_handler(callback = cancel, block = True)
application.add_error_handler(callback = cancel, block = True)
# Run the bot until the user presses Ctrl-C
try:
application.run_polling(allowed_updates=Update.ALL_TYPES)
except Exception:
logger.error('BOT SERVICE: Unexpected timeout error, service restart.')
except Exception as e:
logger.error('BOT SERVICE: Start with unexpected timeout error.')
application.stop_running()
raise Exception('An unexpected timeout error occurred. Exiting the program.')

if __name__ == "__main__":
main()

0 comments on commit 3ce8972

Please sign in to comment.