fix: terminal status (#10142)

Co-authored-by: feng <1304903146@qq.com>
This commit is contained in:
fit2bot 2023-04-06 10:31:41 +08:00 committed by GitHub
parent 47d0882090
commit fb1978a40b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 10 deletions

View File

@ -53,7 +53,7 @@ class Status(models.Model):
def save(self, force_insert=False, force_update=False, using=None, def save(self, force_insert=False, force_update=False, using=None,
update_fields=None): update_fields=None):
self.terminal.set_alive(ttl=120) self.terminal.set_alive(ttl=60 * 3)
return self.save_to_cache() return self.save_to_cache()
def save_to_cache(self): def save_to_cache(self):

View File

@ -1,5 +1,3 @@
import time
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.db import models from django.db import models
@ -11,6 +9,7 @@ from common.utils import get_logger, lazyproperty
from orgs.utils import tmp_to_root_org from orgs.utils import tmp_to_root_org
from terminal.const import TerminalType as TypeChoices from terminal.const import TerminalType as TypeChoices
from users.models import User from users.models import User
from .status import Status
from ..session import Session from ..session import Session
logger = get_logger(__file__) logger = get_logger(__file__)
@ -23,7 +22,7 @@ class TerminalStatusMixin:
@lazyproperty @lazyproperty
def last_stat(self): def last_stat(self):
return self.status_set.order_by('date_created').last() return Status.get_terminal_latest_stat(self)
@lazyproperty @lazyproperty
def load(self): def load(self):
@ -32,11 +31,10 @@ class TerminalStatusMixin:
@property @property
def is_alive(self): def is_alive(self):
if not self.last_stat: key = self.ALIVE_KEY.format(self.id)
return False return cache.get(key, False)
return time.time() - self.last_stat.date_created.timestamp() < 150
def set_alive(self, ttl=120): def set_alive(self, ttl=60 * 3):
key = self.ALIVE_KEY.format(self.id) key = self.ALIVE_KEY.format(self.id)
cache.set(key, True, ttl) cache.set(key, True, ttl)

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import time
from itertools import groupby from itertools import groupby
from common.utils import get_logger from common.utils import get_logger
@ -40,7 +39,7 @@ class ComputeLoadUtil:
@classmethod @classmethod
def compute_load(cls, stat): def compute_load(cls, stat):
if not stat or time.time() - stat.date_created.timestamp() > 150: if not stat:
return ComponentLoad.offline return ComponentLoad.offline
system_status_values = cls._compute_system_stat_status(stat).values() system_status_values = cls._compute_system_stat_status(stat).values()
if ComponentLoad.critical in system_status_values: if ComponentLoad.critical in system_status_values: