-
Notifications
You must be signed in to change notification settings - Fork 167
/
task.go
375 lines (334 loc) · 9.39 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package tasks
import (
"context"
"google.golang.org/protobuf/encoding/protojson"
"sync/atomic"
"time"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/proto/jobs"
"github.com/pydio/cells/v4/common/service/context"
"github.com/pydio/cells/v4/common/utils/permissions"
"github.com/pydio/cells/v4/common/utils/uuid"
"github.com/pydio/cells/v4/scheduler/actions"
)
type Task struct {
*jobs.Job
common.RuntimeHolder
runID string
context context.Context
cancel context.CancelFunc
finished chan bool
rci atomic.Int32
chi int
di int
event interface{}
task *jobs.Task
lastStatus jobs.TaskStatus
lastStatusMsg string
lastHasProgress, lastCanPause bool
lastProgress float32
err error
}
// NewTaskFromEvent creates a task based on incoming job and event
func NewTaskFromEvent(runtime, ctx context.Context, job *jobs.Job, event interface{}) *Task {
ctxUserName, _ := permissions.FindUserNameInContext(ctx)
taskID := uuid.New()
if trigger, ok := event.(*jobs.JobTriggerEvent); ok && trigger.RunTaskId != "" {
taskID = trigger.RunTaskId
}
operationID := job.ID + "-" + taskID[0:8]
c := servicecontext.WithOperationID(ctx, operationID)
// Inject evaluated job parameters if it's not already here
if c.Value(ContextJobParametersKey{}) == nil {
params := jobs.RunParametersComputer(c, &jobs.ActionMessage{}, job, event)
c = context.WithValue(c, ContextJobParametersKey{}, params)
}
t := &Task{
context: c,
Job: job,
runID: taskID,
finished: make(chan bool, 1),
event: event,
task: &jobs.Task{
ID: taskID,
JobID: job.ID,
Status: jobs.TaskStatus_Queued,
StatusMessage: "Pending",
TriggerOwner: ctxUserName,
CanStop: true,
},
}
t.SetRuntimeContext(runtime)
return t
}
// Queue send this new task to the dispatcher queue.
// If a second queue is passed, it may differ from main input queue, so it is used for children queuing
func (t *Task) Queue(queue ...chan RunnerFunc) {
var ct context.Context
var can context.CancelFunc
if d, o := itemTimeout(t.context, t.Job.Timeout); o {
ct, can = context.WithTimeout(t.context, d)
} else {
ct, can = context.WithCancel(t.context)
}
t.context = ct
t.cancel = can
jobId := t.Job.ID
taskId := t.runID
ch := PubSub.Sub(PubSubTopicControl)
go func() {
defer func() {
UnSubWithFlush(ch, PubSubTopicControl)
}()
for {
select {
case <-t.finished:
return
case <-t.RuntimeContext.Done():
t.cancel()
case val := <-ch:
cmd, ok := val.(*jobs.CtrlCommand)
if !ok {
continue
}
if cmd.TaskId != "" && cmd.TaskId != taskId {
continue
}
if cmd.JobId != "" && cmd.JobId != jobId {
continue
}
if cmd.Cmd != jobs.Command_Stop {
continue
}
t.cancel()
}
}
}()
r := RootRunnable(t.context, t)
var secondaryQueue = queue[0]
if len(queue) > 1 {
secondaryQueue = queue[1]
}
if t.Job.MergeAction != nil {
r.SetupCollector(t.context, t.Job.MergeAction, secondaryQueue)
}
logStartMessageFromEvent(r.Context, t.event)
msg := createMessageFromEvent(t.event)
queue[0] <- func(queue chan RunnerFunc) {
r.Dispatch(msg, t.Actions, secondaryQueue)
}
}
// CleanUp is triggered after a task has no more subroutines running.
func (t *Task) CleanUp() {
t.SetEndTime(time.Now())
if t.err != nil {
t.SetStatus(jobs.TaskStatus_Error, t.err.Error())
} else {
t.SetStatus(jobs.TaskStatus_Finished, "Complete")
}
t.Save()
close(t.finished)
}
// Add increments task internal retain counter
func (t *Task) Add(delta int) {
rc := t.rci.Load()
if rc == 0 {
if t.task.StartTime == 0 {
t.task.StartTime = int32(time.Now().Unix())
}
t.SetStatus(jobs.TaskStatus_Running, "Starting...")
t.Save()
}
t.rci.Add(int32(delta))
}
// Done decrements task internal retain counter - When reaching 0, it triggers the CleanUp operation
func (t *Task) Done(delta int) {
newVal := t.rci.Add(-int32(delta))
if newVal == 0 {
t.CleanUp()
}
}
func (t *Task) Save() {
t.SaveStatus(nil, 0)
}
// SaveStatus publish task to PubSub topic, including Runnable context if passed
func (t *Task) SaveStatus(runnableContext context.Context, runnableStatus jobs.TaskStatus) {
if t.lastStatus == jobs.TaskStatus_Unknown || t.taskChanged() {
cl := t.Clone()
t.lastStatus = cl.Status
t.lastStatusMsg = cl.StatusMessage
t.lastHasProgress = cl.HasProgress
t.lastCanPause = cl.CanPause
t.lastProgress = cl.Progress
if runnableContext != nil {
PubSub.Pub(&TaskStatusUpdate{
Task: cl,
RunnableContext: runnableContext,
RunnableStatus: runnableStatus,
}, PubSubTopicTaskStatuses)
} else {
PubSub.Pub(cl, PubSubTopicTaskStatuses)
}
}
}
// Clone creates a protobuf clone of this task
func (t *Task) Clone() *jobs.Task {
bb, _ := protojson.Marshal(t.task)
cl := &jobs.Task{}
_ = protojson.Unmarshal(bb, cl)
return cl
//return proto.Clone(t.task).(*jobs.Task)
}
// GetRunUUID returns the task internal run UUID
func (t *Task) GetRunUUID() string {
return t.runID
}
// SetStatus updates task internal status
func (t *Task) SetStatus(status jobs.TaskStatus, message ...string) {
if len(message) > 0 {
t.task.StatusMessage = message[0]
}
t.task.Status = status
}
// SetProgress updates task internal progress
func (t *Task) SetProgress(progress float32) {
t.task.Progress = progress
}
// SetStartTime updates start time
func (t *Task) SetStartTime(ti time.Time) {
if t.task.StartTime == 0 {
t.task.StartTime = int32(ti.Unix())
}
}
// SetEndTime updates end time
func (t *Task) SetEndTime(ti time.Time) {
t.task.EndTime = int32(ti.Unix())
}
// SetControllable flags task as being able to be stopped or paused
func (t *Task) SetControllable(canPause bool) {
t.task.CanPause = canPause
}
// SetHasProgress flags task as providing progress information
func (t *Task) SetHasProgress() {
t.task.HasProgress = true
}
// SetError set task in error globally
func (t *Task) SetError(e error, appendLog bool) {
t.err = e
}
// GetRunnableChannels prepares a set of data channels for action actual Run method.
func (t *Task) GetRunnableChannels(runnableCtx context.Context, controllable bool) (*actions.RunnableChannels, chan bool) {
status, statusMsg, progress, done := t.createStatusesChannels(runnableCtx)
c := &actions.RunnableChannels{
Status: status,
StatusMsg: statusMsg,
Progress: progress,
}
if controllable {
c.Pause, c.Resume = t.createControlChannels(done)
}
return c, done
}
// createStatusesChannels provides a set of channel used by the runnable to send
// updates about its status to the outside world
func (t *Task) createStatusesChannels(runnableCtx context.Context) (chan jobs.TaskStatus, chan string, chan float32, chan bool) {
status := make(chan jobs.TaskStatus)
statusMsg := make(chan string)
progress := make(chan float32)
done := make(chan bool, 1)
go func() {
defer func() {
close(statusMsg)
close(status)
close(progress)
}()
for {
select {
case s := <-status:
t.task.Status = s
t.SaveStatus(runnableCtx, jobs.TaskStatus_Running)
case s := <-statusMsg:
t.task.StatusMessage = s
t.SaveStatus(runnableCtx, jobs.TaskStatus_Running)
case p := <-progress:
diff := p - t.task.Progress
save := false
if diff > 0.01 || p == 1 {
t.task.Progress = p
save = true
}
if save {
t.SaveStatus(runnableCtx, jobs.TaskStatus_Running)
}
case <-done:
return
}
}
}()
return status, statusMsg, progress, done
}
// createControlChannels provides a set of channel used to send some specific control instructions
// to the runnable
func (t *Task) createControlChannels(done chan bool) (pause chan interface{}, resume chan interface{}) {
pause, resume = make(chan interface{}), make(chan interface{})
jobId := t.Job.ID
taskId := t.task.ID
ch := PubSub.Sub(PubSubTopicControl)
go func() {
defer func() {
close(pause)
close(resume)
UnSubWithFlush(ch, PubSubTopicControl)
}()
for {
select {
case val := <-ch:
if cmd, ok := val.(*jobs.CtrlCommand); ok {
if cmd.TaskId != "" && cmd.TaskId != taskId {
continue
}
if cmd.JobId != "" && cmd.JobId != jobId {
continue
}
switch cmd.Cmd {
case jobs.Command_Pause:
pause <- cmd
case jobs.Command_Resume:
resume <- cmd
}
}
case <-done:
return
}
}
}()
return
}
func (t *Task) taskChanged() bool {
if t.lastStatus != t.task.Status || t.lastStatusMsg != t.task.StatusMessage {
return true
}
if t.lastHasProgress != t.task.HasProgress || t.lastCanPause != t.task.CanPause || t.lastProgress != t.task.Progress {
return true
}
return false
}