-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_manager.go
506 lines (388 loc) · 11.1 KB
/
task_manager.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Task manager
package main
import (
"encoding/json"
"errors"
"os"
"path"
"sort"
"sync"
"time"
)
// Task manager status data
type TaskManager struct {
vault *Vault // Reference to the vault
lock *sync.Mutex // Lock to control access
pending_tasks PendingTasksData // Pending tasks data
pending_tasks_file string // File to store pending tasks status
tasks map[uint64]*ActiveTask // Active tasks
queue []*ActiveTask // List of tasks waiting to start
running_count int32 // Counter of running tasks
max_tasks int32 // Max number of parallel tasks
}
// Active task status data
type ActiveTask struct {
definition *TaskDefinition // Task definition
running bool // True if running
waiting_session bool // True if the task needs credentials
killed bool // True if killed
session *ActiveSession // Reference to the associated session
status *TaskStatus // Task status
}
// Task status data
type TaskStatus struct {
Stage string `json:"stage"` // Name of the stage
StageStart int64 `json:"stage_start"` // Timestamp (Unix milliseconds) of stage start
Progress float64 `json:"stage_progress"` // Stage progress (0-100)
lock *sync.Mutex // Lock to control acess to status data
}
// Get task status
// Returns (1) Stage name
// Returns (2) Timestamp (Unix milliseconds) of stage start
// Returns (3) Stage progress (0-100)
func (s *TaskStatus) Get() (string, int64, float64) {
s.lock.Lock()
defer s.lock.Unlock()
return s.Stage, s.StageStart, s.Progress
}
// Sets stage name
// Auto sets stage start
// Resets progress to 0
// stage - Stage name
func (s *TaskStatus) SetStage(stage string) {
s.lock.Lock()
defer s.lock.Unlock()
s.Stage = stage
s.StageStart = time.Now().UnixMilli()
s.Progress = 0
}
// Sets stage progress
// p - Progress (0-100)
func (s *TaskStatus) SetProgress(p float64) {
s.lock.Lock()
defer s.lock.Unlock()
s.Progress = p
}
type TaskDefinitionType uint16
const (
TASK_ENCODE_ORIGINAL TaskDefinitionType = 0 // Encoding original asset
TASK_ENCODE_RESOLUTION TaskDefinitionType = 1 // Encoding extra resolution
TASK_IMAGE_PREVIEWS TaskDefinitionType = 2 // making previews images for videos
)
// Task definition data
type TaskDefinition struct {
Id uint64 `json:"id"` // Task ID
MediaId uint64 `json:"media_id"` // Media file ID
Type TaskDefinitionType `json:"type"` // Task type
Resolution *UserConfigResolution `json:"resolution"` // Resolution data
FirstTimeEncoding bool `json:"first_time_enc"` // First time media is encoded
}
// Pending tasks data
type PendingTasksData struct {
NextId uint64 `json:"next_id"` // ID for the next task
Pending map[uint64]*TaskDefinition `json:"pending"` // Pending tasks
}
// Initializes task manager
// base_path - Vault path
// vault - Reference to the vault
func (tm *TaskManager) Initialize(base_path string, vault *Vault) error {
tm.vault = vault
tm.lock = &sync.Mutex{}
tm.running_count = 0
tm.max_tasks = 1
tm.tasks = make(map[uint64]*ActiveTask)
tm.queue = make([]*ActiveTask, 0)
file := path.Join(base_path, "tasks.json")
tm.pending_tasks_file = file
if _, err := os.Stat(file); err == nil {
// exists
b, err := os.ReadFile(file)
if err != nil {
return err
}
// Parse
err = json.Unmarshal(b, &tm.pending_tasks)
if err != nil {
return err
}
// Initialize pending tasks
if tm.pending_tasks.Pending == nil {
tm.pending_tasks.Pending = make(map[uint64]*TaskDefinition)
}
for task_id, task_definition := range tm.pending_tasks.Pending {
if task_definition == nil {
continue
}
task := ActiveTask{
definition: task_definition,
status: &TaskStatus{
Stage: "",
StageStart: 0,
Progress: 0,
lock: &sync.Mutex{},
},
running: false,
waiting_session: true,
session: nil,
killed: false,
}
tm.tasks[task_id] = &task
}
} else if errors.Is(err, os.ErrNotExist) {
// does *not* exist
tm.pending_tasks.NextId = 0
tm.pending_tasks.Pending = make(map[uint64]*TaskDefinition)
} else {
return err
}
return nil
}
// Loads configuration
// key - Vault decryption key
func (tm *TaskManager) LoadUserConfigParams(key []byte) error {
uc, err := tm.vault.config.Read(key)
if err != nil {
return err
}
tm.lock.Lock()
defer tm.lock.Unlock()
tm.max_tasks = uc.MaxTasks
return nil
}
// Save pending tasks data
func (tm *TaskManager) SavePendingTasks() error {
tm.lock.Lock()
defer tm.lock.Unlock()
// Get the json data
jsonData, err := json.Marshal(tm.pending_tasks)
if err != nil {
return err
}
// Make a temp file
tFile := GetTemporalFileName("json", true)
// Write file
err = os.WriteFile(tFile, jsonData, FILE_PERMISSION)
if err != nil {
return err
}
// Move to the original path
err = RenameAndReplace(tFile, tm.pending_tasks_file)
if err != nil {
return err
}
return nil
}
// Call when a new session is created
// Provides credentials to tasks that need them
// session - Session reference
func (tm *TaskManager) OnNewSession(session *ActiveSession) error {
tm.lock.Lock()
// Check for tasks waiting for a session and queue them
for _, task := range tm.tasks {
if task.waiting_session {
task.session = session
task.waiting_session = false
tm.queue = append(tm.queue, task)
}
}
tm.lock.Unlock()
// Update user config
err := tm.LoadUserConfigParams(session.key)
if err != nil {
return err
}
tm.RunPendingTasks()
return nil
}
// Runs a task
// task - The task
func (tm *TaskManager) RunTask(task *ActiveTask) {
task.Run(tm.vault) // Run task
// After task has ended, remove it
tm.lock.Lock()
delete(tm.tasks, task.definition.Id)
delete(tm.pending_tasks.Pending, task.definition.Id)
tm.running_count--
tm.lock.Unlock()
// Save
err := tm.SavePendingTasks()
if err != nil {
LogError(err)
}
// Run other tasks
tm.RunPendingTasks()
}
// Runs pending tasks if possible
func (tm *TaskManager) RunPendingTasks() {
tm.lock.Lock()
defer tm.lock.Unlock()
// Pre-sort queue
sort.Slice(tm.queue, func(i, j int) bool {
if tm.queue[i].definition.Type < tm.queue[j].definition.Type {
return true
} else if tm.queue[i].definition.Type > tm.queue[j].definition.Type {
return false
} else if tm.queue[i].definition.Id < tm.queue[j].definition.Id {
return true
} else {
return false
}
})
for len(tm.queue) > 0 && (tm.max_tasks <= 0 || tm.running_count < tm.max_tasks) {
// Spawn next task
nextTask := tm.queue[0]
nextTask.running = true
tm.queue = tm.queue[1:] // Remove from queue
go tm.RunTask(nextTask) // Run
tm.running_count++
}
}
// Creates a task
// session - Session that creates the task
// media_id - Media file ID
// task_type - Task type
// resolution - Resolution data (if task requires it)
// firstTimeEncoding - True only for the first time the media is encoded after upload
// Returns the Id of the new task
func (tm *TaskManager) AddTask(session *ActiveSession, media_id uint64, task_type TaskDefinitionType, resolution *UserConfigResolution, firstTimeEncoding bool) uint64 {
tm.lock.Lock()
tm.pending_tasks.NextId++
task_id := tm.pending_tasks.NextId
task_definition := TaskDefinition{
Id: task_id,
MediaId: media_id,
Type: task_type,
Resolution: resolution,
FirstTimeEncoding: firstTimeEncoding,
}
task := ActiveTask{
definition: &task_definition,
status: &TaskStatus{
Stage: "",
StageStart: 0,
Progress: 0,
lock: &sync.Mutex{},
},
running: false,
waiting_session: false,
session: session,
killed: false,
}
tm.tasks[task_id] = &task
tm.pending_tasks.Pending[task_id] = task.definition // Add to pending list
tm.queue = append(tm.queue, &task) // Enqueue
tm.lock.Unlock()
// Save
err := tm.SavePendingTasks()
if err != nil {
LogError(err)
}
// Run other tasks
tm.RunPendingTasks()
return task_id
}
// Kills a task
// task_id - ID of the task
func (tm *TaskManager) KillTask(task_id uint64) {
tm.lock.Lock()
if tm.tasks[task_id] == nil {
tm.lock.Unlock()
return
}
tm.tasks[task_id].killed = true
delete(tm.pending_tasks.Pending, task_id) // Remove from pending tasks list
tm.lock.Unlock()
// Save
err := tm.SavePendingTasks()
if err != nil {
LogError(err)
}
// Run other tasks
tm.RunPendingTasks()
}
// Kill every task given a media ID
// media_id - Media file ID
func (tm *TaskManager) KillTaskByMedia(media_id uint64) {
tm.lock.Lock()
for task_id, task := range tm.tasks {
if task.definition.MediaId == media_id {
tm.tasks[task_id].killed = true
delete(tm.pending_tasks.Pending, task_id)
}
}
tm.lock.Unlock()
// Save
err := tm.SavePendingTasks()
if err != nil {
LogError(err)
}
// Run other tasks
tm.RunPendingTasks()
}
// Get task status
// task_id - ID of the task
// Returns task status
func (tm *TaskManager) GetTaskStatus(task_id uint64) *TaskStatus {
tm.lock.Lock()
defer tm.lock.Unlock()
if tm.tasks[task_id] == nil {
return nil
}
return tm.tasks[task_id].status
}
// Task list info data struct for API
type TaskListInfoEntry struct {
Id uint64 `json:"id"` // Task ID
Running bool `json:"running"` // True if running
MediaId uint64 `json:"media_id"` // Media file ID
Type TaskDefinitionType `json:"type"` // Task type
Resolution *UserConfigResolution `json:"resolution"` // Resolution data
Stage string `json:"stage"` // Name of current stage
StageStart int64 `json:"stage_start"` // Stage start timestamp (unix milliseconds)
Now int64 `json:"time_now"` // Server time (unix milliseconds)
Progress float64 `json:"stage_progress"` // Stage progress (0-100)
}
// Gets task status info for API
// task_id - Task ID
// Returns task info
func (tm *TaskManager) GetTaskInfo(task_id uint64) *TaskListInfoEntry {
tm.lock.Lock()
defer tm.lock.Unlock()
if tm.tasks[task_id] == nil {
return nil
}
task := tm.tasks[task_id]
var info TaskListInfoEntry
info.Id = task.definition.Id
info.Running = task.running
info.MediaId = task.definition.MediaId
info.Type = task.definition.Type
info.Resolution = task.definition.Resolution
stage, stage_start, stage_p := task.status.Get()
info.Stage = stage
info.StageStart = stage_start
info.Now = time.Now().UnixMilli()
info.Progress = stage_p
return &info
}
// Gets information of all active tasks for API
func (tm *TaskManager) GetAllTasks() []*TaskListInfoEntry {
tm.lock.Lock()
defer tm.lock.Unlock()
result := make([]*TaskListInfoEntry, 0)
for _, task := range tm.tasks {
var info TaskListInfoEntry
info.Id = task.definition.Id
info.Running = task.running
info.MediaId = task.definition.MediaId
info.Type = task.definition.Type
info.Resolution = task.definition.Resolution
stage, stage_start, stage_p := task.status.Get()
info.Stage = stage
info.StageStart = stage_start
info.Now = time.Now().UnixMilli()
info.Progress = stage_p
result = append(result, &info)
}
return result
}