1 Star 0 Fork 0

玄学编程/task-flow
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
workflow.go 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
Bilibotter 提交于 2023-09-11 18:04 +08:00 . optimize finish judge
package task
import (
"sync"
)
type Workflow struct {
procedureMap map[string]*Procedure
context *Context
features map[string]*Feature
pause sync.WaitGroup
finish sync.WaitGroup
lock sync.Mutex
}
func NewWorkflow(input map[string]any) *Workflow {
context := Context{
scope: ProcedureCtx,
scopeContexts: make(map[string]*Context),
table: sync.Map{},
priority: make(map[string]any),
}
for k, v := range input {
context.table.Store(k, v)
}
context.scopeContexts[ProcedureCtx] = &context
flow := Workflow{
lock: sync.Mutex{},
context: &context,
procedureMap: make(map[string]*Procedure),
pause: sync.WaitGroup{},
finish: sync.WaitGroup{},
}
return &flow
}
func (wf *Workflow) WaitToDone() map[string]*Feature {
features := wf.AsyncFlow()
for _, feature := range features {
feature.WaitToDone()
}
return features
}
func (wf *Workflow) AsyncFlow() map[string]*Feature {
if wf.features != nil {
return wf.features
}
wf.lock.Lock()
defer wf.lock.Unlock()
// DCL
if wf.features != nil {
return wf.features
}
features := make(map[string]*Feature, len(wf.procedureMap))
for name, procedure := range wf.procedureMap {
features[name] = procedure.run()
}
wf.features = features
return features
}
func (wf *Workflow) AddProcedure(name string, conf *ProduceConfig) *Procedure {
procedureCtx := Context{
scope: ProcedureCtx,
scopeContexts: make(map[string]*Context),
table: sync.Map{},
}
procedureCtx.scopeContexts[ProcedureCtx] = &procedureCtx
procedure := Procedure{
name: name,
stepMap: make(map[string]*Step),
procedureContexts: procedureCtx.scopeContexts,
context: &procedureCtx,
pause: sync.WaitGroup{},
running: sync.WaitGroup{},
conf: conf,
}
wf.procedureMap[procedure.name] = &procedure
wf.finish.Add(1)
procedure.context.parents = append(procedure.context.parents, wf.context)
return &procedure
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/MetaphysicCoding/task-flow.git
git@gitee.com:MetaphysicCoding/task-flow.git
MetaphysicCoding
task-flow
task-flow
v1.2.0

搜索帮助