mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-07-06 11:36:32 +00:00
perf: simplify db using
This commit is contained in:
parent
e9ff988d8c
commit
f826f43495
@ -56,7 +56,14 @@ def close_old_connections():
|
|||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def safe_db_connection(auto_close=False):
|
def safe_db_connection(auto_close=True):
|
||||||
|
close_old_connections()
|
||||||
|
yield
|
||||||
|
close_old_connections()
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def safe_atomic_db_connection(auto_close=False):
|
||||||
in_atomic_block = connection.in_atomic_block # 当前是否处于事务中
|
in_atomic_block = connection.in_atomic_block # 当前是否处于事务中
|
||||||
autocommit = transaction.get_autocommit() # 是否启用了自动提交
|
autocommit = transaction.get_autocommit() # 是否启用了自动提交
|
||||||
created = False
|
created = False
|
||||||
@ -70,7 +77,6 @@ def safe_db_connection(auto_close=False):
|
|||||||
finally:
|
finally:
|
||||||
# 如果不是事务中(API 请求中可能需要提交事务),则关闭连接
|
# 如果不是事务中(API 请求中可能需要提交事务),则关闭连接
|
||||||
if auto_close or (created and not in_atomic_block and autocommit):
|
if auto_close or (created and not in_atomic_block and autocommit):
|
||||||
print("close connection in safe_db_connection")
|
|
||||||
close_old_connections()
|
close_old_connections()
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ from functools import wraps
|
|||||||
|
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
from .db.utils import open_db_connection, safe_db_connection
|
from .db.utils import open_db_connection, safe_atomic_db_connection
|
||||||
from .utils import logger
|
from .utils import logger
|
||||||
|
|
||||||
|
|
||||||
@ -318,7 +318,7 @@ def bulk_handle(handler, batch_size=50, timeout=0.5):
|
|||||||
if not cache:
|
if not cache:
|
||||||
return
|
return
|
||||||
with tmp_to_org(org_id):
|
with tmp_to_org(org_id):
|
||||||
with safe_db_connection():
|
with safe_atomic_db_connection():
|
||||||
handler(cache)
|
handler(cache)
|
||||||
cache.clear()
|
cache.clear()
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ class SiteMsgWebsocket(JsonWebsocketConsumer):
|
|||||||
user_id = str(self.scope["user"].id)
|
user_id = str(self.scope["user"].id)
|
||||||
|
|
||||||
# 先发一个消息再说
|
# 先发一个消息再说
|
||||||
with safe_db_connection(auto_close=True):
|
with safe_db_connection():
|
||||||
self.send_unread_msg_count()
|
self.send_unread_msg_count()
|
||||||
|
|
||||||
def handle_new_site_msg_recv(msg):
|
def handle_new_site_msg_recv(msg):
|
||||||
|
@ -52,7 +52,7 @@ class TerminalTaskWebsocket(JsonWebsocketConsumer):
|
|||||||
self.send(bytes_data=content)
|
self.send(bytes_data=content)
|
||||||
|
|
||||||
def get_terminal_tasks(self, task_id=None):
|
def get_terminal_tasks(self, task_id=None):
|
||||||
with safe_db_connection(auto_close=True):
|
with safe_db_connection():
|
||||||
critical_time = timezone.now() - datetime.timedelta(minutes=10)
|
critical_time = timezone.now() - datetime.timedelta(minutes=10)
|
||||||
tasks = self.terminal.task_set.filter(is_finished=False, date_created__gte=critical_time)
|
tasks = self.terminal.task_set.filter(is_finished=False, date_created__gte=critical_time)
|
||||||
if task_id:
|
if task_id:
|
||||||
|
Loading…
Reference in New Issue
Block a user