-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
65 lines (56 loc) · 1.12 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
package main
import (
"net/http"
"sync"
"github.com/gin-gonic/gin"
)
type Task struct {
Id uint64 `gorm: PRIMARY_KEY`
Output string `gorm:"type:varchar(255);"`
// 0 = int, 1 = finish, 2 = running, 3 = err
Status uint8
FileType string `gorm:"type:varchar(255);"`
// CreatedAt time.Time
// UpdatedAt time.Time
// DeletedAt time.Time
lock *sync.RWMutex
}
type TaskFunction func(*Task)
func NewTask() *Task {
var t Task
t.Id, _ = sf.NextID()
t.Status = 0
t.lock = new(sync.RWMutex)
db.NewRecord(t)
db.Create(t)
return &t
}
func (t *Task) Run(f TaskFunction) {
t.Status = 0
t.Save()
go f(t)
}
func (t *Task) Save() {
t.lock.Lock()
defer t.lock.Unlock()
db.Model(t).Save(t)
}
func (t *Task) Delete() {
db.Delete(t)
}
func handleTask(c *gin.Context) {
tid := c.Param("tid")
var task Task
db.First(&task, tid)
logan.Info(task)
if task.Id != 0 {
c.JSON(http.StatusOK, result(RESULT_SUCCESS, "", gin.H{
"id": task.Id,
"output": task.Output,
"status": task.Status,
"fileType": task.FileType,
}))
} else {
c.JSON(http.StatusOK, result(RESULT_ERR, "not found task", gin.H{}))
}
}