Merge branch 'pretzel-fix' into 'dev'

Add timeout to telnet.

See merge request crafty-controller/crafty-commander!112
This commit is contained in:
Andrew 2021-12-10 04:52:37 +00:00
commit 1341730230
4 changed files with 23 additions and 18 deletions

View File

@ -17,6 +17,7 @@ import shutil
from requests import get from requests import get
from contextlib import suppress from contextlib import suppress
import ctypes import ctypes
import telnetlib
from datetime import datetime from datetime import datetime
from socket import gethostname from socket import gethostname
@ -98,14 +99,9 @@ class Helpers:
def check_port(server_port): def check_port(server_port):
try: try:
host_public = get('https://api.ipify.org').text host_public = get('https://api.ipify.org').text
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tn = telnetlib.Telnet(host_public, server_port, 10)
sock.settimeout(10.0) telnetlib.close()
result = sock.connect_ex((host_public ,server_port)) return True
sock.close()
if result == 0:
return True
else:
return False
except Exception as err: except Exception as err:
return False return False

View File

@ -168,7 +168,6 @@ class Controller:
return running_servers return running_servers
def stop_all_servers(self): def stop_all_servers(self):
print("in stop all servers")
servers = self.list_running_servers() servers = self.list_running_servers()
logger.info("Found {} running server(s)".format(len(servers))) logger.info("Found {} running server(s)".format(len(servers)))
console.info("Found {} running server(s)".format(len(servers))) console.info("Found {} running server(s)".format(len(servers)))
@ -176,10 +175,7 @@ class Controller:
logger.info("Stopping All Servers") logger.info("Stopping All Servers")
console.info("Stopping All Servers") console.info("Stopping All Servers")
print(servers)
for s in servers: for s in servers:
print(s)
logger.info("Stopping Server ID {} - {}".format(s['id'], s['name'])) logger.info("Stopping Server ID {} - {}".format(s['id'], s['name']))
console.info("Stopping Server ID {} - {}".format(s['id'], s['name'])) console.info("Stopping Server ID {} - {}".format(s['id'], s['name']))

View File

@ -272,6 +272,8 @@ class Server:
console.info("Server {} running with PID {}".format(self.name, self.process.pid)) console.info("Server {} running with PID {}".format(self.name, self.process.pid))
self.is_crashed = False self.is_crashed = False
self.stats.record_stats() self.stats.record_stats()
websocket_helper.broadcast_user(user_id, 'send_start_reload', {
})
else: else:
logger.warning("Server PID {} died right after starting - is this a server config issue?".format(self.process.pid)) logger.warning("Server PID {} died right after starting - is this a server config issue?".format(self.process.pid))
console.warning("Server PID {} died right after starting - is this a server config issue?".format(self.process.pid)) console.warning("Server PID {} died right after starting - is this a server config issue?".format(self.process.pid))
@ -281,13 +283,25 @@ class Server:
console.info("Server {} has crash detection enabled - starting watcher task".format(self.name)) console.info("Server {} has crash detection enabled - starting watcher task".format(self.name))
self.crash_watcher_schedule = schedule.every(30).seconds.do(self.detect_crash).tag(self.name) self.crash_watcher_schedule = schedule.every(30).seconds.do(self.detect_crash).tag(self.name)
check_port_thread = threading.Thread(target=self.check_internet_thread, daemon=True, args=(user_id, user_lang, ), name=f"backup_{self.name}")
check_port_thread.start()
def check_internet_thread(self, user_id, user_lang):
if user_id: if user_id:
if helper.check_internet(): if helper.check_internet():
loc_server_port = servers_helper.get_server_stats_by_id(self.server_id)['server_port'] loc_server_port = servers_helper.get_server_stats_by_id(self.server_id)['server_port']
if helper.check_port(loc_server_port): port_status = False
websocket_helper.broadcast_user(user_id, 'send_start_reload', {
}) for i in range(1):
else: if helper.check_port(loc_server_port):
port_status = True
return
else:
time.sleep(5)
if port_status == False:
websocket_helper.broadcast_user(user_id, 'send_start_error', { websocket_helper.broadcast_user(user_id, 'send_start_error', {
'error': translation.translate('error', 'closedPort', user_lang).format(loc_server_port) 'error': translation.translate('error', 'closedPort', user_lang).format(loc_server_port)
}) })
@ -295,6 +309,7 @@ class Server:
websocket_helper.broadcast_user(user_id, 'send_start_error', { websocket_helper.broadcast_user(user_id, 'send_start_error', {
'error': translation.translate('error', 'internet', user_lang) 'error': translation.translate('error', 'internet', user_lang)
}) })
return
def stop_threaded_server(self): def stop_threaded_server(self):
self.stop_server() self.stop_server()

View File

@ -109,9 +109,7 @@ class TasksManager:
time.sleep(1) time.sleep(1)
def _main_graceful_exit(self): def _main_graceful_exit(self):
print("in graceful")
try: try:
print("in try")
os.remove(helper.session_file) os.remove(helper.session_file)
os.remove(os.path.join(helper.root_dir, '.header')) os.remove(os.path.join(helper.root_dir, '.header'))
self.controller.stop_all_servers() self.controller.stop_all_servers()