diff --git a/api/extensions/ext_celery.py b/api/extensions/ext_celery.py index 26bd6b357712c9..30f216ff95612b 100644 --- a/api/extensions/ext_celery.py +++ b/api/extensions/ext_celery.py @@ -69,7 +69,6 @@ def __call__(self, *args: object, **kwargs: object) -> object: "schedule.create_tidb_serverless_task", "schedule.update_tidb_serverless_status_task", "schedule.clean_messages", - "schedule.mail_clean_document_notify_task", ] day = dify_config.CELERY_BEAT_SCHEDULER_TIME beat_schedule = { @@ -93,11 +92,6 @@ def __call__(self, *args: object, **kwargs: object) -> object: "task": "schedule.clean_messages.clean_messages", "schedule": timedelta(days=day), }, - # every Monday - "mail_clean_document_notify_task": { - "task": "schedule.mail_clean_document_notify_task.mail_clean_document_notify_task", - "schedule": crontab(minute="0", hour="10", day_of_week="1"), - }, } celery_app.conf.update(beat_schedule=beat_schedule, imports=imports) diff --git a/api/schedule/mail_clean_document_notify_task.py b/api/schedule/mail_clean_document_notify_task.py index fe6839288d8503..766954a257371f 100644 --- a/api/schedule/mail_clean_document_notify_task.py +++ b/api/schedule/mail_clean_document_notify_task.py @@ -3,18 +3,14 @@ from collections import defaultdict import click -from flask import render_template # type: ignore +from celery import shared_task # type: ignore -import app -from configs import dify_config -from extensions.ext_database import db from extensions.ext_mail import mail from models.account import Account, Tenant, TenantAccountJoin from models.dataset import Dataset, DatasetAutoDisableLog -from services.feature_service import FeatureService -@app.celery.task(queue="dataset") +@shared_task(queue="mail") def send_document_clean_notify_task(): """ Async Send document clean notify mail @@ -33,58 +29,35 @@ def send_document_clean_notify_task(): # group by tenant_id dataset_auto_disable_logs_map: dict[str, list[DatasetAutoDisableLog]] = defaultdict(list) for dataset_auto_disable_log in dataset_auto_disable_logs: - if dataset_auto_disable_log.tenant_id not in dataset_auto_disable_logs_map: - dataset_auto_disable_logs_map[dataset_auto_disable_log.tenant_id] = [] dataset_auto_disable_logs_map[dataset_auto_disable_log.tenant_id].append(dataset_auto_disable_log) - url = f"{dify_config.CONSOLE_WEB_URL}/datasets" - for tenant_id, tenant_dataset_auto_disable_logs in dataset_auto_disable_logs_map.items(): - features = FeatureService.get_features(tenant_id) - plan = features.billing.subscription.plan - if plan != "sandbox": - knowledge_details = [] - # check tenant - tenant = Tenant.query.filter(Tenant.id == tenant_id).first() - if not tenant: - continue - # check current owner - current_owner_join = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, role="owner").first() - if not current_owner_join: - continue - account = Account.query.filter(Account.id == current_owner_join.account_id).first() - if not account: - continue - dataset_auto_dataset_map = {} # type: ignore - for dataset_auto_disable_log in tenant_dataset_auto_disable_logs: - if dataset_auto_disable_log.dataset_id not in dataset_auto_dataset_map: - dataset_auto_dataset_map[dataset_auto_disable_log.dataset_id] = [] - dataset_auto_dataset_map[dataset_auto_disable_log.dataset_id].append( - dataset_auto_disable_log.document_id - ) + for tenant_id, tenant_dataset_auto_disable_logs in dataset_auto_disable_logs_map.items(): + knowledge_details = [] + tenant = Tenant.query.filter(Tenant.id == tenant_id).first() + if not tenant: + continue + current_owner_join = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, role="owner").first() + if not current_owner_join: + continue + account = Account.query.filter(Account.id == current_owner_join.account_id).first() + if not account: + continue + + dataset_auto_dataset_map = {} # type: ignore + for dataset_auto_disable_log in tenant_dataset_auto_disable_logs: + dataset_auto_dataset_map[dataset_auto_disable_log.dataset_id].append( + dataset_auto_disable_log.document_id + ) - for dataset_id, document_ids in dataset_auto_dataset_map.items(): - dataset = Dataset.query.filter(Dataset.id == dataset_id).first() - if dataset: - document_count = len(document_ids) - knowledge_details.append(rf"Knowledge base {dataset.name}: {document_count} documents") - if knowledge_details: - html_content = render_template( - "clean_document_job_mail_template-US.html", - userName=account.email, - knowledge_details=knowledge_details, - url=url, - ) - mail.send( - to=account.email, subject="Dify Knowledge base auto disable notification", html=html_content - ) + for dataset_id, document_ids in dataset_auto_dataset_map.items(): + dataset = Dataset.query.filter(Dataset.id == dataset_id).first() + if dataset: + document_count = len(document_ids) + knowledge_details.append(f"
Dear {{userName}},
We're sorry for the inconvenience. To ensure optimal performance, documents - that haven’t been updated or accessed in the past 30 days have been disabled in + that haven’t been updated or accessed in the past 7 days have been disabled in your knowledge bases:
You can re-enable them anytime.
Re-enable in Dify