flow

package module
v1.0.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2023 License: GPL-2.0 Imports: 28 Imported by: 0

README

工作流引擎

本项目是根据github.com/antlinker/flow演变而来,原来的项目长时间不更新,并且代码qlang部分无法跑通.

目前这个项目支持如下类型:

  1. 开始/结束/终止事件
  2. 人工任务/脚本任务
  3. 排他网关/并行网关
  4. 支持rpc println(Rpc("GET", "https://httpbin.org/ip", nil))

注:暂时不支持子流程,后续有支持计划

工作流设计器

获取项目

go get github.com/meixiaofei/flow-bpmn

使用

1. 初始化工作流引擎
package main
import (
    "github.com/meixiaofei/flow-bpmn"
    "github.com/meixiaofei/flow-bpmn/service/db"
    _ "github.com/go-sql-driver/mysql"
)
func main() {
    flow.Init(
		db.SetDSN("root:123456@tcp(127.0.0.1:3306)/flows?charset=utf8"),
		db.SetTrace(true),
	)
}

2. 加载工作流文件
	err := flow.LoadFile("leave.bpmn")
	if err != nil {
		// 处理错误
	}
3. 发起流程
  input := map[string]interface{}{
	"day": 1,
  }

	result, err := flow.StartFlow("process_leave_test", "流程发起人ID", input)
	if err != nil {
		// 处理错误
	}
4. 查询待办流程列表
	todos, err := flow.QueryTodoFlows("process_leave_test", "1")
	if err != nil {
		// 处理错误
	}
5. 处理流程
  input := map[string]interface{}{
	"action": "pass",
  }

  result, err = flow.HandleFlow("待办流程节点实例ID", "流程处理人ID", input)
	if err != nil {
		// 处理错误
	}
6. 停止流程
	err := flow.StopFlow("待办流程节点实例ID", func(flowInstance *schema.FlowInstance) bool {
		return flowInstance.Launcher == "XXX"
	})
	if err != nil {
		// 处理错误
	}
7. 接入WEB流程管理
func main() {
serverOptions := []flow.ServerOption{
	    flow.ServerStaticRootOption("./web"),
	    flow.ServerPrefixOption("/flow/"),
	    flow.ServerMiddlewareOption(filter),
	}

	http.Handle("/flow/", flow.StartServer(serverOptions...))
}

func filter(ctx *gear.Context) error {
	fmt.Printf("请求参数:%s - %s \n", ctx.Path, ctx.Method)
	return nil
}
8. 查询流程待办数据
	result,err := flow.QueryTodoFlows("process_leave_test","流程处理人ID")
	if err != nil {
		// 处理错误
	}
9. 查询流程历史数据
result,err := flow.QueryFlowHistory("待办流程实例ID")
if err != nil {
	// 处理错误
}
10. 查询已办理的流程实例ID列表
ids,err := flow.QueryDoneFlowIDs("process_leave_test","流程处理人ID")
if err != nil {
	// 处理错误
}
11. 查询节点实例的候选人ID列表
ids,err := flow.QueryNodeCandidates("待办流程节点实例ID")
if err != nil {
	// 处理错误
}
12. 停止流程实例
	err := flow.StopFlowInstance("待办流程节点实例ID", func(flowInstance *schema.FlowInstance) bool {
		return flowInstance.Launcher == "XXX"
	})
	if err != nil {
		// 处理错误
	}

流程管理 流程设计器

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("未找到流程相关的信息")
)

定义错误

Functions

func CreateFlow added in v1.0.5

func CreateFlow(data []byte) (string, error)

func DeleteFlow added in v1.0.5

func DeleteFlow(recordID string) error

func FromExpContext

func FromExpContext(ctx context.Context) (expression.ExpContext, bool)

FromExpContext 获取表达式的上下文

func FromFlagContext

func FromFlagContext(ctx context.Context) (string, bool)

FromFlagContext 获取flag的上下文

func GetFlow added in v1.0.5

func GetFlow(recordID string) (*schema.Flow, error)

func GetFlowFormByFlowID added in v1.0.9

func GetFlowFormByFlowID(flowID string) (*schema.Form, error)

func GetNodeInstance

func GetNodeInstance(nodeInstanceID string) (*schema.NodeInstance, error)

GetNodeInstance 获取节点实例

func GetTodoByID added in v1.0.5

func GetTodoByID(nodeInstanceID string) (*schema.FlowTodoResult, error)

