gworkflow

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 14 Imported by: 0

README

gworkflow

使用golang实现的简易的工作流引擎,只用于保存流程信息

Documentation

Index

Constants

View Source
const (
	PRE_EVENT = iota + 1
	AFTER_EVENT
	APPLY_EVENT
)

Event type表示一个动作类别 它包含三个类别,节点触发前,节点触发后,节点实际执行后

View Source
const (
	BeginNode = iota + 1
	NormalNode
	SplitNode
	BranchNode
	InterNode
	EndNode
)

node type 是关于节点类型的描述 通常一个节点可以被表示为以下类型: 开始节点,普通节点,分流节点,分支节点,合流节点,结束节点

Variables

View Source
var CAN_NOT_DELETE = errors.New("含有关联数据,无法删除")
View Source
var DELETE_FAIL = errors.New("删除失败")
View Source
var INSERT_FAIL = errors.New("插入失败")
View Source
var PARAM_NULL = errors.New("参数为空")
View Source
var SELECT_NULL = errors.New("查询失败")
View Source
var TRANSACTION_BEGIN_ERR = errors.New("事务开启失败")
View Source
var UPDATE_FAIL = errors.New("更新失败")

Functions

func Init

func Init(writer io.Writer, traceid string, machineId func() (uint16, error))

初始化工作流引擎 默认设置数据库为mysql,不可更改 日志使用zerolog包,默认日志级别为debug @param writer 日志输出位置,默认为标准错误 os.stderr @param traceid 链路跟踪使用,可以为空 @param machineId sonyflake机器ID func()(uint16,error))

func Transaction

func Transaction(f func(*xorm.Session) (interface{}, error)) (i interface{}, err error)

xorm事务控制

Types

type Engine

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

工作流引擎 它并不存储任何信息,但是所有对流程的操作都通过它实现

func New

func New() *Engine

func (Engine) AddEvent

func (e Engine) AddEvent(name string, eventType int) (*Event, error)

添加事件

func (Engine) AddGroup

func (e Engine) AddGroup(admin uint64, name string) (*Group, error)

添加分组

func (Engine) AddGroupUser

func (e Engine) AddGroupUser(gid, uid uint64) error

分组添加成员

func (Engine) AddKind

func (e Engine) AddKind(name string) (*Kind, error)

增加模块

func (Engine) AddMessage

func (e Engine) AddMessage(nid uint64, subject, content string) error

添加消息提醒内容

func (Engine) AddNode

func (e Engine) AddNode(pid uint64, name string, nodeType int, eids []uint64) (*Node, error)

添加节点

func (Engine) AddNodeEvent

func (e Engine) AddNodeEvent(nid, eid uint64) error

添加节点事件

func (Engine) AddProcess

func (e Engine) AddProcess(kindId uint64, name string) (*Process, error)

添加流程,默认启用 kindId 为0时,表示不属于任何模块

func (Engine) AddTemplate

func (e Engine) AddTemplate(pid uint64, content []byte) (*Template, error)

新增模板

func (Engine) AddUser

func (e Engine) AddUser(name string, email string) (*User, error)

新增用户

func (Engine) ApplyNode

func (e Engine) ApplyNode(recordId uint64) error

执行节点(将执行节点执行后动作) 开始和结束节点无法调用此方法

func (Engine) ChangeEmail

func (e Engine) ChangeEmail(id uint64, email string) error

更换邮箱

func (Engine) ChangeNodeUser

func (e Engine) ChangeNodeUser(recordId, uid uint64) error

更换节点执行人 开始节点无法更换执行人

func (Engine) DelKind

func (e Engine) DelKind(id uint64) error

删除模块 若模块下含有流程,此删除方法将不执行

func (Engine) DelKindSafe

func (e Engine) DelKindSafe(id uint64) error

删除模块 若模块下含有流程,此方法将把所有流程对应模块置空

func (Engine) DelKindUnSafe

func (e Engine) DelKindUnSafe(id uint64) error

删除模块 若模块下含有流程,此方法将把模块下流程全部删除

func (Engine) DelNode

func (e Engine) DelNode(id uint64) error

删除节点

func (Engine) DeleteProcess

func (e Engine) DeleteProcess(id uint64) error

删除流程

func (Engine) DisableMessage

func (e Engine) DisableMessage(nid uint64) error

禁用消息提醒

func (Engine) DisableProcess

func (e Engine) DisableProcess(id uint64) error

禁用流程

func (Engine) EnableMessage

func (e Engine) EnableMessage(nid uint64) error

启用消息提醒

func (Engine) EnableProcess

func (e Engine) EnableProcess(id uint64) error

启用流程

