From 08bdd8962beb06ac2d5cafc68d8a3f76b75badef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sat, 8 Oct 2022 11:06:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E8=A7=A3=E5=86=B3=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=90=8E=E6=97=A0=E6=B3=95=E9=80=80=E5=87=BA=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/system/update.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/plugins/system/update.py b/plugins/system/update.py index 15803fb..a24b934 100644 --- a/plugins/system/update.py +++ b/plugins/system/update.py @@ -1,5 +1,7 @@ +import asyncio import json import os +from sys import executable from aiofiles import open as async_open from telegram import Update, Message @@ -20,6 +22,9 @@ UPDATE_DATA = os.path.join(current_dir, "data", "update.json") class UpdatePlugin(Plugin): + def __init__(self): + self._lock = asyncio.Lock() + @staticmethod async def __async_init__(): if os.path.exists(UPDATE_DATA): @@ -33,21 +38,26 @@ class UpdatePlugin(Plugin): logger.exception(exc) os.remove(UPDATE_DATA) - @handler(CommandHandler, command="update") + @handler(CommandHandler, command="update", block=False) @bot_admins_rights_check async def update(self, update: Update, context: CallbackContext): user = update.effective_user message = update.effective_message args = get_all_args(context) logger.info(f"用户 {user.full_name}[{user.id}] update命令请求") - reply_text = await message.reply_text("正在更新") - await execute("git fetch --all") - if len(args) > 0: - await execute("git reset --hard origin/main") - await execute("git pull --all") - if len(args) > 0: - await execute("poetry install --extras all") - await reply_text.edit_text("自动更新成功 正在重启") - async with async_open(UPDATE_DATA, mode='w', encoding='utf-8') as file: - await file.write(reply_text.to_json()) + if self._lock.locked(): + await message.reply_text("程序正在更新 请勿重复操作") + async with self._lock: + reply_text = await message.reply_text("正在更新") + logger.info(f"正在更新代码") + await execute("git fetch --all") + if len(args) > 0: + await execute("git reset --hard origin/main") + await execute("git pull --all") + if len(args) > 0: + await execute(f"{executable} -m poetry install --extras all") + logger.info(f"更新成功 正在重启") + await reply_text.edit_text("更新成功 正在重启") + async with async_open(UPDATE_DATA, mode='w', encoding='utf-8') as file: + await file.write(reply_text.to_json()) raise SystemExit