func Init

func Init(opts ...db.Option)

Init 初始化流程配置

func LoadFile

func LoadFile(name string) error

LoadFile 加载流程文件数据

func NewExpContext

func NewExpContext(ctx context.Context, exp expression.ExpContext) context.Context

NewExpContext 创建表达式的上下文值

func NewFlagContext

func NewFlagContext(ctx context.Context, flag string) context.Context

NewFlagContext 创建flag的上下文值

func QueryAllFlowPage added in v1.0.5

func QueryAllFlowPage(params schema.FlowQueryParam, pageIndex, pageSize uint) (int64, []*schema.FlowQueryResult, error)

func QueryDone added in v1.0.5

func QueryDone(typeCode, flowCode, userID string, lastTime int64, count int) ([]*schema.FlowDoneResult, error)

func QueryDoneByPage added in v1.0.9

func QueryDoneByPage(typeCode, flowCode, userID string, status, pageIndex, pageSize int) (int64, []*schema.FlowDoneResult, error)

func QueryDoneFlowIDs

func QueryDoneFlowIDs(flowCode, userID string) ([]string, error)

QueryDoneFlowIDs 查询已办理的流程实例ID列表

func QueryFlowHistory

func QueryFlowHistory(flowInstanceID string) ([]*schema.FlowHistoryResult, error)

QueryFlowHistory 查询流程历史数据 flowInstanceID 流程实例内码

func QueryGroupFlowPage added in v1.0.8

func QueryGroupFlowPage(params schema.FlowQueryParam, pageIndex, pageSize uint) (int64, []*schema.FlowQueryResult, error)

func QueryLastNodeInstance

func QueryLastNodeInstance(flowInstanceID string) (*schema.NodeInstance, error)

func QueryNodeCandidates

func QueryNodeCandidates(nodeInstanceID string) ([]string, error)

QueryNodeCandidates 查询节点实例的候选人ID列表

func QueryTodoFlows

func QueryTodoFlows(flowCode, userID string) ([]*schema.FlowTodoResult, error)

QueryTodoFlows 查询流程待办数据 flowCode 流程编号 userID 待办人

func QueryTodoFlowsPaginate

func QueryTodoFlowsPaginate(flowCode, userID string, page int, pageSize int) (int, []*schema.FlowTodoResult, error)

QueryTodoFlowsPaginate 分页查询流程待办数据 flowCode 流程编号 userID 待办人

func SetExecer

func SetExecer(execer Execer)

SetExecer 设定表达式执行器

func SetParser

func SetParser(parser Parser)

SetParser 设定解析器

func StartServer

func StartServer(opts ...ServerOption) http.Handler

StartServer 启动管理服务

func StopFlow

func StopFlow(nodeInstanceID string, allowStop func(*schema.FlowInstance) bool) error

StopFlow 停止流程

func StopFlowInstance

func StopFlowInstance(flowInstanceID string, allowStop func(*schema.FlowInstance) bool) error

StopFlowInstance 停止流程实例

Types

type API

type API struct {
	// contains filtered or unexported fields
}

API 提供API管理

func (*API) DeleteFlow

func (a *API) DeleteFlow(ctx *gear.Context) error

DeleteFlow 删除流程数据

func (*API) GetFlow

func (a *API) GetFlow(ctx *gear.Context) error

GetFlow 获取流程数据

func (*API) Init

func (a *API) Init(engine *Engine) *API

Init 初始化

func (*API) QueryFlowPage

func (a *API) QueryFlowPage(ctx *gear.Context) error

QueryFlowPage 查询流程分页数据

func (*API) SaveFlow

func (a *API) SaveFlow(ctx *gear.Context) error

SaveFlow 保存流程

type AutoCallbackHandler

type AutoCallbackHandler func(action, flag, userID string, input []byte, result *HandleResult) error

AutoCallbackHandler 自动执行节点回调处理

type EndHandle

type EndHandle func(*schema.FlowInstance)

EndHandle 定义流程结束处理函数

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine 流程引擎

func DefaultEngine

func DefaultEngine() *Engine

DefaultEngine 默认引擎

func (*Engine) CreateFlow

func (e *Engine) CreateFlow(data []byte) (string, error)

CreateFlow 创建流程数据

func (*Engine) DeleteFlow added in v1.0.5

func (e *Engine) DeleteFlow(recordID string) error

func (*Engine) FlowBll

