蓝鲸bk-sops源码学习二:流程组件注册实现原理「建议收藏」

蓝鲸bk-sops源码学习二:流程组件注册实现原理「建议收藏」研究背景自己的项目都是python3.6开发。想使用蓝鲸的流程系统,真是千难万难。魔改路上真是一路坎坷。由于BK-SOPS需要结合蓝鲸的一整套服务才能够运行,所以单独把标准运维的流程系统抽出来然后融合进自己的系统。看看蓝鲸标准运维的功能多元接入支持:标准运维对接了蓝鲸通知、作业平台、配置平台等服务,作为官方标准插件库提供服务,还支持用户自定义接入企业内部系统,定制开发标准插件。可视化流…

大家好,又见面了,我是你们的朋友全栈君。

研究背景

自己的项目都是python3.6开发。想使用蓝鲸的流程系统,真是千难万难。魔改路上真是一路坎坷。由于BK-SOPS需要结合蓝鲸的一整套服务才能够运行,所以单独把标准运维的流程系统抽出来然后融合进自己的系统。

看看蓝鲸标准运维的功能

  • 多元接入支持:标准运维对接了蓝鲸通知、作业平台、配置平台等服务,作为官方标准插件库提供服务,还支持用户自定义接入企业内部系统,定制 开发标准插件。
  • 可视化流程编排:通过拖拽方式组合标准插件节点到一个流程模板。
  • 多种流程模式:支持标准插件节点的串行、并行,支持子流程,可以根据全局参数自动选择 分支执行,节点失败处理机制可配置。
  • 参数引擎:支持参数共享,支持参数替换。
    可交互的任务执行:任务执行中可以随时暂停、继续、撤销,节点失败后可以重试、跳过。
  • 通用权限管理:通过配置平台同步业务角色,支持流程模板的使用权限控制。

改造日志

2020年10月20日

有博友留言问能不能独立部署,是可以的。下面是我之前研究的代码。能运行并且自定义节点什么的,我没有改完。欢迎大家一起学习,分享出来我之前研究的源码

https://github.com/CyberWong/django_pipeline

2019年08月23日

这里用到比较多的就是元类。
元类就是在构建类时候运行的,蓝鲸的组件就是在构建类时候把组件写到一个公共类,然后构造流程时候通过get方法,返回组件类。

代码一

from pipeline.component_framework.component import Component
class TestComponent(Component):
    name = 'test'
    code = 'test'
    bound_service = TestService
    form = 'test.js'

这就是个标准的test组件。继承Component,看看Component的结构

from pipeline.component_framework.base import ComponentMeta
class Component(object,metaclass=ComponentMeta):

    def __init__(self, data_dict):
        self.data_dict = data_dict

    @classmethod
    def outputs_format(cls):
        outputs = cls.bound_service().outputs()
        outputs = map(lambda oi: oi._asdict(), outputs)
        return outputs

    def clean_execute_data(self, context):
        return self.data_dict

    def data_for_execution(self, context, pipeline_data):
        data_dict = self.clean_execute_data(context)
        inputs = { 
   }

        for key, tag_info in data_dict.items():
            if tag_info is None:
                raise ComponentDataLackException('Lack of inputs: %s' % key)

            inputs[key] = get_variable(key, tag_info, context, pipeline_data)

        return DataObject(inputs)

    def service(self):
        return self.bound_service()

Python2和Python3使用元类的语法不一样,sops用的是Python2,这里需要修改一下

# python2
class Component(object):
    __metaclass__ = ComponentMeta

在python3里面,Python3默认就是新式类,声明的语法如下:

# python3
class Component(object,metaclass=ComponentMeta):
# or
class Component(metaclass=ComponentMeta):

研究了好久为什么通过ComponentLibrary获取不到自己定义的组件。ComponentLibrary就是一个组件库,Component类初始化时候会到ComponentLibrary组件库类中,注册的方法就是通过元类,也就是上面的ComponentMeta

