mirror of
https://github.com/jumpserver/jumpserver.git
synced 2026-03-18 11:02:09 +00:00
perf: change sqlite to signals
This commit is contained in:
@@ -15,7 +15,7 @@ class CommonConfig(AppConfig):
|
||||
from . import tasks # noqa
|
||||
from .signals import django_ready
|
||||
|
||||
excludes = ['migrate', 'compilemessages', 'makemigrations', 'start']
|
||||
excludes = ['migrate', 'compilemessages', 'makemigrations', 'start', 'shell']
|
||||
for i in excludes:
|
||||
if i in sys.argv:
|
||||
return
|
||||
|
||||
@@ -9,9 +9,9 @@ class SettingsConfig(AppConfig):
|
||||
def ready(self):
|
||||
from . import signal_handlers # noqa
|
||||
from . import tasks # noqa
|
||||
from .models import init_sqlite_db, register_sqlite_connection
|
||||
try:
|
||||
init_sqlite_db()
|
||||
register_sqlite_connection()
|
||||
except Exception:
|
||||
pass
|
||||
# from .models import init_sqlite_db, register_sqlite_connection
|
||||
# try:
|
||||
# init_sqlite_db()
|
||||
# register_sqlite_connection()
|
||||
# except Exception:
|
||||
# pass
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.files.storage import default_storage
|
||||
from django.core.files.uploadedfile import InMemoryUploadedFile
|
||||
from django.db import models, connections
|
||||
from django.db import models
|
||||
from django.db.utils import ProgrammingError, OperationalError
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework.utils.encoders import JSONEncoder
|
||||
@@ -17,6 +15,7 @@ from common.utils import get_logger
|
||||
from .const import ChatAITypeChoices
|
||||
from .signals import setting_changed
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
@@ -212,35 +211,6 @@ def get_chatai_data():
|
||||
return data
|
||||
|
||||
|
||||
def init_sqlite_db():
|
||||
db_path = settings.LEAK_PASSWORD_DB_PATH
|
||||
if not os.path.isfile(db_path):
|
||||
# 这里处理一下历史数据,有可能用户 copy 了旧的文件到 目录下
|
||||
src = os.path.join(settings.PROJECT_DIR, 'data', 'leak_passwords.db')
|
||||
if not os.path.isfile(src):
|
||||
src = os.path.join(
|
||||
settings.APPS_DIR, 'accounts', 'automations',
|
||||
'check_account', 'leak_passwords.db'
|
||||
)
|
||||
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
||||
shutil.copy(src, db_path)
|
||||
logger.info(f'init sqlite db {db_path}')
|
||||
return db_path
|
||||
|
||||
|
||||
def register_sqlite_connection():
|
||||
connections.databases['sqlite'] = {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'ATOMIC_REQUESTS': False,
|
||||
'NAME': settings.LEAK_PASSWORD_DB_PATH,
|
||||
'TIME_ZONE': None,
|
||||
'CONN_HEALTH_CHECKS': False,
|
||||
'CONN_MAX_AGE': 0,
|
||||
'OPTIONS': {},
|
||||
'AUTOCOMMIT': True,
|
||||
}
|
||||
|
||||
|
||||
class LeakPasswords(models.Model):
|
||||
id = models.AutoField(primary_key=True, verbose_name=_("ID"))
|
||||
password = models.CharField(max_length=1024, verbose_name=_("Password"))
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
import json
|
||||
|
||||
from django.conf import LazySettings
|
||||
import os
|
||||
import shutil
|
||||
from django.db import connections
|
||||
from django.conf import LazySettings, settings
|
||||
from django.db.models.signals import post_save
|
||||
from django.db.utils import ProgrammingError, OperationalError
|
||||
from django.dispatch import receiver
|
||||
from django.db.models.signals import post_migrate
|
||||
from django.utils.functional import LazyObject
|
||||
from jumpserver.const import BASE_DIR
|
||||
|
||||
from common.decorators import on_transaction_commit
|
||||
from common.signals import django_ready
|
||||
@@ -75,3 +79,34 @@ def monkey_patch_settings(sender, **kwargs):
|
||||
LazySettings.__getattr__ = monkey_patch_getattr
|
||||
except (ProgrammingError, OperationalError):
|
||||
pass
|
||||
|
||||
|
||||
@receiver(post_migrate, dispatch_uid='settings.signal_handlers.init_sqlite_db')
|
||||
def init_sqlite_db(sender, **kwargs):
|
||||
db_path = settings.LEAK_PASSWORD_DB_PATH
|
||||
if not os.path.isfile(db_path):
|
||||
# 这里处理一下历史数据,有可能用户 copy 了旧的文件到 目录下
|
||||
src = os.path.join(settings.PROJECT_DIR, 'data', 'leak_passwords.db')
|
||||
if not os.path.isfile(src):
|
||||
src = os.path.join(
|
||||
settings.APPS_DIR, 'accounts', 'automations',
|
||||
'check_account', 'leak_passwords.db'
|
||||
)
|
||||
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
||||
shutil.copy(src, db_path)
|
||||
logger.info(f'init sqlite db {db_path}')
|
||||
return db_path
|
||||
|
||||
|
||||
@receiver(django_ready)
|
||||
def register_sqlite_connection(sender, **kwargs):
|
||||
connections.databases['sqlite'] = {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'ATOMIC_REQUESTS': False,
|
||||
'NAME': settings.LEAK_PASSWORD_DB_PATH,
|
||||
'TIME_ZONE': None,
|
||||
'CONN_HEALTH_CHECKS': False,
|
||||
'CONN_MAX_AGE': 0,
|
||||
'OPTIONS': {},
|
||||
'AUTOCOMMIT': True,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user