Git Product home page Git Product logo

Comments (3)

IMBlues avatar IMBlues commented on June 12, 2024

周期性同步本身通过 Celery 同步,只需要将对应的任务注册到任务表,由 celery beatcelery worker 进程分别负责定时和执行。

我们已经有了注册周期任务方法 bkuser_core.categories.plugin.utils.make_periodic_sync_task

def make_periodic_sync_task(category_id: int, interval_seconds: int):
    """创建同步周期任务"""
    try:
        schedule, _ = IntervalSchedule.objects.get_or_create(every=interval_seconds, period=IntervalSchedule.SECONDS)
    except IntervalSchedule.MultipleObjectsReturned:
        # 兼容目前已有 schedule
        schedule = IntervalSchedule.objects.filter(every=interval_seconds, period=IntervalSchedule.SECONDS)[0]

    # 通过 category_id 来做任务名
    PeriodicTask.objects.get_or_create(
        interval=schedule,
        name=str(category_id),
        task="bkuser_core.categories.tasks.adapter_sync",
        enabled=True,
        kwargs=json.dumps({"instance_id": category_id}),
    )

这里需要传入 category_idinterval_seconds 两个参数,前者直接传入目录对象的 ID,后者是周期任务的间隔,由于同步任务本身时间都较长,所以建议设置较长的间隔,以保证不会出现 celery 任务堆积,最后导致 Broker 堵死。

如果只是想一次性创建周期任务,那么直接进入 api 模块的 python shell,手动调用创建即可。

或者可以模仿 ldap 插件的方式,通过 handler 在每次创建或更新目录时自动更新周期任务

@receiver(post_category_create)
def create_sync_tasks(sender, category, **kwargs):
    if category.type not in [CategoryType.LDAP.value, CategoryType.MAD.value]:
        return

    logger.info("going to add periodic task for Category<%s>", category.id)
    make_periodic_sync_task(
        category_id=category.id,
        interval_seconds=get_plugin_by_category(category).extra_config["default_sync_period"],
    )

关于插件的配置方式还需要修改和优化,可以暂时用上述的方式解决

from bk-user.

IMBlues avatar IMBlues commented on June 12, 2024

配置的修改和优化方式,可以参考 #43 ,有任何想法都可以评论留言

from bk-user.

wklken avatar wklken commented on June 12, 2024

https://github.com/TencentBlueKing/bk-user/blob/development/src/api/bkuser_core/categories/management/commands/make_crontab_sync_task_for_category.py

支持Django command触发

from bk-user.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.