diff --git a/src/bk-user/bkuser/apis/web/tenant_setting/views.py b/src/bk-user/bkuser/apis/web/tenant_setting/views.py index 28627f3f7..676023890 100644 --- a/src/bk-user/bkuser/apis/web/tenant_setting/views.py +++ b/src/bk-user/bkuser/apis/web/tenant_setting/views.py @@ -28,12 +28,9 @@ TenantUserValidityPeriodConfig, UserBuiltinField, ) -from bkuser.apps.tenant.periodic_tasks import send_tenant_user_expiring_notification from bkuser.common.error_codes import error_codes from bkuser.common.views import ExcludePatchAPIViewMixin, ExcludePutAPIViewMixin -send_tenant_user_expiring_notification() - class TenantUserFieldListApi(CurrentUserTenantMixin, generics.ListAPIView): pagination_class = None diff --git a/src/bk-user/bkuser/apps/tenant/periodic_tasks.py b/src/bk-user/bkuser/apps/tenant/periodic_tasks.py index a4285f20d..4b6e67dc3 100644 --- a/src/bk-user/bkuser/apps/tenant/periodic_tasks.py +++ b/src/bk-user/bkuser/apps/tenant/periodic_tasks.py @@ -15,7 +15,7 @@ from bkuser.apps.tenant.constants import NotificationScene from bkuser.apps.tenant.models import Tenant, TenantUser, TenantUserValidityPeriodConfig -from bkuser.apps.tenant.notifier import TenantUserValidityPeriodNotifier +from bkuser.apps.tenant.tasks import send_notifications from bkuser.celery import app from bkuser.common.task import BaseTask @@ -49,22 +49,24 @@ def send_tenant_user_expiring_notification(): should_notify_users = [] for remain_days in remind_before_expire: account_expired_date = now + datetime.timedelta(days=int(remain_days)) + account_expired_date_start = account_expired_date.replace(hour=0, minute=0, second=0) + should_notify_users += list( tenant_users.filter( account_expired_at__range=[ - account_expired_date.replace(hour=0, minute=0, second=0), - account_expired_date.replace(hour=23, minute=59, second=59, microsecond=999999), + account_expired_date_start, + account_expired_date_start + datetime.timedelta(days=1), ] - ) + ).values_list("id", flat=True) ) # 发送通知 logger.info("going to notify expiring users in tenant{%s}, count: %s", tenant_id, len(should_notify_users)) if not should_notify_users: - return + continue - TenantUserValidityPeriodNotifier(tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRING).send( - should_notify_users + send_notifications.delay( + tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRING, tenant_user_ids=should_notify_users ) @@ -77,24 +79,25 @@ def send_tenant_user_expired_notification(): now = timezone.now() # 获取 租户-过期用户 tenant_ids = Tenant.objects.filter().values_list("id", flat=True) + tenant_user_map = { tenant_id: TenantUser.objects.filter( account_expired_at__range=[ now.replace(hour=0, minute=0, second=0), - now.replace(hour=23, minute=59, second=59, microsecond=999999), + now + datetime.timedelta(days=1), ], tenant_id=tenant_id, - ) + ).values_list("id", flat=True) for tenant_id in tenant_ids } - for tenant_id, tenant_users in tenant_user_map.items(): + for tenant_id, tenant_user_ids in tenant_user_map.items(): # 发送过期通知 - should_notify_users = list(tenant_users) + should_notify_users = list(tenant_user_ids) logger.info("going to notify expired users in tenant{%s}, count: %s", tenant_id, len(should_notify_users)) if not should_notify_users: - return + continue - TenantUserValidityPeriodNotifier(tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRED).send( - should_notify_users + send_notifications.delay( + tenant_id=tenant_id, scene=NotificationScene.TENANT_USER_EXPIRED, tenant_user_ids=should_notify_users ) diff --git a/src/bk-user/bkuser/apps/tenant/tasks.py b/src/bk-user/bkuser/apps/tenant/tasks.py new file mode 100644 index 000000000..54ae01ce1 --- /dev/null +++ b/src/bk-user/bkuser/apps/tenant/tasks.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available. +Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at http://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. +""" +import logging +from typing import List + +from bkuser.apps.tenant.constants import NotificationScene +from bkuser.apps.tenant.models import TenantUser +from bkuser.apps.tenant.notifier import TenantUserValidityPeriodNotifier +from bkuser.celery import app +from bkuser.common.task import BaseTask + +logger = logging.getLogger(__name__) + + +@app.task(base=BaseTask, ignore_result=True) +def send_notifications(tenant_id: str, scene: NotificationScene, tenant_user_ids: List[str]): + users = TenantUser.objects.filter(id__in=tenant_user_ids) + logger.info( + "going to send notification for users. user_count=%s tenant=%s, scene=%s", len(users), tenant_id, scene + ) + try: + TenantUserValidityPeriodNotifier(tenant_id=tenant_id, scene=scene).send(users) + except Exception: + logger.exception("send notification failed, please check!")