From 9439035b866ccf8ef1d10385911ec12e99b38a48 Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Fri, 7 Jun 2024 11:20:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B4=A6=E5=8F=B7=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=EF=BC=8C=E4=BA=91=E5=90=8C=E6=AD=A5=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E4=B8=8D=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/celery/decorator.py | 21 ++++++++++++--------- apps/ops/celery/utils.py | 3 +++ apps/settings/serializers/auth/ldap.py | 9 ++++++--- apps/settings/tasks/ldap.py | 18 +++++++----------- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/apps/ops/celery/decorator.py b/apps/ops/celery/decorator.py index 45626bb0f..317a7f7ae 100644 --- a/apps/ops/celery/decorator.py +++ b/apps/ops/celery/decorator.py @@ -57,21 +57,22 @@ def register_as_period_task( task = '{func.__module__}.{func.__name__}'.format(func=func) _name = name if name else task add_register_period_task({ - _name: { - 'task': task, - 'interval': interval, - 'crontab': crontab, - 'args': args, - 'kwargs': kwargs if kwargs else {}, - 'enabled': True, - 'description': description - } + _name: { + 'task': task, + 'interval': interval, + 'crontab': crontab, + 'args': args, + 'kwargs': kwargs if kwargs else {}, + 'description': description + } }) @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) + return wrapper + return decorate @@ -85,6 +86,7 @@ def after_app_ready_start(func): @wraps(func) def decorate(*args, **kwargs): return func(*args, **kwargs) + return decorate @@ -98,4 +100,5 @@ def after_app_shutdown_clean_periodic(func): @wraps(func) def decorate(*args, **kwargs): return func(*args, **kwargs) + return decorate diff --git a/apps/ops/celery/utils.py b/apps/ops/celery/utils.py index 580a256fe..1db7b4675 100644 --- a/apps/ops/celery/utils.py +++ b/apps/ops/celery/utils.py @@ -80,6 +80,9 @@ def create_or_update_celery_periodic_tasks(tasks): description=detail.get('description') or '', last_run_at=last_run_at, ) + enabled = detail.get('enabled') + if enabled is not None: + defaults["enabled"] = enabled task = PeriodicTask.objects.update_or_create( defaults=defaults, name=name, ) diff --git a/apps/settings/serializers/auth/ldap.py b/apps/settings/serializers/auth/ldap.py index 5e0abf1c3..e47a8cf9f 100644 --- a/apps/settings/serializers/auth/ldap.py +++ b/apps/settings/serializers/auth/ldap.py @@ -93,7 +93,10 @@ class LDAPSettingSerializer(serializers.Serializer): AUTH_LDAP = serializers.BooleanField(required=False, label=_('Enable LDAP auth')) - @staticmethod - def post_save(): + def post_save(self): + keys = ['AUTH_LDAP_SYNC_IS_PERIODIC', 'AUTH_LDAP_SYNC_INTERVAL', 'AUTH_LDAP_SYNC_CRONTAB'] + kwargs = {k: self.validated_data[k] for k in keys if k in self.validated_data} + if not kwargs: + return from settings.tasks import import_ldap_user_periodic - import_ldap_user_periodic() + import_ldap_user_periodic(**kwargs) diff --git a/apps/settings/tasks/ldap.py b/apps/settings/tasks/ldap.py index e600017cb..1882bf37b 100644 --- a/apps/settings/tasks/ldap.py +++ b/apps/settings/tasks/ldap.py @@ -9,7 +9,7 @@ from common.utils import get_logger from common.utils.timezone import local_now_display from ops.celery.decorator import after_app_ready_start from ops.celery.utils import ( - create_or_update_celery_periodic_tasks, delete_celery_periodic_task + create_or_update_celery_periodic_tasks, disable_celery_periodic_task ) from orgs.models import Organization from settings.notifications import LDAPImportMessage @@ -65,20 +65,15 @@ def import_ldap_user(): @shared_task(verbose_name=_('Registration periodic import ldap user task')) @after_app_ready_start -def import_ldap_user_periodic(): - if not settings.AUTH_LDAP: - return +def import_ldap_user_periodic(**kwargs): task_name = 'import_ldap_user_periodic' - delete_celery_periodic_task(task_name) - if not settings.AUTH_LDAP_SYNC_IS_PERIODIC: - return - - interval = settings.AUTH_LDAP_SYNC_INTERVAL + interval = kwargs.get('AUTH_LDAP_SYNC_INTERVAL', settings.AUTH_LDAP_SYNC_INTERVAL) + enabled = kwargs.get('AUTH_LDAP_SYNC_IS_PERIODIC', settings.AUTH_LDAP_SYNC_IS_PERIODIC) + crontab = kwargs.get('AUTH_LDAP_SYNC_CRONTAB', settings.AUTH_LDAP_SYNC_CRONTAB) if isinstance(interval, int): interval = interval * 3600 else: interval = None - crontab = settings.AUTH_LDAP_SYNC_CRONTAB if crontab: # 优先使用 crontab interval = None @@ -86,7 +81,8 @@ def import_ldap_user_periodic(): task_name: { 'task': import_ldap_user.name, 'interval': interval, - 'crontab': crontab + 'crontab': crontab, + 'enabled': enabled } } create_or_update_celery_periodic_tasks(tasks)