蓝鲸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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 高数——多元函数的定义及极限

    高数——多元函数的定义及极限之前我们学习的导数、微分和积分都是针对一元函数的,也就是函数只依赖一个变量,但是在我们今后遇到的实际问题中,更多出现的却是要考虑多个变量的情况,这是我们就要用多元函数来表示它们之间的关系了。比如地球表面上一点的温度T同时依赖于纬度x和经度y,可以用一个二元函数T=f(x,y)来表示。和一元函数一样,二元函数也是有定义域和值域的,一元函数的定义域是轴上一个“线段”上的点的集合,而…

    2022年6月3日
    46
  • IDEA 如何进行全局搜索

    IDEA 如何进行全局搜索毕业季来临,很多小伙伴在忙于考公或者准备研究生复试等工作,因此从网上下载或者购买了源码。源码在本地运行成功之后,想要稍微修改一下,结果发现无从下手。本文就向大家介绍一个程序员在开发中最常见最常用的功能:全局搜索。全局搜索:不管是你用任何的开发语言Java/C#/Python等,或者用任何的开发工具IDEA/Eclipse/MyEclipse等。全局搜索都是快速定位代码的一种快捷方式。废话不多说,直接上…

    2022年6月16日
    151
  • 一文解释清卷积神经网络中池化层的作用「建议收藏」

    池化层:池化层夹在连续的卷积层中间,用于压缩数据和参数的量,减小过拟合。简而言之,如果输入是图像的话,那么池化层的最主要作用就是压缩图像。池化层分为3类,平均池化,最大池化和随机池化。拿最大池化举个例子:上图的例子是按步幅2进行2X2的最大池化时的处理顺序。最大池化是获得最大值的运算,“2X2”表示目标区域的大小。如上图所示,从2X2的窗口的移动间隔为2个元素。另外,一般来说,池化的窗口大小会和步幅设定成相同的值。比如3X3的窗口的步幅会设为3,4X4的窗口的步幅会设为4等。而最大池化的优点是:

    2022年4月16日
    177
  • 服务器ssh免密钥登陆(vscode远程连接服务器)

    VScode实现远程查看代码要使用一些插件,我用的是Remote-SSH,安装很简单,推荐看下下面的博客,写的很详细:https://blog.csdn.net/u010417914/article/details/96918562重要的是遇到的一些问题:1.我的服务器用的是Ubuntu18.04,生成rsa公钥和私钥后,注意要以附加到文件尾部的方式把公钥拷贝到…

    2022年4月14日
    55
  • 1. CMake 系列 – 从零构建动态库和静态库

    1. CMake 系列 – 从零构建动态库和静态库

    2021年11月22日
    47
  • vue webpak版本 查看_vue版本以及webpack版本

    vue webpak版本 查看_vue版本以及webpack版本vue作为大前端的主流框架更新速度也是极快。那么vue的更新会有哪些问题呢?最近在搭建vue框架的时候发现由于vue版本的快速迭代已经与原本般配的webpack产生了隔阂。webpack作为大前端的主流打包工具如果与之不兼容,会有越来越多的麻烦事情。经过反复测试,得出结论一篇vue与webpack最佳拍档组合版本号公布。npminitnpminstallwebpack@3.10.0v…

    2022年6月1日
    431

发表回复

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

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