mirror of
https://github.com/langgenius/dify.git
synced 2026-01-22 03:32:58 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
import datetime
|
|
import logging
|
|
import time
|
|
|
|
import click
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
import app
|
|
from configs import dify_config
|
|
from extensions.ext_database import db
|
|
from extensions.ext_redis import redis_client
|
|
from models.model import (
|
|
App,
|
|
Message,
|
|
MessageAgentThought,
|
|
MessageAnnotation,
|
|
MessageChain,
|
|
MessageFeedback,
|
|
MessageFile,
|
|
)
|
|
from models.web import SavedMessage
|
|
from services.feature_service import FeatureService
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@app.celery.task(queue="dataset")
|
|
def clean_messages():
|
|
click.echo(click.style("Start clean messages.", fg="green"))
|
|
start_at = time.perf_counter()
|
|
plan_sandbox_clean_message_day = datetime.datetime.now() - datetime.timedelta(
|
|
days=dify_config.PLAN_SANDBOX_CLEAN_MESSAGE_DAY_SETTING
|
|
)
|
|
while True:
|
|
try:
|
|
# Main query with join and filter
|
|
messages = (
|
|
db.session.query(Message)
|
|
.where(Message.created_at < plan_sandbox_clean_message_day)
|
|
.order_by(Message.created_at.desc())
|
|
.limit(100)
|
|
.all()
|
|
)
|
|
|
|
except SQLAlchemyError:
|
|
raise
|
|
if not messages:
|
|
break
|
|
for message in messages:
|
|
app = db.session.query(App).filter_by(id=message.app_id).first()
|
|
if not app:
|
|
logger.warning(
|
|
"Expected App record to exist, but none was found, app_id=%s, message_id=%s",
|
|
message.app_id,
|
|
message.id,
|
|
)
|
|
continue
|
|
features_cache_key = f"features:{app.tenant_id}"
|
|
plan_cache = redis_client.get(features_cache_key)
|
|
if plan_cache is None:
|
|
features = FeatureService.get_features(app.tenant_id)
|
|
redis_client.setex(features_cache_key, 600, features.billing.subscription.plan)
|
|
plan = features.billing.subscription.plan
|
|
else:
|
|
plan = plan_cache.decode()
|
|
if plan == "sandbox":
|
|
# clean related message
|
|
db.session.query(MessageFeedback).where(MessageFeedback.message_id == message.id).delete(
|
|
synchronize_session=False
|
|
)
|
|
db.session.query(MessageAnnotation).where(MessageAnnotation.message_id == message.id).delete(
|
|
synchronize_session=False
|
|
)
|
|
db.session.query(MessageChain).where(MessageChain.message_id == message.id).delete(
|
|
synchronize_session=False
|
|
)
|
|
db.session.query(MessageAgentThought).where(MessageAgentThought.message_id == message.id).delete(
|
|
synchronize_session=False
|
|
)
|
|
db.session.query(MessageFile).where(MessageFile.message_id == message.id).delete(
|
|
synchronize_session=False
|
|
)
|
|
db.session.query(SavedMessage).where(SavedMessage.message_id == message.id).delete(
|
|
synchronize_session=False
|
|
)
|
|
db.session.query(Message).where(Message.id == message.id).delete()
|
|
db.session.commit()
|
|
end_at = time.perf_counter()
|
|
click.echo(click.style(f"Cleaned messages from db success latency: {end_at - start_at}", fg="green"))
|