class ComponentMeta(type):
    def __new__(cls, name, bases, attrs):
        super_new = super(ComponentMeta, cls).__new__
	
        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
    
        parents = [b for b in bases if isinstance(b, ComponentMeta)]
        # 为了确保只对子类初始化
        if not parents:
            return super_new(cls, name, bases, attrs)

        # Create the class
       	# 创建一个类,并动态引入
        module_name = attrs.pop('__module__')
        new_class = super_new(cls, name, bases, { 
   '__module__': module_name})
        module = importlib.import_module(new_class.__module__)

        # Add all attributes to the class
        attrs.setdefault('desc', '')
        for obj_name, obj in attrs.items():
            setattr(new_class, obj_name, obj)

        # check
        if not getattr(new_class, 'name', None):
            raise ValueError("component %s name can't be empty" %
                             new_class.__name__)

        if not getattr(new_class, 'code', None):
            raise ValueError("component %s code can't be empty" %
                             new_class.__name__)

        if not getattr(new_class, 'bound_service', None) or not issubclass(new_class.bound_service, Service):
            raise ValueError("component %s service can't be empty and must be subclass of Service" %
                             new_class.__name__)

        if not getattr(new_class, 'form', None):
            setattr(new_class, 'form', None)

        # category/group name
        # 在这里我们可以发现,可以自己自定义组名。使用__group_name__
        group_name = getattr(
            module, "__group_name__",
            new_class.__module__.split(".")[-1].title()
        )
        setattr(new_class, 'group_name', group_name)
        new_name = u"%s-%s" % (group_name, new_class.name)
		# 自定义icon
        # category/group name
        group_icon = getattr(
            module, "__group_icon__",
            ''
        )
        setattr(new_class, 'group_icon', group_icon)
		
        if not getattr(module, '__register_ignore__', False):
        	# 就是在这里
            ComponentLibrary.components[new_class.code] = new_class
            # try:
            # ComponentModel.objects.update_or_create(
            # code=new_class.code,
            # defaults={ 
   
            # 'name': new_name,
            # 'status': __debug__,
            # }
            # )
            # except Exception as e:
            # if not isinstance(e, ProgrammingError):
            # logging.exception(e)

        return new_class

下面再看看ComponentLibrary这个东西,很有趣的一种设计方法。

class ComponentLibrary(object):
    components = { 
   }

    def __new__(cls, *args, **kwargs):
    	# 
        component_code = kwargs.get('component_code', None)
        if args:
            component_code = args[0]
        if not component_code:
            raise ValueError('please pass a component_code in args or kwargs: '
                             'ComponentLibrary(\'code\') or ComponentLibrary(component_code=\'code\')')
        if component_code not in cls.components:
            raise ComponentNotExistException('component %s does not exist.' %
                                             component_code)
        return cls.components[component_code]

    @classmethod
    def get_component_class(cls, component_code):
        return cls.components.get(component_code)

    @classmethod
    def get_component(cls, component_code, data_dict):
        return cls.get_component_class(component_code)(data_dict)

受益匪浅。大牛的设计思想果然牛逼

蓝鲸bk-sops源码学习

一:流程图前端一
二:流程图前端二之节点创建
三:流程组件注册实现原理

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141413.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 移动端避免使用100vh[通俗易懂]

    移动端避免使用100vh[通俗易懂]CSS中的视口单位听起来很棒。如果要设置元素的样式以占据整个屏幕的高度,则可以设置height:100vh,您拥有一个完美的全屏元素,该元素会随着视口的变化而调整大小!可悲的是,事实并…

    2022年6月9日
    67
  • idea2021激活码获取-激活码分享

    (idea2021激活码获取)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    353
  • pip卸载所有包_pip导出包

    pip卸载所有包_pip导出包pip批量完全卸载包

    2022年10月17日
    2
  • java缓存技术的介绍

    java缓存技术的介绍一、什么是缓存1、Cache是高速缓冲存储器一种特殊的存储器子系统,其中复制了频繁使用的数据以利于快速访问2、凡是位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为Cache二、缓存的分类1、基于web应用的系统架构图2、在系统架构的不同层级之间,为了加快访问速度,都可以存在缓存操作系统磁盘缓存->减少磁盘机械操作

    2022年10月5日
    2
  • 两种Ajax写法_js调用java方法

    两种Ajax写法_js调用java方法Ajax:(AsynchronousJavascriptAndXML)简称为异步的js和xmljs中有两种写法://原生js写法functionshow(){varxhr=newXMLHttpRequest();xhr.onreadystatechange=function(){if(xhr.readyState==4&&xhr.status==200){…

    2022年9月27日
    5
  • fedora内核版本_ubuntu内核升级

    fedora内核版本_ubuntu内核升级声明一下我的系统环境:WindowsXPSP3+Vmware7虚拟机Fedora12DVDi386安装镜像。首先安装的时候要注意,在选择安装文件的时候选择“网页,软件开发,服务器。。。”这样就用有GCC了,以及其他的插件。 1,下载内核官方网址:www.kernel.org,我下载的版本是2.6.33,文件名为:linux-2.6.33.tar,但是到Li

    2025年11月11日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号