mirror of https://github.com/jumpserver/jumpserver
perf: db connection close if needt
parent
a72e6456d9
commit
e9ff988d8c
|
@ -4,6 +4,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
from django.db import close_old_connections
|
||||||
|
|
||||||
|
|
||||||
class CommonConfig(AppConfig):
|
class CommonConfig(AppConfig):
|
||||||
|
@ -21,3 +22,4 @@ class CommonConfig(AppConfig):
|
||||||
|
|
||||||
if not os.environ.get('DJANGO_DEBUG_SHELL'):
|
if not os.environ.get('DJANGO_DEBUG_SHELL'):
|
||||||
django_ready.send(CommonConfig)
|
django_ready.send(CommonConfig)
|
||||||
|
close_old_connections()
|
||||||
|
|
|
@ -56,7 +56,7 @@ def close_old_connections():
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def safe_db_connection():
|
def safe_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
|
||||||
|
@ -69,7 +69,7 @@ def safe_db_connection():
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
# 如果不是事务中(API 请求中可能需要提交事务),则关闭连接
|
# 如果不是事务中(API 请求中可能需要提交事务),则关闭连接
|
||||||
if 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")
|
print("close connection in safe_db_connection")
|
||||||
close_old_connections()
|
close_old_connections()
|
||||||
|
|
||||||
|
|
|
@ -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():
|
with safe_db_connection(auto_close=True):
|
||||||
self.send_unread_msg_count()
|
self.send_unread_msg_count()
|
||||||
|
|
||||||
def handle_new_site_msg_recv(msg):
|
def handle_new_site_msg_recv(msg):
|
||||||
|
|
|
@ -115,11 +115,10 @@ def on_celery_task_pre_run(task_id='', kwargs=None, **others):
|
||||||
|
|
||||||
@signals.task_postrun.connect
|
@signals.task_postrun.connect
|
||||||
def on_celery_task_post_run(task_id='', state='', **kwargs):
|
def on_celery_task_post_run(task_id='', state='', **kwargs):
|
||||||
close_old_connections()
|
|
||||||
|
|
||||||
CeleryTaskExecution.objects.filter(id=task_id).update(
|
CeleryTaskExecution.objects.filter(id=task_id).update(
|
||||||
state=state, date_finished=timezone.now(), is_finished=True
|
state=state, date_finished=timezone.now(), is_finished=True
|
||||||
)
|
)
|
||||||
|
close_old_connections()
|
||||||
|
|
||||||
|
|
||||||
@signals.after_task_publish.connect
|
@signals.after_task_publish.connect
|
||||||
|
|
|
@ -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():
|
with safe_db_connection(auto_close=True):
|
||||||
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