diff --git a/requirements.txt b/requirements.txt index 4bb0270..f01e0e2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -pyrogram==1.2.20 +pyrogram==1.3.5 tgcrypto==1.2.2 yt-dlp==2021.12.27 APScheduler==3.8.1 diff --git a/ytdlbot/tasks.py b/ytdlbot/tasks.py index 1e92886..20d386f 100644 --- a/ytdlbot/tasks.py +++ b/ytdlbot/tasks.py @@ -34,7 +34,6 @@ bot_text = BotText() app = Celery('tasks', broker=BROKER) celery_client = create_app(":memory:") -celery_client.start() @app.task() @@ -151,6 +150,7 @@ def normal_download(bot_msg, client, url): def run_celery(): + celery_client.start() argv = [ "-A", "tasks", 'worker', '--loglevel=info', "--pool=threads", f"--concurrency={WORKERS * 2}", diff --git a/ytdlbot/utils.py b/ytdlbot/utils.py index 45513e0..67a5cf1 100644 --- a/ytdlbot/utils.py +++ b/ytdlbot/utils.py @@ -8,6 +8,7 @@ __author__ = "Benny " import contextlib +import inspect as pyinspect import logging import os import pathlib @@ -36,7 +37,6 @@ def apply_log_formatter(): def customize_logger(logger: "list"): apply_log_formatter() for log in logger: - # TODO: bug fix: lost response sometime. logging.getLogger(log).setLevel(level=logging.INFO) @@ -175,14 +175,51 @@ def tail(f, lines=1, _buffer=4098): return lines_found[-lines:] -def auto_restart(): - indicators = ["types.UpdatesTooLong"] - with open("/var/log/ytdl.log") as f: - logs = "".join(tail(f, lines=5)) +class Detector: + def __init__(self, logs: "str"): + self.logs = logs - for indicator in indicators: - if indicator in logs: - logging.critical("Potential crash detected, suiciding now...") + @staticmethod + def func_name(): + with contextlib.suppress(Exception): + return pyinspect.stack()[1][3] + return "N/A" + + def updates_too_long_detector(self): + # If you're seeing this, that means you have logged more than 10 device + # and this earliest account was kicked out. Restart the program could get you back in. + indicators = ["types.UpdatesTooLong"] + for indicator in indicators: + if indicator in self.logs: + logging.warning("Potential crash detected by %s, it's time to commit suicide...", self.func_name()) + return True + logging.debug("No crash detected.") + + def next_salt_detector(self): + text = "Next salt in" + if self.logs.count(text) >= 4: + logging.warning("Potential crash detected by %s, it's time to commit suicide...", self.func_name()) + return True + + def idle_detector(self): + mtime = os.stat("/var/log/ytdl.log").st_mtime + cur_ts = time.time() + if cur_ts - mtime > 300: + logging.warning("Potential crash detected by %s, it's time to commit suicide...", self.func_name()) + return True + + +def auto_restart(): + with open("/var/log/ytdl.log") as f: + logs = "".join(tail(f, lines=10)) + + det = Detector(logs) + method_list = [getattr(det, func) for func in dir(det) if func.endswith("_detector")] + for method in method_list: + if method(): + logging.critical("Bye bye world!☠️") psutil.Process().kill() - logging.debug("No crash detected.") + +if __name__ == '__main__': + auto_restart() diff --git a/ytdlbot/ytdl_bot.py b/ytdlbot/ytdl_bot.py index cd43902..e9f5688 100644 --- a/ytdlbot/ytdl_bot.py +++ b/ytdlbot/ytdl_bot.py @@ -10,10 +10,8 @@ __author__ = "Benny " import logging import os import re -import tempfile import typing -import filetype from apscheduler.schedulers.background import BackgroundScheduler from pyrogram import Client, filters, types from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant