From 80b9db417c3798a6c0b011fc55604e9f67339a40 Mon Sep 17 00:00:00 2001 From: Bai Date: Mon, 7 Sep 2020 20:33:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(ldap):=20=E8=8E=B7=E5=8F=96ldap=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=88=97=E8=A1=A8=EF=BC=8C=E9=87=87=E7=94=A8=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/settings/api.py | 10 ++++++---- apps/settings/tasks/ldap.py | 7 ++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/apps/settings/api.py b/apps/settings/api.py index 786c43bad..2c7c31d13 100644 --- a/apps/settings/api.py +++ b/apps/settings/api.py @@ -2,6 +2,7 @@ # import json +import threading from collections.abc import Iterable from smtplib import SMTPSenderRefused from rest_framework import generics @@ -15,7 +16,7 @@ from .utils import ( LDAPServerUtil, LDAPCacheUtil, LDAPImportUtil, LDAPSyncUtil, LDAP_USE_CACHE_FLAGS, LDAPTestUtil, ObjectDict ) -from .tasks import sync_ldap_user_task +from .tasks import sync_ldap_user from common.permissions import IsOrgAdmin, IsSuperUser from common.utils import get_logger from .serializers import ( @@ -204,8 +205,9 @@ class LDAPUserListApi(generics.ListAPIView): if sync_util.task_no_start: # 任务外部设置 task running 状态 sync_util.set_task_status(sync_util.TASK_STATUS_IS_RUNNING) - task = sync_ldap_user_task.delay() - data = {'msg': 'Cache no data, sync task {} started.'.format(task.id)} + t = threading.Thread(target=sync_ldap_user) + t.start() + data = {'msg': 'Sync start.'} return Response(data=data, status=409) # 同步任务正在执行 if sync_util.task_is_running: @@ -214,7 +216,7 @@ class LDAPUserListApi(generics.ListAPIView): # 同步任务执行结束 if sync_util.task_is_over: msg = sync_util.get_task_error_msg() - data = {'error': 'Synchronization task report error: {}'.format(msg)} + data = {'error': 'Synchronization error: {}'.format(msg)} return Response(data=data, status=400) return super().list(request, *args, **kwargs) diff --git a/apps/settings/tasks/ldap.py b/apps/settings/tasks/ldap.py index 60058e03e..6869d0254 100644 --- a/apps/settings/tasks/ldap.py +++ b/apps/settings/tasks/ldap.py @@ -1,17 +1,14 @@ # coding: utf-8 # -from celery import shared_task - from common.utils import get_logger from ..utils import LDAPSyncUtil -__all__ = ['sync_ldap_user_task'] +__all__ = ['sync_ldap_user'] logger = get_logger(__file__) -@shared_task -def sync_ldap_user_task(): +def sync_ldap_user(): LDAPSyncUtil().perform_sync()