-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathtask.go
284 lines (256 loc) · 9.21 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright Project Harbor 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 task
import (
"context"
"encoding/json"
"fmt"
"time"
cjob "github.com/goharbor/harbor/src/common/job"
"github.com/goharbor/harbor/src/common/job/models"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/lib/config"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/task/dao"
)
var (
// Mgr is a global task manager instance
Mgr = NewManager()
)
// Manager manages tasks.
// The execution and task managers provide an execution-task model to abstract the interactive with jobservice.
// All of the operations with jobservice should be delegated by them
type Manager interface {
// Create submits the job to jobservice and creates a corresponding task record.
// An execution must be created first and the task will be linked to it.
// The "extraAttrs" can be used to set the customized attributes
Create(ctx context.Context, executionID int64, job *Job, extraAttrs ...map[string]interface{}) (id int64, err error)
// Stop the specified task
Stop(ctx context.Context, id int64) (err error)
// Get the specified task
Get(ctx context.Context, id int64) (task *Task, err error)
// List the tasks according to the query
// Query the "ExtraAttrs" by setting 'query.Keywords["ExtraAttrs.key"]="value"'
List(ctx context.Context, query *q.Query) (tasks []*Task, err error)
// Update the extra attributes of the specified task
UpdateExtraAttrs(ctx context.Context, id int64, extraAttrs map[string]interface{}) (err error)
// Get the log of the specified task
GetLog(ctx context.Context, id int64) (log []byte, err error)
// GetLogByJobID get the log of specified job id
GetLogByJobID(ctx context.Context, jobID string) (log []byte, err error)
// Count counts total of tasks according to the query.
// Query the "ExtraAttrs" by setting 'query.Keywords["ExtraAttrs.key"]="value"'
Count(ctx context.Context, query *q.Query) (int64, error)
// Update the status of the specified task
Update(ctx context.Context, task *Task, props ...string) error
// UpdateStatusInBatch updates the status of tasks in batch
UpdateStatusInBatch(ctx context.Context, jobIDs []string, status string, batchSize int) error
// ExecutionIDsByVendorAndStatus retrieve execution id by vendor type and status
ExecutionIDsByVendorAndStatus(ctx context.Context, vendorType, status string) ([]int64, error)
// ListScanTasksByReportUUID lists scan tasks by report uuid, although it's a specific case but it will be
// more suitable to support multi database in the future.
ListScanTasksByReportUUID(ctx context.Context, uuid string) (tasks []*Task, err error)
}
// NewManager creates an instance of the default task manager
func NewManager() Manager {
return &manager{
dao: dao.NewTaskDAO(),
execDAO: dao.NewExecutionDAO(),
jsClient: cjob.GlobalClient,
coreURL: config.GetCoreURL(),
}
}
type manager struct {
dao dao.TaskDAO
execDAO dao.ExecutionDAO
jsClient cjob.Client
coreURL string
}
func (m *manager) Update(ctx context.Context, task *Task, props ...string) error {
return m.dao.Update(ctx, &dao.Task{
ID: task.ID,
Status: task.Status,
}, props...)
}
func (m *manager) Count(ctx context.Context, query *q.Query) (int64, error) {
return m.dao.Count(ctx, query)
}
func (m *manager) Create(ctx context.Context, executionID int64, jb *Job, extraAttrs ...map[string]interface{}) (int64, error) {
// create task record in database
id, err := m.createTaskRecord(ctx, executionID, extraAttrs...)
if err != nil {
return 0, err
}
log.Debugf("the database record for task %d created", id)
// submit job to jobservice
// As all database operations are in a transaction which is committed until API returns,
// when the job is submitted to the jobservice and running, the task record may not
// insert yet, this will cause the status hook handler returning 404, and the jobservice
// will re-send the status hook again
jobID, err := m.submitJob(ctx, id, jb)
if err != nil {
// failed to submit job to jobservice, delete the task record
log.Errorf("delete task %d from db due to failed to submit job %v, error: %v", id, jb.Name, err)
if err := m.dao.Delete(ctx, id); err != nil {
log.Errorf("failed to delete the task %d: %v", id, err)
}
return 0, err
}
log.Debugf("the task %d is submitted to jobservice, the job ID is %s", id, jobID)
// populate the job ID for the task
if err = m.dao.Update(ctx, &dao.Task{
ID: id,
JobID: jobID,
}, "JobID"); err != nil {
log.Errorf("failed to populate the job ID for the task %d: %v", id, err)
}
return id, nil
}
func (m *manager) createTaskRecord(ctx context.Context, executionID int64, extraAttrs ...map[string]interface{}) (int64, error) {
exec, err := m.execDAO.Get(ctx, executionID)
if err != nil {
return 0, err
}
extras := map[string]interface{}{}
if len(extraAttrs) > 0 && extraAttrs[0] != nil {
extras = extraAttrs[0]
}
data, err := json.Marshal(extras)
if err != nil {
return 0, err
}
now := time.Now()
return m.dao.Create(ctx, &dao.Task{
VendorType: exec.VendorType,
ExecutionID: executionID,
Status: job.PendingStatus.String(),
StatusCode: job.PendingStatus.Code(),
ExtraAttrs: string(data),
CreationTime: now,
UpdateTime: now,
})
}
func (m *manager) submitJob(_ context.Context, id int64, jb *Job) (string, error) {
jobData := &models.JobData{
Name: jb.Name,
StatusHook: fmt.Sprintf("%s/service/notifications/tasks/%d", m.coreURL, id),
}
if jb.Parameters != nil {
jobData.Parameters = models.Parameters(jb.Parameters)
}
if jb.Metadata != nil {
jobData.Metadata = &models.JobMetadata{
JobKind: jb.Metadata.JobKind,
ScheduleDelay: jb.Metadata.ScheduleDelay,
Cron: jb.Metadata.Cron,
IsUnique: jb.Metadata.IsUnique,
}
}
return m.jsClient.SubmitJob(jobData)
}
func (m *manager) Stop(ctx context.Context, id int64) error {
task, err := m.dao.Get(ctx, id)
if err != nil {
return err
}
// when a task is in final status, if it's a periodic or retrying job it will
// run again in the near future, so we must operate the stop action to these final
// status jobs as well
if err = m.jsClient.PostAction(task.JobID, string(job.StopCommand)); err != nil {
// job not found, update it's status to stop directly
if err == cjob.ErrJobNotFound {
now := time.Now()
err = m.dao.Update(ctx, &dao.Task{
ID: task.ID,
Status: job.StoppedStatus.String(),
StatusCode: job.StoppedStatus.Code(),
UpdateTime: now,
EndTime: now,
}, "Status", "StatusCode", "UpdateTime", "EndTime")
if err != nil {
return err
}
log.Debugf("got job not found error for task %d, update it's status to stop directly", task.ID)
// as in this case no status hook will be sent, here refresh the execution status directly
_, _, err = m.execDAO.RefreshStatus(ctx, task.ExecutionID)
return err
}
return err
}
log.Debugf("the stop request for task %d is sent", id)
return nil
}
func (m *manager) Get(ctx context.Context, id int64) (*Task, error) {
task, err := m.dao.Get(ctx, id)
if err != nil {
return nil, err
}
t := &Task{}
t.From(task)
return t, nil
}
func (m *manager) List(ctx context.Context, query *q.Query) ([]*Task, error) {
tasks, err := m.dao.List(ctx, query)
if err != nil {
return nil, err
}
var ts []*Task
for _, task := range tasks {
t := &Task{}
t.From(task)
ts = append(ts, t)
}
return ts, nil
}
func (m *manager) ListScanTasksByReportUUID(ctx context.Context, uuid string) ([]*Task, error) {
tasks, err := m.dao.ListScanTasksByReportUUID(ctx, uuid)
if err != nil {
return nil, err
}
var ts []*Task
for _, task := range tasks {
t := &Task{}
t.From(task)
ts = append(ts, t)
}
return ts, nil
}
func (m *manager) UpdateExtraAttrs(ctx context.Context, id int64, extraAttrs map[string]interface{}) error {
data, err := json.Marshal(extraAttrs)
if err != nil {
return err
}
return m.dao.Update(ctx, &dao.Task{
ID: id,
ExtraAttrs: string(data),
UpdateTime: time.Time{},
}, "ExtraAttrs", "UpdateTime")
}
func (m *manager) GetLog(ctx context.Context, id int64) ([]byte, error) {
task, err := m.dao.Get(ctx, id)
if err != nil {
return nil, err
}
return m.jsClient.GetJobLog(task.JobID)
}
func (m *manager) UpdateStatusInBatch(ctx context.Context, jobIDs []string, status string, batchSize int) error {
return m.dao.UpdateStatusInBatch(ctx, jobIDs, status, batchSize)
}
func (m *manager) ExecutionIDsByVendorAndStatus(ctx context.Context, vendorType, status string) ([]int64, error) {
return m.dao.ExecutionIDsByVendorAndStatus(ctx, vendorType, status)
}
func (m *manager) GetLogByJobID(_ context.Context, jobID string) (log []byte, err error) {
return m.jsClient.GetJobLog(jobID)
}