🐛 解决更新后无法退出问题

This commit is contained in:
洛水居室 2022-10-08 11:06:25 +08:00
parent 0fa7c4eacc
commit 08bdd8962b
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -1,5 +1,7 @@
import asyncio
import json import json
import os import os
from sys import executable
from aiofiles import open as async_open from aiofiles import open as async_open
from telegram import Update, Message from telegram import Update, Message
@ -20,6 +22,9 @@ UPDATE_DATA = os.path.join(current_dir, "data", "update.json")
class UpdatePlugin(Plugin): class UpdatePlugin(Plugin):
def __init__(self):
self._lock = asyncio.Lock()
@staticmethod @staticmethod
async def __async_init__(): async def __async_init__():
if os.path.exists(UPDATE_DATA): if os.path.exists(UPDATE_DATA):
@ -33,21 +38,26 @@ class UpdatePlugin(Plugin):
logger.exception(exc) logger.exception(exc)
os.remove(UPDATE_DATA) os.remove(UPDATE_DATA)
@handler(CommandHandler, command="update") @handler(CommandHandler, command="update", block=False)
@bot_admins_rights_check @bot_admins_rights_check
async def update(self, update: Update, context: CallbackContext): async def update(self, update: Update, context: CallbackContext):
user = update.effective_user user = update.effective_user
message = update.effective_message message = update.effective_message
args = get_all_args(context) args = get_all_args(context)
logger.info(f"用户 {user.full_name}[{user.id}] update命令请求") logger.info(f"用户 {user.full_name}[{user.id}] update命令请求")
reply_text = await message.reply_text("正在更新") if self._lock.locked():
await execute("git fetch --all") await message.reply_text("程序正在更新 请勿重复操作")
if len(args) > 0: async with self._lock:
await execute("git reset --hard origin/main") reply_text = await message.reply_text("正在更新")
await execute("git pull --all") logger.info(f"正在更新代码")
if len(args) > 0: await execute("git fetch --all")
await execute("poetry install --extras all") if len(args) > 0:
await reply_text.edit_text("自动更新成功 正在重启") await execute("git reset --hard origin/main")
async with async_open(UPDATE_DATA, mode='w', encoding='utf-8') as file: await execute("git pull --all")
await file.write(reply_text.to_json()) 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 raise SystemExit