Skip to content

Commit

Permalink
Merge pull request #111 from OVINC-CN/feat_task
Browse files Browse the repository at this point in the history
feat(chat): auto delete old message log
  • Loading branch information
OrenZhang authored Jan 3, 2025
2 parents 3d1458f + 7f93efe commit fbe0ad3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion apps/cel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": (),
},
}
10 changes: 8 additions & 2 deletions apps/chat/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit fbe0ad3

Please sign in to comment.