func (Engine) GetAllProcess

func (e Engine) GetAllProcess() []Process

查询全部流程

func (Engine) GetKind

func (e Engine) GetKind(id uint64, name string) (*Kind, error)

查询指定模块 按id或名称精确查询 当参数为空时,默认查询第一条

func (Engine) GetNodeMessage

func (e Engine) GetNodeMessage(nid uint64) (*Message, error)

获取节点消息提醒内容

func (Engine) GetProcess

func (e Engine) GetProcess(id uint64, name string) (*Process, error)

通过id或者name查询指定流程

func (Engine) GetTemplate

func (e Engine) GetTemplate(pid uint64) (*Template, error)

查询流程启用模板

func (Engine) ListEvent

func (e Engine) ListEvent() ([]Event, error)

查询事件列表

func (Engine) ListGroup

func (e Engine) ListGroup(id uint64, name string) ([]Group, error)

查询分组 按id精确查询 或 按名称模糊查询 查询条件为空时查询全部

func (Engine) ListGroupUser

func (e Engine) ListGroupUser(gid, uid uint64, username string) ([]GroupUser, error)

查询分组中的成员 可以按uid精确查询,或使用成员名称模糊查询 查询条件为空时查询全部

func (Engine) ListKind

func (e Engine) ListKind() ([]Kind, error)

获取所有模块

func (Engine) ListKindProcess

func (e Engine) ListKindProcess(kid uint64) ([]Process, error)

查询指定模块下流程

func (Engine) ListRecord

func (e Engine) ListRecord(uid, nid uint64, actuallyNo string) ([]Record, error)

查询节点执行记录 以uid,nid精确查询,以actuallyNo模糊查询

func (*Engine) RegisterEvent

func (e *Engine) RegisterEvent(events []Event)

注册事件对应func

func (*Engine) RegisterMailBox

func (e *Engine) RegisterMailBox(mailBox MailBox)

注册邮件服务器

func (Engine) RemoveEvent

func (e Engine) RemoveEvent(id uint64) error

移除事件

func (Engine) RemoveGroup

func (e Engine) RemoveGroup(id uint64) error

解散分组

func (Engine) RemoveGroupUser

func (e Engine) RemoveGroupUser(gid, uid uint64) error

分组移除成员

func (Engine) RemoveNodeEvent

func (e Engine) RemoveNodeEvent(nid, eid uint64) error

移除节点事件

func (Engine) RenameKind

func (e Engine) RenameKind(id uint64, name string) error

模块重命名

func (Engine) RenameNode

func (e Engine) RenameNode(id uint64, name string) error

节点重命名

func (Engine) RenameProcess

func (e Engine) RenameProcess(id uint64, name string) error

流程重命名

func (Engine) RenameUser

func (e Engine) RenameUser(id uint64, name string) error

用户重命名

func (Engine) TouchNode

func (e Engine) TouchNode(uid, nid uint64, actuallyNo string) error

触发节点(这里将执行节点触发前和触发后的动作) 开始节点不执行触发动作,仅执行执行后动作,且开始节点执行状态默认true 结束节点不知晓执行后动作,仅执行触发动作,且结束节点执行状态默认true 应用并不会处理节点的条件判断,以及前置节点类型判断,使用者需要自行处理 uid表示节点的执行人 这里使用actuallyNo表示实际业务中的编号,它将被保存在record记录中

type Event

type Event struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Name      string    `xorm:"varchar(36) not null unique index comment('名称')" json:"name"`
	EventType int       `xorm:"TINYINT(1) not null comment('事件类别')" json:"eventType"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
	Func      EventFunc `xorm:"-"`
}

Event 用于描述工作流中节点的相关动作

type EventFunc

type EventFunc func() error

type Group

type Group struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Name      string    `xorm:"varchar(36) not null unique index comment('组别名称')" json:"name"`
	Active    bool      `xorm:"not null default 1 comment('激活状态:0-未激活,1-激活')" json:"active"`
	Admin     uint64    `xorm:"not null comment('管理员')"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
}

group是对用户的简单分组,它可以被描述为一个组织,也可以描述成一个团体 需要注意的是,它只是一个简单的分组,并不能够用来描述组与组之间的关系 同样的,一个用户也可以同时属于多个组别

type GroupUser

type GroupUser struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Gid       uint64    `xorm:"not null comment('分组ID')" json:"gid"`
	Uid       uint64    `xorm:"not null comment('用户ID')" json:"uid"`
	Username  string    `xorm:"varchar(36) not null index comment('用户名称')" json:"username"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
}

type Kind

type Kind struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Name      string    `xorm:"varchar(36) index not null unique comment('模块名称')" json:"name"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
}