func (e *Engine) FlowBll() *bll.Flow

FlowBll 流程业务

func (*Engine) GetExecer added in v1.0.1

func (e *Engine) GetExecer() Execer

func (*Engine) GetFlow added in v1.0.5

func (e *Engine) GetFlow(recordID string) (*schema.Flow, error)

func (*Engine) GetFlowFormByFlowID added in v1.0.9

func (e *Engine) GetFlowFormByFlowID(flowID string) (*schema.Form, error)

func (*Engine) GetNodeInstance

func (e *Engine) GetNodeInstance(nodeInstanceID string) (*schema.NodeInstance, error)

GetNodeInstance 获取节点实例

func (*Engine) GetTodoByID added in v1.0.5

func (e *Engine) GetTodoByID(nodeInstanceID string) (*schema.FlowTodoResult, error)

func (*Engine) HandleFlow

func (e *Engine) HandleFlow(ctx context.Context, nodeInstanceID, userID string, inputData []byte) (*HandleResult, error)

HandleFlow 处理流程节点 nodeInstanceID 节点实例内码 userID 处理人 inputData 输入数据

func (*Engine) Init

func (e *Engine) Init(parser Parser, execer Execer, sqlDB *sql.DB, trace bool) (*Engine, error)

Init 初始化流程引擎

func (*Engine) LaunchFlow

func (e *Engine) LaunchFlow(ctx context.Context, flowID, userID string, inputData []byte) (*HandleResult, error)

LaunchFlow 发起流程(基于流程ID)

func (*Engine) LoadFile

func (e *Engine) LoadFile(name string) error

LoadFile 加载文件数据

func (*Engine) QueryAllFlowPage added in v1.0.5

func (e *Engine) QueryAllFlowPage(params schema.FlowQueryParam, pageIndex, pageSize uint) (int64, []*schema.FlowQueryResult, error)

func (*Engine) QueryDone added in v1.0.5

func (e *Engine) QueryDone(typeCode, flowCode, userID string, lastTime int64, count int) ([]*schema.FlowDoneResult, error)

func (*Engine) QueryDoneByPage added in v1.0.9

func (e *Engine) QueryDoneByPage(typeCode, flowCode, userID string, status, pageIndex, pageSize int) (int64, []*schema.FlowDoneResult, error)

func (*Engine) QueryDoneFlowIDs

func (e *Engine) QueryDoneFlowIDs(flowCode, userID string) ([]string, error)

QueryDoneFlowIDs 查询已办理的流程实例ID列表

func (*Engine) QueryFlowHistory

func (e *Engine) QueryFlowHistory(flowInstanceID string) ([]*schema.FlowHistoryResult, error)

QueryFlowHistory 查询流程历史数据 flowInstanceID 流程实例内码

func (*Engine) QueryLastNodeInstance

func (e *Engine) QueryLastNodeInstance(flowInstanceID string) (*schema.NodeInstance, error)

func (*Engine) QueryNodeCandidates

func (e *Engine) QueryNodeCandidates(nodeInstanceID string) ([]string, error)

QueryNodeCandidates 查询节点实例的候选人ID列表

func (*Engine) QueryTodoFlows

func (e *Engine) QueryTodoFlows(flowCode, userID string) ([]*schema.FlowTodoResult, error)

QueryTodoFlows 查询流程待办数据 flowCode 流程编号 userID 待办人

func (*Engine) QueryTodoFlowsPaginate

func (e *Engine) QueryTodoFlowsPaginate(flowCode, userID string, page int, pageSize int) (int, []*schema.FlowTodoResult, error)

QueryTodoFlowsPaginate 分页查询流程待办数据 flowCode 流程编号 userID 待办人

func (*Engine) SetAutoCallback

func (e *Engine) SetAutoCallback(callback AutoCallbackHandler)

SetAutoCallback 设定自动节点回调函数

func (*Engine) SetExecer

func (e *Engine) SetExecer(execer Execer)

SetExecer 设定表达式执行器

func (*Engine) SetGetDBContext

func (e *Engine) SetGetDBContext(fn func(flag string) context.Context)

SetGetDBContext 设定获取DB上下文

func (*Engine) SetLogger

func (e *Engine) SetLogger(logger Logger)

SetLogger 设定日志接口

func (*Engine) SetParser

func (e *Engine) SetParser(parser Parser)

SetParser 设定解析器

func (*Engine) StartFlow

