/
task.go
191 lines (159 loc) · 6.89 KB
/
task.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
/*
Copyright 2021 The KodeRover Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package service
import (
"encoding/json"
"fmt"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/koderover/zadig/pkg/microservice/aslan/config"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/models"
commonmodels "github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/models/task"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/repository/mongodb"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/nsq"
"github.com/koderover/zadig/pkg/microservice/aslan/core/common/service/scmnotify"
"github.com/koderover/zadig/pkg/setting"
e "github.com/koderover/zadig/pkg/tool/errors"
)
type cancelMessage struct {
Revoker string `json:"revoker"`
PipelineName string `json:"pipeline_name"`
TaskID int64 `json:"task_id"`
ReqID string `json:"req_id"`
}
func CancelWorkflowTaskV3(userName, pipelineName string, taskID int64, typeString config.PipelineType, requestID string, log *zap.SugaredLogger) error {
if err := CancelTask(userName, pipelineName, taskID, typeString, requestID, log); err != nil {
log.Errorf("[%s] cancel pipeline [%s:%d] error: %s", userName, pipelineName, taskID, err)
return e.ErrCancelTask.AddDesc(err.Error())
}
return nil
}
func CancelTaskV2(userName, pipelineName string, taskID int64, typeString config.PipelineType, requestID string, log *zap.SugaredLogger) error {
if err := CancelTask(userName, pipelineName, taskID, typeString, requestID, log); err != nil {
log.Errorf("[%s] cancel pipeline [%s:%d] error: %v", userName, pipelineName, taskID, err)
return e.ErrCancelTask.AddDesc(err.Error())
}
return nil
}
func CancelTask(userName, pipelineName string, taskID int64, typeString config.PipelineType, reqID string, log *zap.SugaredLogger) error {
scmNotifyService := scmnotify.NewService()
for _, t := range List(log) {
if t.PipelineName == pipelineName &&
t.TaskID == taskID &&
t.Type == typeString &&
(t.Status == config.StatusWaiting || t.Status == config.StatusBlocked || t.Status == config.StatusCreated) {
Remove(t, log)
err := mongodb.NewTaskColl().UpdateStatus(taskID, pipelineName, userName, config.StatusCancelled)
if err != nil {
log.Errorf("PipelineTaskV2.UpdateStatus [%s:%d] error: %v", pipelineName, taskID, err)
return err
}
// 获取更新后的任务数据并发送通知
t, err := mongodb.NewTaskColl().Find(taskID, pipelineName, typeString)
if err != nil {
log.Errorf("[%s] task: %s:%d not found", userName, pipelineName, taskID)
return err
}
if typeString == config.WorkflowType {
_ = scmNotifyService.UpdateWebhookComment(t, log)
} else if typeString == config.TestType {
_ = scmNotifyService.UpdateWebhookCommentForTest(t, log)
} else if typeString == config.SingleType {
_ = scmNotifyService.UpdatePipelineWebhookComment(t, log)
} else if typeString == config.ScanningType {
err = scmNotifyService.UpdateWebhookCommentForScanning(t, log)
// this logic will not affect the result of cancellation, so we only print the error in logs.
if err != nil {
log.Errorf("Failed to update webhook comment for scanning in upon cancel, the error is: %s", err)
}
}
return nil
}
}
b, err := json.Marshal(cancelMessage{Revoker: userName, PipelineName: pipelineName, TaskID: taskID, ReqID: reqID})
if err != nil {
log.Errorf("marshal cancel message error: %v", err)
return err
}
t, err := mongodb.NewTaskColl().Find(taskID, pipelineName, typeString)
if err != nil {
log.Errorf("[%s] task: %s:%d not found", userName, pipelineName, taskID)
return err
}
if t.Status == config.StatusPassed {
log.Errorf("[%s] task: %s:%d is passed, cannot cancel", userName, pipelineName, taskID)
return fmt.Errorf("task: %s:%d is passed, cannot cancel", pipelineName, taskID)
}
t.Status = config.StatusCancelled
t.TaskRevoker = userName
log.Infof("[%s] CancelRunningTask %s:%d on %s", userName, pipelineName, taskID, t.AgentHost)
if err := mongodb.NewTaskColl().Update(t); err != nil {
log.Errorf("[%s] update task: %s:%d error: %v", userName, pipelineName, taskID, err)
return err
}
q := covertTaskToQueue(t)
Remove(q, log)
if err := nsq.Publish(setting.TopicCancel, b); err != nil {
log.Errorf("[%s] cancel %s %s:%d error", userName, t.AgentHost, pipelineName, taskID)
return err
}
if typeString == config.WorkflowType {
_ = scmNotifyService.UpdateWebhookComment(t, log)
} else if typeString == config.TestType {
_ = scmNotifyService.UpdateWebhookCommentForTest(t, log)
} else if typeString == config.SingleType {
_ = scmNotifyService.UpdatePipelineWebhookComment(t, log)
} else if typeString == config.ScanningType {
err = scmNotifyService.UpdateWebhookCommentForScanning(t, log)
// this logic will not affect the result of cancellation, so we only print the error in logs.
if err != nil {
log.Errorf("Failed to update webhook comment for scanning in upon cancel, the error is: %s", err)
}
}
return nil
}
func GetServiceTasks(log *zap.SugaredLogger) (map[string][]string, error) {
taskPreviewList, err := mongodb.NewTaskColl().List(&mongodb.ListTaskOption{Type: config.ServiceType})
if err != nil {
log.Errorf("getJobNamespaceAndSelector PipelineTaskV2.List error: %v", err)
return nil, e.ErrListTasks
}
respTaskNamespaceMap := make(map[string][]string)
for _, taskPreview := range taskPreviewList {
if _, isExist := respTaskNamespaceMap[taskPreview.Namespace]; isExist {
list := sets.NewString(respTaskNamespaceMap[taskPreview.Namespace]...)
if !list.Has(taskPreview.ServiceName) {
respTaskNamespaceMap[taskPreview.Namespace] = append(respTaskNamespaceMap[taskPreview.Namespace], taskPreview.ServiceName)
}
} else {
respTaskNamespaceMap[taskPreview.Namespace] = []string{taskPreview.ServiceName}
}
}
return respTaskNamespaceMap, nil
}
// workaround. only used to delete a queue item
func covertTaskToQueue(t *task.Task) *models.Queue {
return &models.Queue{
TaskID: t.TaskID,
PipelineName: t.PipelineName,
Type: t.Type,
CreateTime: t.CreateTime,
}
}
func GetWorkflowTaskCallback(taskID int64, pipelineName string) (*commonmodels.CallbackRequest, error) {
return mongodb.NewCallbackRequestColl().Find(&mongodb.CallbackFindOption{
TaskID: taskID,
PipelineName: pipelineName,
})
}