diff --git a/Makefile b/Makefile index 607beef..299f31f 100644 --- a/Makefile +++ b/Makefile @@ -46,4 +46,7 @@ ps: docker-compose -f docker-compose.yml -f worker.yml ps down: - docker-compose -f docker-compose.yml -f worker.yml down \ No newline at end of file + docker-compose -f docker-compose.yml -f worker.yml down + +logs: + docker-compose -f docker-compose.yml -f worker.yml logs -f worker ytdl \ No newline at end of file diff --git a/ytdlbot/tasks.py b/ytdlbot/tasks.py index 166fefa..214a85d 100644 --- a/ytdlbot/tasks.py +++ b/ytdlbot/tasks.py @@ -11,14 +11,17 @@ import logging import os import pathlib import re +import subprocess import tempfile import threading import time from urllib.parse import quote_plus +import psutil import requests from apscheduler.schedulers.background import BackgroundScheduler from celery import Celery +from celery.worker.control import Panel from pyrogram import idle from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup @@ -240,6 +243,24 @@ def ytdl_normal_download(bot_msg, client, url): temp_dir.cleanup() +@Panel.register +def hot_patch(*args): + git_path = pathlib.Path().cwd().parent.as_posix() + logging.info("Hot patching on path %s...", git_path) + + unset = "/usr/bin/git config --unset http.https://github.com/.extraheader" + pull_unshallow = "/usr/bin/git pull origin --unshallow" + pull = "git pull" + + subprocess.call(unset, shell=True, cwd=git_path) + if subprocess.call(pull_unshallow, shell=True, cwd=git_path) != 0: + logging.info("Already unshallow, pulling now...") + subprocess.call(pull, shell=True, cwd=git_path) + + logging.info("Code is updated, applying hot patch now...") + psutil.Process().kill() + + def run_celery(): argv = [ "-A", "tasks", 'worker', '--loglevel=info', diff --git a/ytdlbot/ytdl_bot.py b/ytdlbot/ytdl_bot.py index fe99d64..87d7815 100644 --- a/ytdlbot/ytdl_bot.py +++ b/ytdlbot/ytdl_bot.py @@ -24,7 +24,8 @@ from config import (AUTHORIZED_USER, ENABLE_CELERY, ENABLE_VIP, OWNER, from constant import BotText from db import InfluxDB, MySQL, Redis from limit import verify_payment -from tasks import (audio_entrance, direct_download_entrance, +from tasks import app as celery_app +from tasks import (audio_entrance, direct_download_entrance, hot_patch, ytdl_download_entrance) from utils import (auto_restart, customize_logger, get_revision, get_user_settings, set_user_settings) @@ -91,6 +92,17 @@ def help_handler(client: "Client", message: "types.Message"): client.send_message(chat_id, bot_text.help, disable_web_page_preview=True) +@app.on_message(filters.command(["hot_patch"])) +def help_handler(client: "Client", message: "types.Message"): + username = message.from_user.username + chat_id = message.chat.id + if username == OWNER: + celery_app.control.broadcast("hot_patch") + client.send_chat_action(chat_id, "typing") + client.send_message(chat_id, "Oorah!") + hot_patch() + + @app.on_message(filters.command(["ping"])) def ping_handler(client: "Client", message: "types.Message"): chat_id = message.chat.id