-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
114 lines (101 loc) · 3.02 KB
/
ui.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
package cron_ui
import (
"log"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
)
const DateTimeLayout = "2006-01-02 15:04:05"
type JobInfo struct {
Name string
Spec string
}
type CronUI struct {
cron *cron.Cron
entriesInfo map[cron.EntryID]JobInfo
manualHistory map[cron.EntryID][]string
engine *gin.Engine
host string
}
func NewCronUI(c *cron.Cron, info map[cron.EntryID]JobInfo, host string) *CronUI {
return &CronUI{
cron: c,
host: host,
engine: gin.Default(),
entriesInfo: info,
manualHistory: make(map[cron.EntryID][]string),
}
}
func (c *CronUI) Start() {
c.setUp()
err := c.engine.Run(c.host)
log.Fatalln(err)
}
func (c *CronUI) Stop() {
}
func (c *CronUI) setUp() {
c.engine.Use(CORS())
c.engine.GET("/", func(ctx *gin.Context) {
ctx.Data(http.StatusOK, "text/html;charset=UTF-8", []byte(template))
})
c.engine.GET("/cronjob", c.getCronJob)
c.engine.POST("/cronjob/:entry_id", c.startCronJob)
}
func (c *CronUI) getCronJob(ctx *gin.Context) {
entries := c.cron.Entries()
result := make([]map[string]interface{}, len(entries))
for i, entry := range entries {
result[i] = make(map[string]interface{})
result[i]["id"] = entry.ID
result[i]["name"] = entry.ID
if info, ok := c.entriesInfo[entry.ID]; ok {
result[i]["name"] = info.Name
result[i]["spec"] = info.Spec
}
if histories, ok := c.manualHistory[entry.ID]; ok {
result[i]["manual_history"] = histories
}
result[i]["pre_time"] = entry.Prev.Format(DateTimeLayout)
result[i]["next_time"] = entry.Next.Format(DateTimeLayout)
}
ctx.JSON(http.StatusOK, result)
}
func (c *CronUI) startCronJob(ctx *gin.Context) {
entryIDStr := ctx.Param("entry_id")
entryID, _ := strconv.Atoi(entryIDStr)
entry := c.cron.Entry(cron.EntryID(entryID))
if entry.Next.Before(time.Now().Add(10 * time.Second)) {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "10秒后启动的任务无法手动启动",
})
return
}
go func(e *cron.Entry) {
e.Job.Run()
}(&entry)
histories, ok := c.manualHistory[entry.ID]
if !ok {
histories = make([]string, 0, 1)
}
histories = append(histories, time.Now().Format(DateTimeLayout))
c.manualHistory[entry.ID] = histories
ctx.JSON(http.StatusOK, gin.H{"message": "ok"})
}
func CORS() gin.HandlerFunc {
return func(context *gin.Context) {
// 允许 Origin 字段中的域发送请求
context.Writer.Header().Add("Access-Control-Allow-Origin", "*")
context.Writer.Header().Set("Access-Control-Max-Age", "86400")
context.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE, PATCH")
context.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length,Apitoken")
context.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Headers")
context.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if context.Request.Method == "OPTIONS" {
context.AbortWithStatus(200)
} else {
context.Next()
}
}
}