Skip to content

Commit

Permalink
Fix celery oom (#2308)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu327 authored Oct 30, 2023
1 parent df3a3c0 commit b935455
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions saas/backend/apps/role/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from textwrap import dedent
from typing import Dict, List, Union

from django.core.paginator import Paginator
from django.db import connection, models
from django.utils.functional import cached_property

Expand Down Expand Up @@ -172,26 +173,35 @@ def delete_action_from_scope(cls, system_id: str, action_id: str):
"""
从可授权范围里删除某个操作,由于json存储了所有授权信息,所以无法直接索引,只能遍历所有
"""
role_scopes = cls.objects.filter(type=RoleScopeType.AUTHORIZATION.value)
qs = Role.objects.only("id")
if system_id != "bk_ci_rbac":
qs = qs.exclude(source_system_id="bk_ci_rbac")

paginator = Paginator(qs, 100)
should_updated_role_scopes = []
for role_scope in role_scopes:
content = json.loads(role_scope.content)
should_updated = False
# 遍历授权范围里每个系统
for scope in content:
if scope["system_id"] != system_id:
continue
# 判断Action是否存在,不存在则忽略
action_ids = {action["id"] for action in scope["actions"]}
if action_id not in action_ids:
for i in paginator.page_range:
for role in paginator.page(i):
role_scope = cls.objects.filter(type=RoleScopeType.AUTHORIZATION.value, role_id=role.id).first()
if not role_scope:
continue
# 如果包含要删除的Action,则进行更新数据
scope["actions"] = [action for action in scope["actions"] if action["id"] != action_id]
should_updated = True
break
if should_updated:
role_scope.content = json_dumps(content)
should_updated_role_scopes.append(role_scope)

content = json.loads(role_scope.content)
should_updated = False
# 遍历授权范围里每个系统
for scope in content:
if scope["system_id"] != system_id:
continue
# 判断Action是否存在,不存在则忽略
action_ids = {action["id"] for action in scope["actions"]}
if action_id not in action_ids:
continue
# 如果包含要删除的Action,则进行更新数据
scope["actions"] = [action for action in scope["actions"] if action["id"] != action_id]
should_updated = True
break
if should_updated:
role_scope.content = json_dumps(content)
should_updated_role_scopes.append(role_scope)

# 批量更新分级管理员授权范围
if len(should_updated_role_scopes) > 0:
Expand Down

0 comments on commit b935455

Please sign in to comment.