-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrashRestart.py
82 lines (68 loc) · 2.16 KB
/
CrashRestart.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import json
import os
import time
PLUGIN_METADATA = {
'id': 'crash_restart',
'version': '1.0.0',
'name': 'Crash Restart',
'author': 'Fallen_Breath',
'link': 'https://github.com/MCDReforged/CrashRestart',
}
CONFIG_FILE = os.path.join('config', 'CrashRestart.json')
# MAXIMUM: 3 crashes within 5min
config = {
"MAX_COUNT": 3,
"COUNTING_TIME": 300 # seconds
}
default_config = config.copy()
counter = None
count_start_time = None
is_crash = False
def on_server_startup(server):
global is_crash
is_crash = False
def on_info(server, info):
if not info.is_user and info.logging_level == 'ERROR' and info.content.startswith('This crash report has been saved to:'):
global is_crash
is_crash = True
server.logger.info('Crash report creation detected')
def on_server_stop(server, return_code):
global counter, count_start_time, is_crash
if return_code == 0 and not is_crash:
return
max_count = config['MAX_COUNT']
counting_time = config['COUNTING_TIME']
reason = 'the return code of the server is {}'.format(
return_code) if return_code != 0 else 'a crash report has been created'
server.logger.info('Seems like a crash has happened since {}'.format(reason))
current_time = time.time()
if count_start_time is not None and current_time - count_start_time <= counting_time:
counter += 1
if counter > max_count:
server.logger.info('No restart this time due to maximum allowed crashes ({}) in {} seconds exceeded'.format(
max_count, counting_time))
return
else:
count_start_time = time.time()
counter = 1
server.logger.info(
'Restarting the server, crash counter: {}/{}'.format(counter, max_count))
server.start()
def on_load(server, old):
global counter, count_start_time, is_crash
if old is not None:
counter = old.counter
count_start_time = old.count_start_time
is_crash = old.is_crash
global config
try:
with open(CONFIG_FILE) as file:
js = json.load(file)
for key in config.keys():
config[key] = js[key]
except:
server.logger.info('fail to read config file, using default value')
config = default_config
with open(CONFIG_FILE, 'w') as file:
json.dump(config, file, indent=4)
server.logger.info('Config info: {}'.format(config))