-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_task.go
117 lines (107 loc) · 2.12 KB
/
handle_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
package main
import (
"io"
"strings"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func listTasks(c *gin.Context) {
res := NewRes()
uid := c.GetString(identityKey)
if uid == "" {
uid = c.GetString(userKey)
}
var tasks []*Task
taskSet.Range(func(_, v interface{}) bool {
task, ok := v.(*Task)
if ok {
if task.Owner == uid {
tasks = append(tasks, task)
}
}
return true
})
dbtasks := []*Task{}
err := db.Where(`owner = ? `, uid).Find(&dbtasks).Error
if err != nil {
log.Errorf(`listTasks, query %s's tasks info error`, uid)
}
for _, t := range dbtasks {
updated := false
for i, tt := range tasks {
if tt.ID == t.ID {
tasks[i] = tt
updated = true
break
}
}
if !updated {
tasks = append(tasks, t)
}
}
res.DoneData(c, tasks)
}
func taskQuery(c *gin.Context) {
res := NewRes()
uid := c.GetString(identityKey)
if uid == "" {
uid = c.GetString(userKey)
}
ids := c.Param("ids")
if ids == "" {
res.Fail(c, 4001)
return
}
var tasks []*Task
for _, id := range strings.Split(ids, ",") {
v, ok := taskSet.Load(id)
if ok {
task, ok := v.(*Task)
if ok {
tasks = append(tasks, task)
continue
}
}
task := &Task{}
err := db.Where(`id = ? `, id).First(task).Error
if err == nil {
tasks = append(tasks, task)
continue
}
task.ID = id
task.Error = "task not found"
log.Warnf(`taskQuery, query %s's task(%s) info error`, uid, id)
tasks = append(tasks, task)
}
res.DoneData(c, tasks)
}
func taskStreamQuery(c *gin.Context) {
id := c.Param("id")
uid := c.GetString(identityKey)
if uid == "" {
uid = c.GetString(userKey)
}
log.Info(uid)
task, ok := taskSet.Load(id)
if ok {
// listener := openListener(roomid)
ticker := time.NewTicker(1 * time.Second)
// users.Add("connected", 1)
defer func() {
// closeListener(roomid, listener)
ticker.Stop()
// users.Add("disconnected", 1)
}()
c.Stream(func(w io.Writer) bool {
select {
// case msg := <-listener:
// messages.Add("outbound", 1)
// c.SSEvent("message", msg)
case <-ticker.C:
c.SSEvent("task", task)
}
return true
})
}
}