Skip to content

Commit

Permalink
decouple redis and mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Dec 14, 2023
1 parent 23f617e commit acacfe5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 4 additions & 2 deletions ytdlbot/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def __init__(self):
try:
self.r = redis.StrictRedis(host=REDIS, db=0, decode_responses=True)
self.r.ping()
except redis.RedisError:
except Exception:
logging.warning("Redis connection failed, using fake redis instead.")
self.r = fakeredis.FakeStrictRedis(host=REDIS, db=0, decode_responses=True)

db_banner = "=" * 20 + "DB data" + "=" * 20
Expand Down Expand Up @@ -254,7 +255,8 @@ def __init__(self):
self.con = pymysql.connect(
host=MYSQL_HOST, user=MYSQL_USER, passwd=MYSQL_PASS, db="ytdl", charset="utf8mb4"
)
except pymysql.err.OperationalError:
except Exception:
logging.warning("MySQL connection failed, using fake mysql instead.")
self.con = FakeMySQL()

self.con.ping(reconnect=True)
Expand Down
3 changes: 2 additions & 1 deletion ytdlbot/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
# celery -A tasks worker --loglevel=info --pool=solo
# app = Celery('celery', broker=BROKER, accept_content=['pickle'], task_serializer='pickle')
app = Celery("tasks", broker=BROKER)
redis = Redis()
channel = Channel()

session = "ytdl-celery"
Expand Down Expand Up @@ -122,6 +121,7 @@ def direct_download_task(chat_id, message_id, url):


def forward_video(client, bot_msg, url: str):
redis = Redis()
chat_id = bot_msg.chat.id
unique = get_unique_clink(url, chat_id)
cached_fid = redis.get_send_cache(unique)
Expand Down Expand Up @@ -307,6 +307,7 @@ def generate_input_media(file_paths: list, cap: str) -> list:


def upload_processor(client, bot_msg, url, vp_or_fid: typing.Union[str, list]):
redis = Redis()
# raise pyrogram.errors.exceptions.FloodWait(13)
# if is str, it's a file id; else it's a list of paths
payment = Payment()
Expand Down
10 changes: 7 additions & 3 deletions ytdlbot/ytdl_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
app = create_app(":memory:")

logging.info("Authorized users are %s", AUTHORIZED_USER)
redis = Redis()
channel = Channel()


Expand Down Expand Up @@ -205,6 +204,7 @@ def purge_handler(client: Client, message: types.Message):

@app.on_message(filters.command(["ping"]))
def ping_handler(client: Client, message: types.Message):
redis = Redis()
chat_id = message.chat.id
client.send_chat_action(chat_id, "typing")
if os.uname().sysname == "Darwin" or ".heroku" in os.getenv("PYTHONHOME", ""):
Expand All @@ -231,6 +231,7 @@ def sub_count_handler(client: Client, message: types.Message):

@app.on_message(filters.command(["direct"]))
def direct_handler(client: Client, message: types.Message):
redis = Redis()
chat_id = message.from_user.id
client.send_chat_action(chat_id, "typing")
url = re.sub(r"/direct\s*", "", message.text)
Expand Down Expand Up @@ -398,6 +399,7 @@ def search_ytb(kw: str):
@app.on_message(filters.incoming & (filters.text | filters.document))
@private_use
def download_handler(client: Client, message: types.Message):
redis = Redis()
payment = Payment()
chat_id = message.from_user.id
client.send_chat_action(chat_id, "typing")
Expand Down Expand Up @@ -480,6 +482,7 @@ def download_resolution_callback(client: Client, callback_query: types.CallbackQ

@app.on_callback_query(filters.regex(r"convert"))
def audio_callback(client: Client, callback_query: types.CallbackQuery):
redis = Redis()
if not ENABLE_FFMPEG:
callback_query.answer("Audio conversion is disabled now.")
callback_query.message.reply_text("Audio conversion is disabled now.")
Expand Down Expand Up @@ -546,6 +549,7 @@ def trx_notify(_, **kwargs):


if __name__ == "__main__":
redis = Redis()
MySQL()
TRX_SIGNAL.connect(trx_notify)
scheduler = BackgroundScheduler(timezone="Europe/London", job_defaults={"max_instances": 6})
Expand All @@ -555,8 +559,8 @@ def trx_notify(_, **kwargs):
scheduler.add_job(redis.reset_today, "cron", hour=0, minute=0)
scheduler.add_job(InfluxDB().collect_data, "interval", seconds=120)
scheduler.add_job(TronTrx().check_payment, "interval", seconds=60, max_instances=1)
# default quota allocation of 10,000 units per day
scheduler.add_job(periodic_sub_check, "interval", seconds=3600)
# default quota allocation of 10,000 units per day
scheduler.add_job(periodic_sub_check, "interval", seconds=3600)
scheduler.start()
banner = f"""
▌ ▌ ▀▛▘ ▌ ▛▀▖ ▜ ▌
Expand Down

0 comments on commit acacfe5

Please sign in to comment.