kind 用于给流程区分模块

func (*Kind) BeforeInsert

func (m *Kind) BeforeInsert()

type MailBox

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

type Message

type Message struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Nid       uint64    `xorm:"unique comment('节点ID')" json:"nid"`
	Subject   string    `xorm:"varchar(100) not null comment('主题')" json:"subject"`
	Content   string    `xorm:"varchar(500) comment('提醒内容')" json:"content"`
	Active    bool      `xorm:"TINYINT(1) default 1 comment('启用状态')" json:"active"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
}

message是节点被触发时的提醒设置 框架默认实现了邮件提醒的方式,当然使用者可以通过event扩展注册另外的提醒方式

type Node

type Node struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Pid       uint64    `xorm:"index comment('节点所属流程ID')" json:"pid"`
	Name      string    `xorm:"varchar(36) not null unique index comment('节点名称')" json:"name"`
	NodeType  int       `xorm:"TINYINT(1) not null comment('节点类型')" json:"nodeType"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
}

node用于描述一个工作流的节点信息 它包括一个工作流节点的基本信息,以及相应的动作 框架并不提供节点的信息查询,事实上,节点的信息应该被保存在模板中,使用者应该解析自定义的模板并从中获取节点条件和节点信息

type NodeEvent

type NodeEvent struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Nid       uint64    `xorm:"not null comment('节点ID')" json:"nid"`
	Eid       uint64    `xorm:"not null comment('事件ID')" json:"eid"`
	EventType int       `xorm:"TINYINT(1) not null comment('事件类别')" json:"eventType"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
}

节点启用动作

type Process

type Process struct {
	Id        uint64    `xorm:"pk" json:"id"`
	KindId    uint64    `xorm:"index comment('流程所属模块')" json:"kindId"`
	Name      string    `xorm:"varchar(36) not null unique index comment('流程名称')" json:"name"`
	Active    bool      `xorm:"not null default 1 comment('激活状态:0-未激活,1-激活')" json:"active"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
	DeletedAt time.Time `xorm:"deleted comment('删除时间')" json:"deletedAt"`
}

流程信息应该被描述为单个的文档 默认情况下,流程的结构被定义为一个图,依赖于他的节点信息

func (*Process) BeforeInsert

func (m *Process) BeforeInsert()

type Record

type Record struct {
	Id         uint64    `xorm:"pk" json:"id"`
	Nid        uint64    `xorm:"not null comment('节点ID')" json:"nid"`
	Uid        uint64    `xorm:"not null comment('节点处理用户ID')" json:"nid"`
	PreUid     uint64    `xorm:"comment('节点原所属用户ID')" json:"preUid"`
	ActuallyNo string    `xorm:"varchar(50) not null comment('流程实际执行编号')" json:"actuallyNo"`
	Status     bool      `xorm:"TINYINT(1) not null default 0 comment('执行状态:0-未执行,1已执行')"`
	CreatedAt  time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt  time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
}

工作流实际执行节点记录

type Template

type Template struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Pid       uint64    `xorm:"index not null unique('version') comment('流程ID')" json:"pid"`
	Version   int       `xorm:"default 1 not null unique('version') comment('版本号')" json:"version"`
	Content   []byte    `xorm:"not null comment('模板内容')" json:"content"`
	Active    bool      `xorm:"not null default 1 comment('激活状态:0-未激活,1-激活')" json:"active"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
	DeletedAt time.Time `xorm:"deleted comment('删除时间')" json:"deletedAt"`
}

流程模板被用来表示工作流的内容 框架并不关心模板在实际使用时的数据类型和节点内容 它的拓扑结构必须是完整的图,包含节点的信息和节点走向以及判断条件 它还可以定义针对节点的一系列动作

func (*Template) BeforeInsert

func (m *Template) BeforeInsert()

type User

type User struct {
	Id        uint64    `xorm:"pk" json:"id"`
	Name      string    `xorm:"varchar(36) not null index comment('用户名称')" json:"name"`
	Email     string    `xorm:"varchar(50) not null unique comment('邮箱')" json:"email"`
	CreatedAt time.Time `xorm:"created comment('创建时间')" json:"createdAt"`
	UpdatedAt time.Time `xorm:"updated comment('更新时间')" json:"updatedAt"`
	DeletedAt time.Time `xorm:"deleted comment('删除时间')" json:"deletedAt"`
}

user是工作流中用于表示实际流程流转的具体用户 它并不用于存储一个用户的详细信息 使用时每个用户必须拥有一个唯一标识,在这里我们使用邮箱作为条件

Jump to

Keyboard shortcuts

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