2021-08-06 11:16:18 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
from django.conf import settings
|
2021-11-18 10:47:01 +00:00
|
|
|
|
|
|
|
from common.db.utils import close_old_connections
|
2021-08-06 11:16:18 +00:00
|
|
|
from common.decorator import Singleton
|
|
|
|
from common.utils import get_disk_usage, get_cpu_load, get_memory_usage, get_logger
|
2021-11-18 10:47:01 +00:00
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
from .serializers.terminal import TerminalRegistrationSerializer, StatSerializer
|
|
|
|
from .const import TerminalType
|
2022-10-31 06:29:42 +00:00
|
|
|
from .models import Terminal
|
2021-08-06 11:16:18 +00:00
|
|
|
|
|
|
|
__all__ = ['CoreTerminal', 'CeleryTerminal']
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTerminal(object):
|
|
|
|
|
|
|
|
def __init__(self, suffix_name, _type):
|
2021-09-09 07:18:43 +00:00
|
|
|
server_hostname = os.environ.get('SERVER_HOSTNAME') or ''
|
|
|
|
hostname = socket.gethostname()
|
|
|
|
if server_hostname:
|
|
|
|
name = f'[{suffix_name}]-{server_hostname}'
|
|
|
|
else:
|
|
|
|
name = f'[{suffix_name}]-{hostname}'
|
|
|
|
self.name = name
|
2021-08-06 11:16:18 +00:00
|
|
|
self.interval = 30
|
2021-09-15 13:01:25 +00:00
|
|
|
self.remote_addr = self.get_remote_addr(hostname)
|
2021-08-06 11:16:18 +00:00
|
|
|
self.type = _type
|
|
|
|
|
2021-09-15 13:01:25 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_remote_addr(hostname):
|
|
|
|
try:
|
|
|
|
return socket.gethostbyname(hostname)
|
|
|
|
except socket.gaierror:
|
|
|
|
return '127.0.0.1'
|
|
|
|
|
2021-08-06 11:16:18 +00:00
|
|
|
def start_heartbeat_thread(self):
|
|
|
|
print(f'- Start heartbeat thread => ({self.name})')
|
|
|
|
t = threading.Thread(target=self.start_heartbeat)
|
|
|
|
t.setDaemon(True)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
def start_heartbeat(self):
|
|
|
|
while True:
|
|
|
|
heartbeat_data = {
|
|
|
|
'cpu_load': get_cpu_load(),
|
|
|
|
'memory_used': get_memory_usage(),
|
|
|
|
'disk_used': get_disk_usage(path=settings.BASE_DIR),
|
|
|
|
'sessions': [],
|
|
|
|
}
|
2022-11-04 03:40:16 +00:00
|
|
|
status_serializer = StatSerializer(data=heartbeat_data)
|
2021-08-06 11:16:18 +00:00
|
|
|
status_serializer.is_valid()
|
|
|
|
status_serializer.validated_data.pop('sessions', None)
|
|
|
|
terminal = self.get_or_register_terminal()
|
|
|
|
status_serializer.validated_data['terminal'] = terminal
|
|
|
|
|
2021-11-18 10:47:01 +00:00
|
|
|
try:
|
|
|
|
status_serializer.save()
|
|
|
|
time.sleep(self.interval)
|
2022-11-17 01:16:48 +00:00
|
|
|
except Exception:
|
2022-11-04 03:40:16 +00:00
|
|
|
print("Save status error, close old connections")
|
2021-11-18 10:47:01 +00:00
|
|
|
close_old_connections()
|
2022-11-17 01:16:48 +00:00
|
|
|
finally:
|
|
|
|
time.sleep(self.interval)
|
2021-08-06 11:16:18 +00:00
|
|
|
|
|
|
|
def get_or_register_terminal(self):
|
2021-09-13 10:01:15 +00:00
|
|
|
terminal = Terminal.objects.filter(
|
|
|
|
name=self.name, type=self.type, is_deleted=False
|
|
|
|
).first()
|
2021-08-06 11:16:18 +00:00
|
|
|
if not terminal:
|
|
|
|
terminal = self.register_terminal()
|
2021-09-13 10:01:15 +00:00
|
|
|
|
|
|
|
terminal.remote_addr = self.remote_addr
|
|
|
|
terminal.save()
|
2021-08-06 11:16:18 +00:00
|
|
|
return terminal
|
|
|
|
|
|
|
|
def register_terminal(self):
|
2021-09-13 10:01:15 +00:00
|
|
|
data = {
|
|
|
|
'name': self.name, 'type': self.type,
|
|
|
|
'remote_addr': self.remote_addr
|
|
|
|
}
|
2021-08-06 11:16:18 +00:00
|
|
|
serializer = TerminalRegistrationSerializer(data=data)
|
|
|
|
serializer.is_valid()
|
|
|
|
terminal = serializer.save()
|
|
|
|
return terminal
|
|
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class CoreTerminal(BaseTerminal):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
2022-11-04 03:40:16 +00:00
|
|
|
suffix_name=TerminalType.core.label,
|
|
|
|
_type=TerminalType.core.value
|
2021-08-06 11:16:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class CeleryTerminal(BaseTerminal):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
2022-11-04 03:40:16 +00:00
|
|
|
suffix_name=TerminalType.celery.label,
|
|
|
|
_type=TerminalType.celery.value
|
2021-08-06 11:16:18 +00:00
|
|
|
)
|