func (e *Engine) StartFlow(ctx context.Context, flowCode, userID string, inputData []byte) (*HandleResult, error)

StartFlow 启动流程 flowCode 流程编号 userID 发起人 inputData 输入数据

func (*Engine) StartTiming

func (e *Engine) StartTiming(interval time.Duration)

StartTiming 启动定时器

func (*Engine) StopFlow

func (e *Engine) StopFlow(nodeInstanceID string, allowStop func(*schema.FlowInstance) bool) error

StopFlow 停止流程

func (*Engine) StopFlowInstance

func (e *Engine) StopFlowInstance(flowInstanceID string, allowStop func(*schema.FlowInstance) bool) error

StopFlowInstance 停止流程实例

func (*Engine) StopTiming

func (e *Engine) StopTiming()

StopTiming 停止定时器

type Execer

type Execer interface {
	// 执行表达式返回布尔类型的值
	ExecReturnBool(ctx context.Context, exp, params []byte) (bool, error)
	ExecReturnMap(ctx context.Context, exp, params []byte) (map[string]interface{}, error)

	// 执行表达式返回字符串切片类型的值
	ExecReturnStringSlice(ctx context.Context, exp, params []byte) ([]string, error)
}

Execer 表达式执行器

func NewQLangExecer

func NewQLangExecer() Execer

NewQLangExecer 创建基于qlang的表达式执行器

type FieldOption

type FieldOption struct {
	ID   string // 选项值ID
	Name string // 选项值名称
}

FieldOption 枚举选项

type FieldProperty

type FieldProperty struct {
	ID    string // 属性ID
	Value string // 属性值
}

FieldProperty 字段属性

type FieldValidation

type FieldValidation struct {
	Name   string // 约束名
	Config string // 约束配置
}

FieldValidation 字段验证

type FormFieldResult

type FormFieldResult struct {
	ID           string             // 字段ID
	Type         string             // 字段类型
	Label        string             // 字段标签
	DefaultValue string             // 默认值
	Values       []*FieldOption     // 枚举类型
	Validations  []*FieldValidation // 字段验证
	Properties   []*FieldProperty   // 字段属性
}

FormFieldResult 表单字段

type HandleResult

type HandleResult struct {
	IsEnd        bool                 `json:"is_end"`        // 是否结束
	NextNodes    []*NextNode          `json:"next_nodes"`    // 下一处理节点
	FlowInstance *schema.FlowInstance `json:"flow_instance"` // 流程实例
}

HandleResult 处理结果

func HandleFlow

func HandleFlow(nodeInstanceID, userID string, input interface{}) (*HandleResult, error)

HandleFlow 处理流程节点 nodeInstanceID 节点实例内码 userID 处理人 input 输入数据

func HandleFlowWithContext

func HandleFlowWithContext(ctx context.Context, nodeInstanceID, userID string, input interface{}) (*HandleResult, error)

HandleFlowWithContext 处理流程节点 nodeInstanceID 节点实例内码 userID 处理人 input 输入数据

func LaunchFlow added in v1.0.8

func LaunchFlow(flowID, userID string, inputData []byte) (*HandleResult, error)

func StartFlow

func StartFlow(flowCode, userID string, input interface{}) (*HandleResult, error)

StartFlow 启动流程 flowCode 流程编号 userID 发起人 input 输入数据

func StartFlowWithContext

func StartFlowWithContext(ctx context.Context, flowCode, userID string, input interface{}) (*HandleResult, error)

StartFlowWithContext 启动流程 flowCode 流程编号 nodeCode 开始节点编号 userID 发起人 input 输入数据

func (*HandleResult) String

func (r *HandleResult) String() string

type Logger

type Logger interface {
	Errorf(format string, args ...interface{})
}

Logger 定义日志接口

type NextNode

type NextNode struct {
	Node         *schema.Node         // 节点信息
	CandidateIDs []string             // 节点候选人
	NodeInstance *schema.NodeInstance // 节点实例
}

NextNode 下一节点

type NextNodeHandle

type NextNodeHandle func(*schema.Node, *schema.NodeInstance, []*schema.NodeCandidate)

NextNodeHandle 定义下一节点处理函数

type NodeFormResult

type NodeFormResult struct {
	ID     string             // 表单ID
	Fields []*FormFieldResult // 表单字段
}

NodeFormResult 节点表单

type NodeResult

