idle detector and upgrade

add idle and next salt detector
upgrade to latest pyrogram
fix session issue
This commit is contained in:
BennyThink 2022-01-16 19:38:47 +08:00
parent 5208b9de31
commit 83b671b873
No known key found for this signature in database
GPG key ID: 6CD0DBDA5235D481
4 changed files with 48 additions and 13 deletions

View file

@ -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

View file

@ -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}",

View file

@ -8,6 +8,7 @@
__author__ = "Benny <benny.think@gmail.com>"
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()

View file

@ -10,10 +10,8 @@ __author__ = "Benny <benny.think@gmail.com>"
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