Skip to content

Commit

Permalink
docs: 补充自定义ExecutableEndEvent 的使用说明
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshuaikang authored and normal-wls committed Jul 3, 2023
1 parent 25d829e commit 48957b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bamboo-engine 是一个通用的流程引擎,他可以解析,执行,调度
- bamboo-pipeline
- [自定义组件](./docs/user_guide/custom_component.md)
- [运行自定义组件](./docs/user_guide/run_your_component.md)
- [自定义结束事件](./docs/user_guide/custom_end_event.md)
- [组件单元测试](./docs/user_guide/component_unit_test.md)
- [Worker 配置](./docs/user_guide/workers.md)
- [引擎管理端配置](./docs/user_guide/engine_admin_config.md)
Expand Down
50 changes: 50 additions & 0 deletions docs/user_guide/custom_end_event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 自定义结束事件处理器

在某些场景下,我们可能会需要在一个任务结束之后执行一段自定义的业务逻辑,很多上层的应用都会封装自己的任务抽象,例如希望pipeline的任务结束时修改业务层的任务状态为**已完成**

此时使用默认的`EmptyEndEvent` ,我们除了轮询任务状态之后我们将无法第一时间感知到任务结束的事件。

`ExecutableEndEvent` 提供了一种可执行的结束事件,允许我们在运行到结束节点时执行一段特定的业务逻辑。

具体使用方式如下:

首先我们需要自定义一个我们自己的`ExecutableEndEvent`实现,并将它注册到FlowNodeClsFactory里面。通常我们建议这段逻辑放到应用初始化的阶段来做。

之后为们需要实现自己的 execute 方法,该方法接收三个参数:

- in_subprocess 是否是子流程的end事件
- root_pipeline_id 根 `pipeline id`
- current_pipeline_id 当前的`pipeline_id`

```python
from bamboo_engine.validator import api
from pipeline.core.flow import ExecutableEndEvent, FlowNodeClsFactory

class CustomExecutableEndEvent(ExecutableEndEvent):
def execute(self, in_subprocess, root_pipeline_id, current_pipeline_id):
print("it is executed")

api.add_sink_type("CustomExecutableEndEvent")
FlowNodeClsFactory.register_node("CustomExecutableEndEvent", CustomExecutableEndEvent)
```

之后我们需要再构建流程时使用ExecutableEndEvent并指定为我们自定义的`CustomExecutableEndEvent`。需要注意的是,构建流程的`ExecutableEndEvent` 使用的是`bamboo_engine.builder`模块下的。

构建一个新的实例:
```python

from pipeline.eri.runtime import BambooDjangoRuntime
from bamboo_engine import api
from bamboo_engine.builder import EmptyStartEvent, ServiceActivity, Data, build_tree,ExecutableEndEvent

start = EmptyStartEvent()
buy_ticket = ServiceActivity(component_code='example_component', name='example_component')
# 这里的type需要指定为我们自定义的type
end = ExecutableEndEvent(type="CustomExecutableEndEvent")
start.extend(buy_ticket).extend(end)
pipeline_data = Data()
pipeline = build_tree(start, data=pipeline_data)
api.run_pipeline(runtime=BambooDjangoRuntime(), pipeline=pipeline)
```

之后当流程运行到结束节点时,将会调用CustomExecutableEndEvent的execute方法了。

0 comments on commit 48957b4

Please sign in to comment.