-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.py
57 lines (53 loc) · 1.66 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
重载插件
"""
import requests
import time
import subprocess
import data.log
import haku.config
from handlers.message import Message, Plugin
__upgrade_flag = False
def run(message: Message) -> str:
global __upgrade_flag
msg_list = message.message.split()
if len(msg_list) == 2 and msg_list[1] == 'upgrade':
if __upgrade_flag:
return 'Already upgrading'
__upgrade_flag = True
config = haku.config.Config()
on_failure = True
duration = time.time()
trys = 0
# git pull
while on_failure:
trys += 1
code = subprocess.call(f'cd {config.get_root_path()} && git pull > /dev/null 2>&1', shell=True)
if code == 0:
on_failure = False
else:
time.sleep(5)
__upgrade_flag = False
# send reboot request
url = f'http://{config.get_listen_host()}:{config.get_listen_port()}/stop'
try:
resp = requests.get(url, timeout=10)
except:
return 'Upgrade failed.'
else:
if resp.status_code != 200:
return 'Upgrade failed.'
duration = int(time.time() - duration)
return f'Upgrade succeed.\nTried git pull {trys} times in {duration} seconds.'
elif len(msg_list) != 1:
return '升级 bot\n' \
'用法:\n' \
' update\n' \
' update upgrade'
data.log.get_logger().debug('Update plugin cache')
try:
plugin = Plugin()
plugin.reload()
except Exception as e:
return f'Update plugin cache failed: {e}'
return 'Update plugin cache success'