From bfa97e760efae0fe87bfe15ffeb6be3f975823bd Mon Sep 17 00:00:00 2001 From: chakcy <947105045@qq.com> Date: Fri, 7 Nov 2025 19:38:09 +0800 Subject: [PATCH] =?UTF-8?q?modify:=20=E4=BC=98=E5=8C=96=E6=B8=85=E7=90=86?= =?UTF-8?q?=E8=B0=83=E5=BA=A6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/queue_sqlite/__init__.py | 2 +- .../scheduler/cleanup_scheduler.py | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5dac9c2..fb025a6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ build/ dist/ wheels/ *.egg-info +.python-version # Virtual environments .venv diff --git a/src/queue_sqlite/__init__.py b/src/queue_sqlite/__init__.py index f24fa75..4ccb39d 100644 --- a/src/queue_sqlite/__init__.py +++ b/src/queue_sqlite/__init__.py @@ -33,7 +33,7 @@ error_log_handler.setFormatter( logging.getLogger().addHandler(error_log_handler) __title__ = "queue_sqlite" -__version__ = "0.2.0" +__version__ = "0.2.1" __author__ = "chakcy" __email__ = "947105045@qq.com" __license__ = "MIT" diff --git a/src/queue_sqlite/scheduler/cleanup_scheduler.py b/src/queue_sqlite/scheduler/cleanup_scheduler.py index c44a636..31acef6 100644 --- a/src/queue_sqlite/scheduler/cleanup_scheduler.py +++ b/src/queue_sqlite/scheduler/cleanup_scheduler.py @@ -14,6 +14,7 @@ import threading import time from queue_sqlite_core import ShardedQueueOperation import logging +import os class CleanupScheduler: @@ -28,6 +29,7 @@ class CleanupScheduler: self.is_running = False self.cleanup_thread = None self.remove_days = remove_days + self.max_db_size_mb = 500 # for i in range(self.queue_operation.shard_num): # 清理过期但未处理的消息 @@ -40,6 +42,7 @@ class CleanupScheduler: while self.is_running: try: self.queue_operation.clean_expired_messages() + self.remove_days = self._check_and_optimize_db(self.remove_days) except Exception as e: logging.error(f"清理消息错误: {str(e)}") @@ -50,6 +53,37 @@ class CleanupScheduler: break time.sleep(1) + def _check_and_optimize_db(self, remove_days): + """检查数据库大小并优化""" + try: + # 获取数据库文件大小 + db_size_mb = self._get_database_size() + + if db_size_mb > self.max_db_size_mb: + logging.info(f"数据库大小 {db_size_mb}MB 超过限制,执行优化...") + print(f"数据库大小 {db_size_mb}MB 超过限制,执行优化...") + + if remove_days > 1: + remove_days = remove_days - 1 + + self.queue_operation.clean_old_messages(remove_days) + + except Exception as e: + logging.error(f"数据库优化错误: {str(e)}") + + return remove_days + + def _get_database_size(self) -> int: + """获取数据库文件大小""" + # 实现获取数据库文件大小的逻辑 + total_size = 0 + db_path = self.queue_operation.db_dir + for root, _, files in os.walk(db_path): + for file in files: + file_path = os.path.join(root, file) + total_size += os.path.getsize(file_path) + return total_size // (1024 * 1024) + def start_cleanup(self): if self.is_running: return