perf: Check whether the applet is available.

pull/13888/head
Eric 2024-08-05 15:39:16 +08:00 committed by Bryan
parent c646084c51
commit 81b5f1ce93
2 changed files with 13 additions and 3 deletions

View File

@ -200,7 +200,7 @@ class ConnectionToken(JMSOrgBaseModel):
host_account = applet.select_host_account(self.user, self.asset)
if not host_account:
raise JMSException({'error': 'No host account available'})
raise JMSException({'error': 'No host account available, please check the applet, host and account'})
host, account, lock_key = bulk_get(host_account, ('host', 'account', 'lock_key'))
gateway = host.domain.select_gateway() if host.domain else None

View File

@ -15,6 +15,7 @@ from assets.models import Platform
from common.db.models import JMSBaseModel
from common.utils import lazyproperty, get_logger
from common.utils.yml import yaml_load_with_i18n
from terminal.const import PublishStatus
logger = get_logger(__name__)
@ -178,7 +179,6 @@ class Applet(JMSBaseModel):
if host_matched:
return random.choice(host_matched)
hosts = [h for h in hosts if h.auto_create_accounts]
prefer_key = self.host_prefer_key_tpl.format(user.id)
prefer_host_id = cache.get(prefer_key, None)
@ -279,7 +279,9 @@ class Applet(JMSBaseModel):
if not host:
return None
logger.info('Select applet host: {}'.format(host.name))
if not self.is_available_on_host(host):
logger.debug('No available applet {} for applet host: {}'.format(self.name, host.name))
return None
valid_accounts = host.accounts.all().filter(is_active=True, privileged=False)
account = self.try_to_use_same_account(user, host)
if not account:
@ -312,6 +314,14 @@ class Applet(JMSBaseModel):
platform.delete()
return super().delete(using, keep_parents)
def is_available_on_host(self, host):
publication = AppletPublication.objects.filter(applet=self, host=host).first()
if not publication:
return False
if publication.status in [PublishStatus.pending, PublishStatus.failed]:
return False
return True
class AppletPublication(JMSBaseModel):
applet = models.ForeignKey('Applet', on_delete=models.CASCADE, related_name='publications',