-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtask.go
More file actions
69 lines (60 loc) · 1.19 KB
/
task.go
File metadata and controls
69 lines (60 loc) · 1.19 KB
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
package main
import (
"suno-api/common"
"suno-api/entity/po"
"time"
)
var queue = make(chan Task, 100)
type Task struct {
Action string
ID string
}
func AddTask(task Task) {
queue <- task
}
func startTaskWorker() {
common.SafeGoroutine(func() {
for task := range queue {
switch task.Action {
case po.TaskActionMusic:
t := task
safeRun(func() {
common.LoopTask(func() (bool, *common.RelayError) {
done, relayErr := loopFetchTask(t.ID)
if relayErr != nil {
common.Logger.Errorw("loopFetchTask error", "err", relayErr)
return false, relayErr
}
if done {
common.Logger.Infow("任务完成", "task", t.ID)
return true, nil
}
return false, nil
}, 5*time.Second, 10*time.Minute, true)
})
}
}
})
}
func safeRun(fn func()) {
defer func() {
if err := recover(); err != nil {
common.Logger.Errorw("safeRun panic", "err", err)
}
}()
fn()
}
func recoverTasks() {
tasks, err := po.GetTaskByQuery(po.TaskQuery{
UnFinish: true,
})
if err != nil {
common.Logger.Fatalw("recoverTasks error", "err", err)
}
for _, t := range tasks {
AddTask(Task{
Action: t.Action,
ID: t.TaskID,
})
}
}