-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_service.go
205 lines (170 loc) · 4.52 KB
/
task_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package sm
import (
"strings"
log "github.com/sirupsen/logrus"
)
type task_service struct {
repository TaskRepository
job_repository JobRepository
}
func NewTaskService(
repository TaskRepository, job_repository JobRepository) TaskService {
return &task_service{
repository: repository,
job_repository: job_repository,
}
}
func (this *task_service) RegisterTask(
module_id int64,
name, description, start_url, cancel_url string,
input, output map[string]ParamType) (*Task, error) {
if len(name) <= 1 {
return nil, ErrTaskNameTooShort
} else if len(description) <= 1 {
return nil, ErrTaskDescTooShort
} else if !(strings.HasPrefix(start_url, "http://") ||
strings.HasPrefix(start_url, "https://")) {
return nil, ErrInvalidStartUrl
} else if !(strings.HasPrefix(cancel_url, "http://") ||
strings.HasPrefix(cancel_url, "https://") ||
strings.HasPrefix(cancel_url, "REST http://") ||
strings.HasPrefix(cancel_url, "REST https://")) {
return nil, ErrInvalidCancelUrl
} else if len(input) == 0 {
return nil, ErrEmptyInput
} else if len(output) == 0 {
return nil, ErrEmptyOutput
}
exists, err := this.repository.NameExists(name)
if err != nil {
return nil, err
} else if exists {
return nil, ErrTaskAlreadyExists
}
task := Task{
ModuleID: module_id,
Name: name,
Description: description,
StartUrl: start_url,
CancelUrl: cancel_url,
Input: make(map[string]ParamType),
Output: make(map[string]ParamType),
}
for name, value := range input {
if value != StringParam &&
value != IntParam &&
value != DoubleParam {
return nil, ErrInvalidInputType
}
task.Input[name] = value
}
for name, value := range output {
if value != StringParam &&
value != IntParam &&
value != DoubleParam {
return nil, ErrInvalidOutputType
}
task.Output[name] = value
}
log.Infof("register task name=%v", task.Name)
err = this.repository.CreateTask(&task)
if err != nil {
return nil, err
}
log.Infof("task name=%v id=%v registed", task.Name, task.ID)
return &task, nil
}
func (this *task_service) SetAvailability(id int64, enabled bool) error {
task, err := this.repository.GetTask(id)
if err != nil {
return err
} else if task == nil || task.Deleted {
return ErrNotFound
}
return this.repository.SetAvailability(id, enabled)
}
func (this *task_service) DeleteTask(id int64) error {
task, err := this.repository.GetTask(id)
if err != nil {
return err
} else if task == nil || task.Deleted {
return ErrNotFound
} else if task.Enabled {
return ErrTaskIsEnabled
}
has_jobs, err := this.job_repository.TaskHasActiveJobs(task.ID)
if err != nil {
return err
} else if has_jobs {
return ErrTaskHasJobStepsInProgress
}
return this.repository.DeleteTask(task.ID)
}
func (this *task_service) Update(id int64, fields map[string]string) error {
task, err := this.repository.GetTask(id)
if err != nil {
return err
} else if task == nil || task.Deleted {
return ErrNotFound
}
is_active, err := this.job_repository.TaskHasActiveJobs(task.ID)
if err != nil {
return err
}
for name, value := range fields {
if name == "Name" {
task.Name = value
} else if name == "Description" {
task.Description = value
} else if task.Enabled {
// For the rest of the field only allow updates if the task is disabled
return ErrTaskIsEnabled
} else if is_active {
// For the rest of the fields only allows update if the task is not currently used.
return ErrTaskHasJobStepsInProgress
} else if name == "StartUrl" {
task.StartUrl = value
} else if name == "CancelUrl" {
task.CancelUrl = value
}
}
return this.repository.Save(task)
}
func (this *task_service) UpdateVars(id int64, data map[string]ParamType, is_input bool) error {
task, err := this.repository.GetTask(id)
if err != nil {
return nil
} else if task == nil || task.Deleted {
return ErrNotFound
} else if task.Enabled {
return ErrTaskIsEnabled
}
is_active, err := this.job_repository.TaskHasActiveJobs(task.ID)
if err != nil {
return err
} else if is_active {
return ErrTaskHasJobStepsInProgress
}
if is_input {
task.Input = make(map[string]ParamType)
} else {
task.Output = make(map[string]ParamType)
}
for name, value := range data {
if value != StringParam &&
value != IntParam &&
value != DoubleParam {
if is_input {
return ErrInvalidInputType
} else {
return ErrInvalidOutputType
}
}
if is_input {
task.Input[name] = value
} else {
task.Output[name] = value
}
}
return this.repository.SaveVars(task, is_input)
}