From 9bf76ae07a59dcfafc7e91c12f6fcba4a0b4d2bd Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 8 Aug 2023 15:41:55 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20applet=20=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E8=B4=A6=E5=8F=B7=E8=B0=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/accounts/filters.py | 1 + apps/locale/zh/LC_MESSAGES/django.mo | 4 ++-- apps/locale/zh/LC_MESSAGES/django.po | 7 ++++--- apps/terminal/models/applet/applet.py | 23 ++++++++++++++++++----- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/apps/accounts/filters.py b/apps/accounts/filters.py index dc28d78aa..e71a5b9fb 100644 --- a/apps/accounts/filters.py +++ b/apps/accounts/filters.py @@ -14,6 +14,7 @@ class AccountFilterSet(BaseFilterSet): username = drf_filters.CharFilter(field_name="username", lookup_expr='exact') address = drf_filters.CharFilter(field_name="asset__address", lookup_expr='exact') asset_id = drf_filters.CharFilter(field_name="asset", lookup_expr='exact') + asset = drf_filters.CharFilter(field_name='asset', lookup_expr='exact') assets = drf_filters.CharFilter(field_name='asset_id', lookup_expr='exact') nodes = drf_filters.CharFilter(method='filter_nodes') node_id = drf_filters.CharFilter(method='filter_nodes') diff --git a/apps/locale/zh/LC_MESSAGES/django.mo b/apps/locale/zh/LC_MESSAGES/django.mo index e2d64b74a..c9e28a258 100644 --- a/apps/locale/zh/LC_MESSAGES/django.mo +++ b/apps/locale/zh/LC_MESSAGES/django.mo @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ecbfbaa09e20e182a7804f2b60d7241cae6f81dbc58abede1e7a4a863b18777a -size 124596 +oid sha256:6a49d0a2a96656a3f98a0d13f64d426be5f60110ef629bed8d871c6525c16d82 +size 124739 diff --git a/apps/locale/zh/LC_MESSAGES/django.po b/apps/locale/zh/LC_MESSAGES/django.po index 969a82ed2..054fca398 100644 --- a/apps/locale/zh/LC_MESSAGES/django.po +++ b/apps/locale/zh/LC_MESSAGES/django.po @@ -6241,9 +6241,10 @@ msgid "" "support multiple open and the special has been used, the public account will " "be used to connect" msgstr "" -"这些账号用于连接发布的应用,账号现在分为两种类型,一种是专用的,每个用户都有" -"一个专用账号。 另一种是公共的,当应用不支持多开且专用的已经被使用时,会使用公" -"共账号连接" +"这些账号用于连接发布的应用,账号现在分为两种类型:
" +"一种是专用的,每个用户都有一个专用账号。 " +"另一种是公共的,当应用不支持多开且专用的已经被使用时,会使用公共账号连接;
" +"注意: 如果不开启自动创建账号, 当前发布机仅能被指定标签的资产调度到,默认不会放到调度池中" #: terminal/serializers/applet_host.py:77 msgid "The number of public accounts created automatically" diff --git a/apps/terminal/models/applet/applet.py b/apps/terminal/models/applet/applet.py index 07d63bce1..ccd1a8e52 100644 --- a/apps/terminal/models/applet/applet.py +++ b/apps/terminal/models/applet/applet.py @@ -156,10 +156,11 @@ class Applet(JMSBaseModel): spec_label = asset.labels.filter(name__in=['AppletHost', '发布机']).first() if spec_label: - host = [host for host in hosts if host.name == spec_label.value] - if host: - return host[0] + matched = [host for host in hosts if host.name == spec_label.value] + if matched: + return matched[0] + hosts = [h for h in hosts if h.auto_create_accounts] prefer_key = 'applet_host_prefer_{}'.format(user.id) prefer_host_id = cache.get(prefer_key, None) pref_host = [host for host in hosts if host.id == prefer_host_id] @@ -213,22 +214,26 @@ class Applet(JMSBaseModel): private_account = valid_accounts.filter(username='js_{}'.format(user.username)).first() if not private_account: + logger.debug('Private account not found ...') return None # 优先使用 private account,支持并发或者不支持并发时,如果私有没有被占用,则使用私有 account = None # 如果都支持,不管私有是否被占用,都使用私有 if all_can_concurrent: + logger.debug('All can concurrent, use private account') account = private_account # 如果主机都不支持并发,则查询一下私有账号有没有任何应用使用,如果没有被使用,则使用私有 elif not host_can_concurrent: private_using_key = self.accounts_using_key_tmpl.format(host.id, private_account.username, '*') - private_is_using = len(cache.keys(private_using_key, [])) > 0 + private_is_using = len(cache.keys(private_using_key)) + logger.debug("Private account is using: {}".format(private_is_using)) if not private_is_using: account = private_account # 如果主机支持,但是应用不支持并发,则查询一下私有账号有没有被这个应用使用, 如果没有被使用,则使用私有 elif host_can_concurrent and not app_can_concurrent: private_app_using_key = self.accounts_using_key_tmpl.format(host.id, private_account.username, self.name) private_is_using_by_this_app = cache.get(private_app_using_key, False) + logger.debug("Private account is using {} by {}".format(private_is_using_by_this_app, self.name)) if not private_is_using_by_this_app: account = private_account return account @@ -236,24 +241,32 @@ class Applet(JMSBaseModel): def select_host_account(self, user, asset): # 选择激活的发布机 host = self.select_host(user, asset) + logger.info('Select applet host: {}'.format(host.name)) if not host: return None valid_accounts = host.accounts.all().filter(is_active=True, privileged=False) account = self.try_to_use_private_account(user, host, valid_accounts) if not account: + logger.debug('No private account, try to use public account') account = self.select_a_public_account(user, host, valid_accounts) + if not account: + logger.debug('No available account for applet host: {}'.format(host.name)) + return None + ttl = 60 * 60 * 24 lock_key = self.accounts_using_key_tmpl.format(host.id, account.username, self.name) cache.set(lock_key, account.username, ttl) - return { + res = { 'host': host, 'account': account, 'lock_key': lock_key, 'ttl': ttl } + logger.debug('Select host and account: {}'.format(res)) + return res def delete(self, using=None, keep_parents=False): platform = self.get_related_platform()