From 366b5d681eef454197a596a2ced78660ac4c34d1 Mon Sep 17 00:00:00 2001 From: orenzhang Date: Fri, 3 Jan 2025 14:33:38 +0800 Subject: [PATCH 1/2] feat(chat): auto delete old message log --- apps/cel/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/cel/__init__.py b/apps/cel/__init__.py index caa314b..bcb732a 100644 --- a/apps/cel/__init__.py +++ b/apps/cel/__init__.py @@ -17,5 +17,10 @@ "task": "apps.chat.tasks.openrouter_model_sync", "schedule": crontab(minute="*/10"), "args": (), - } + }, + "delete_old_history": { + "task": "apps.chat.tasks.delete_old_history", + "schedule": crontab(minute="0", hour="8"), + "args": (), + }, } From 7f93efe5829ce4feedcc9e3ea099af7d6bce7026 Mon Sep 17 00:00:00 2001 From: orenzhang Date: Fri, 3 Jan 2025 14:38:44 +0800 Subject: [PATCH 2/2] fix(chat): old message log filter failed --- apps/chat/tasks.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/chat/tasks.py b/apps/chat/tasks.py index 5d37b0d..3225a1e 100644 --- a/apps/chat/tasks.py +++ b/apps/chat/tasks.py @@ -134,11 +134,17 @@ def delete_old_history(self): celery_logger.info("[DeleteOldMessageHistory] Start %s", self.request.id) logs = ChatMessageChangeLog.objects.filter( - updated_lt=(timezone.now() - datetime.timedelta(days=settings.MESSAGE_LOG_RETAIN_DAYS)) + created_at__lt=(timezone.now() - datetime.timedelta(days=settings.MESSAGE_LOG_RETAIN_DAYS)) ).prefetch_related("user") celery_logger.info("[DeleteOldMessageHistory] Total: %d", logs.count()) for log in logs: - celery_logger.info("[DeleteOldMessageHistory] User: %s; MessageID: %s", log.user, log.message_id) + celery_logger.info( + "[DeleteOldMessageHistory] ID: %d; User: %s; MessageID: %s; CreatedAt: %s", + log.id, + log.user, + log.message_id, + log.created_at, + ) logs.delete() celery_logger.info("[DeleteOldMessageHistory] End %s", self.request.id)