type NodeResult struct {
	NodeID               string            // 节点ID
	NodeName             string            // 节点名称
	NodeType             NodeType          // 节点类型
	Content              string            // 节点执行脚本内容
	Routers              []*RouterResult   // 节点路由
	Properties           []*PropertyResult // 节点属性
	CandidateExpressions []string          // 候选人表达式
	FormResult           *NodeFormResult   // 节点表单
}

NodeResult 节点数据

type NodeRouter

type NodeRouter struct {
	// contains filtered or unexported fields
}

NodeRouter 节点路由

func (*NodeRouter) GetFlowInstance

func (n *NodeRouter) GetFlowInstance() *schema.FlowInstance

GetFlowInstance 获取流程实例

func (*NodeRouter) Init

func (n *NodeRouter) Init(ctx context.Context, engine *Engine, nodeInstanceID string, inputData []byte, options ...NodeRouterOption) (*NodeRouter, error)

Init 初始化节点路由

func (*NodeRouter) Next

func (n *NodeRouter) Next(processor string) error

Next 流向下一节点

type NodeRouterOption

type NodeRouterOption func(*nodeRouterOptions)

NodeRouterOption 节点路由配置

func AutoStartOption

func AutoStartOption(autoStart bool) NodeRouterOption

AutoStartOption 自动开始流程配置

func OnFlowEndOption

func OnFlowEndOption(fn EndHandle) NodeRouterOption

OnFlowEndOption 注册流程结束事件

func OnNextNodeOption

func OnNextNodeOption(fn NextNodeHandle) NodeRouterOption

OnNextNodeOption 注册下一节点处理事件配置

type NodeType

type NodeType string

NodeType 节点类型

const (
	// StartEvent 开始事件
	StartEvent NodeType = "startEvent"
	// EndEvent 结束事件
	EndEvent NodeType = "endEvent"
	// TerminateEvent 终止事件
	TerminateEvent NodeType = "terminateEvent"
	// UserTask 人工任务
	UserTask   NodeType = "userTask"
	ScriptTask NodeType = "scriptTask"
	// ExclusiveGateway 排他网关
	ExclusiveGateway NodeType = "exclusiveGateway"
	// ParallelGateway 并行网关
	ParallelGateway NodeType = "parallelGateway"
	// Unknown 未知类型
	Unknown NodeType = "Unknown"
)

func GetNodeTypeByName

func GetNodeTypeByName(s string) (NodeType, error)

GetNodeTypeByName 转换节点类型

func (NodeType) String

func (n NodeType) String() string

type ParseResult

type ParseResult struct {
	FlowID      string        // 流程ID
	FlowName    string        // 流程名称
	FlowMemo    string        // 流程笔记
	FlowVersion int64         // 流程版本号
	FlowStatus  int           // 流程状态(1:可用 2:不可用)
	IsAutoStart bool          // 是否自动启动
	Nodes       []*NodeResult // 节点数据
}

ParseResult 流程数据

type Parser

type Parser interface {
	// 解析流程定义数据
	Parse(ctx context.Context, data []byte) (*ParseResult, error)
}

Parser 流程数据解析器

func NewXMLParser

func NewXMLParser() Parser

NewXMLParser xml解析器

type PropertyResult

type PropertyResult struct {
	Name  string // 属性名称
	Value string // 属性值
}

PropertyResult 节点属性

type Renderer

type Renderer interface {
	Render(context.Context, *NodeFormResult) ([]byte, error)
}

Renderer 表单渲染器

func NewIonicRenderer

func NewIonicRenderer() Renderer

NewIonicRenderer 渲染到ionic

type RouterResult

type RouterResult struct {
	TargetNodeID string // 目标节点ID
	Explain      string // 说明
	Expression   string // 条件表达式
}

RouterResult 节点路由数据

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 流程管理服务

func (*Server) Init

func (a *Server) Init(engine *Engine, opts ...ServerOption) *Server

Init 初始化

func (*Server) ServeHTTP

func (a *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ServerOption

type ServerOption func(*serverOptions)

ServerOption 流程服务配置

func ServerMiddlewareOption

func ServerMiddlewareOption(middlewares ...gear.Middleware) ServerOption

ServerMiddlewareOption 中间件

func ServerPrefixOption

func ServerPrefixOption(prefix string) ServerOption

ServerPrefixOption 访问前缀

func ServerStaticRootOption

func ServerStaticRootOption(staticRoot string) ServerOption

ServerStaticRootOption 静态文件目录

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL