-
Notifications
You must be signed in to change notification settings - Fork 5
/
process_service.go
164 lines (139 loc) · 3.84 KB
/
process_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package goflow
import "time"
//流程服务
type ProcessService struct {
ProcessCache map[string]*Process //Process缓存,KEY为[名称.版本]
NameCache map[string]string //ProcessName缓存,KEY为ID
InnerInterceptorCache map[string]IInterceptor //内置拦截器
CustomInterceptorCache map[string]IInterceptor //自定义拦截器
}
//初始化服务对象和加入内置拦截器
func (p *ProcessService) InitProcessService() {
p.ProcessCache = make(map[string]*Process)
p.NameCache = make(map[string]string)
p.InnerInterceptorCache = make(map[string]IInterceptor)
p.CustomInterceptorCache = make(map[string]IInterceptor)
p.SetInnerInterceptor(&SurrogateInterceptor{})
}
//使用自定义拦截器缓存
func (p *ProcessService) SetCustomInterceptor(interceptor IInterceptor) {
p.CustomInterceptorCache[interceptor.GetName()] = interceptor
}
//获取自定义缓存拦截器
func (p *ProcessService) GetCustomInterceptor(name string) IInterceptor {
if interceptor, ok := p.CustomInterceptorCache[name]; ok {
return interceptor
}
return nil
}
//使用拦截器缓存
func (p *ProcessService) SetInnerInterceptor(interceptor IInterceptor) {
p.InnerInterceptorCache[interceptor.GetName()] = interceptor
}
//获取缓存拦截器
func (p *ProcessService) GetInnerInterceptor(name string) IInterceptor {
if interceptor, ok := p.InnerInterceptorCache[name]; ok {
return interceptor
}
return nil
}
//缓存Process
func (p *ProcessService) Cache(process *Process) {
processName := process.Name + DEFAULT_SEPARATOR + IntToStr(process.Version)
delete(p.ProcessCache, processName)
if process.Model == nil {
processModel := &ProcessModel{}
processModel.BuildRelationship([]byte(process.Content), p)
process.SetModel(processModel)
}
processName = process.Name + DEFAULT_SEPARATOR + IntToStr(process.Version)
p.ProcessCache[processName] = process
p.NameCache[process.Id] = processName
}
//部署Process
func (p *ProcessService) Deploy(input []byte, creator string) string {
processModel := &ProcessModel{}
processModel.BuildRelationship(input, p)
ver := -1
oldProcess := GetLatestProcess(processModel.Name)
if oldProcess != nil {
ver = oldProcess.Version
}
process := &Process{
Id: NewUUID(),
State: FS_ACTIVITY,
Content: string(input),
Creator: creator,
CreateTime: time.Now(),
Version: ver + 1,
}
process.SetModel(processModel)
p.Cache(process)
Save(process, process.Id)
return process.Id
}
//重新部署Process
func (p *ProcessService) ReDeploy(id string, input []byte) {
process := &Process{}
success := process.GetProcessById(id)
if success {
process.Content = string(input)
p.Cache(process)
Update(process, process.Id)
}
}
//卸载部署
func (p *ProcessService) UnDeploy(id string) {
process := &Process{}
success := process.GetProcessById(id)
if success {
process.State = FS_FINISH
p.Cache(process)
Update(process, process.Id)
}
}
//根据ID得到Process
func (p *ProcessService) GetProcessById(id string) *Process {
processName := p.NameCache[id]
process := p.ProcessCache[processName]
if process == nil {
process = &Process{}
if process.GetProcessById(id) {
p.Cache(process)
return process
} else {
return nil
}
} else {
return process
}
}
//根据名称、版本得到Process
func (p *ProcessService) GetProcessByVersion(name string, version int) *Process {
ver := version
if ver == -1 {
dbProcess := GetLatestProcess(name)
if dbProcess == nil {
return nil
} else {
ver = dbProcess.Version
}
}
processName := name + DEFAULT_SEPARATOR + IntToStr(ver)
process := p.ProcessCache[processName]
if process == nil {
process = &Process{
Name: name,
Version: ver,
}
success := process.GetProcess()
if success {
p.Cache(process)
return process
} else {
return nil
}
} else {
return process
}
}