-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathscheduler.go
250 lines (183 loc) · 6.18 KB
/
scheduler.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
package controllers
import (
"strconv"
"net/http"
"bytes"
"time"
"github.com/lifei6671/go-git-webhook/models"
"github.com/lifei6671/go-git-webhook/modules/pager"
"github.com/lifei6671/go-git-webhook/tasks"
"github.com/gorilla/websocket"
"github.com/astaxie/beego/logs"
)
// upgrader 默认的 WebSocket 选项
var upgrader = websocket.Upgrader{CheckOrigin : verification} // use default options
// SchedulerController 任务调度控制器
type SchedulerController struct {
BaseController
}
// Index 首页
func (c *SchedulerController) Index() {
c.Prepare()
c.TplName = "scheduler/index_vue.html"
pageIndex, _ := c.GetInt("page", 1)
relation_id,_ := strconv.Atoi(c.Ctx.Input.Param(":id"))
if relation_id <= 0 {
c.Abort("404")
}
relationDetailedResult ,err := models.FindRelationDetailedByWhere("AND relation_id = ?",relation_id)
if err != nil {
logs.Info("FindRelationDetailed Error : ", err.Error())
c.Abort("500")
}
var relationDetailed models.RelationDetailed
if len(relationDetailedResult) > 0{
relationDetailed = relationDetailedResult[0]
}
var schedulers []models.Scheduler
pageOptions := pager.PageOptions{
TableName: models.NewScheduler().TableName(),
EnableFirstLastLink : true,
CurrentPage : pageIndex,
PageSize : 15,
ParamName : "page",
Conditions : " AND relation_id = " + strconv.Itoa(relationDetailed.RelationId) + " order by scheduler_id desc",
}
totalItem, totalCount, rs, pageHtml := pager.GetPagerLinks(&pageOptions, c.Ctx)
_,err = rs.QueryRows(&schedulers) //把当前页面的数据序列化进一个切片内
if err != nil {
logs.Error("",err.Error())
}
var webList []models.WebScheduler
if c.IsAjax() {
if len(schedulers) > 0 {
webList = make([]models.WebScheduler,len(schedulers))
for i, item := range schedulers {
webList[i] = (&item).ToWebScheduler()
}
}
c.JsonResult(0,"ok",webList)
}
c.Data["Model"] = relationDetailed
c.Data["lists"] = webList
c.Data["html"] = pageHtml
c.Data["totalItem"] = totalItem
c.Data["totalCount"] = totalCount
c.Data["WebHook"] = true
c.Data["WebSocketUrl"] = "ws://" + c.Ctx.Request.Host + c.URLFor("SchedulerController.Status",":scheduler_id","")
}
// Console 控制台
func (c *SchedulerController) Console() {
schedulerId,err := strconv.Atoi(c.Ctx.Input.Param(":scheduler_id"))
if err != nil {
c.JsonResult(500,"Parameter error")
}
scheduler := models.NewScheduler()
scheduler.SchedulerId = schedulerId
if err := scheduler.Find();err != nil {
c.JsonResult(500,"Error 50001: Query data error")
}
deailed,err := models.FindRelationDetailedByWhere("AND relation_id = ? AND member_id = ?", scheduler.RelationId,c.Member.MemberId)
if err != nil || len(deailed) <= 0{
c.JsonResult(404,"The data does not exist")
}
data := map[string]interface{}{"log": scheduler.LogContent,"status": scheduler.Status}
c.JsonResult(0,"ok",data)
}
// Cancel 取消任务
func (c *SchedulerController) Cancel() {
schedulerId,err := strconv.Atoi(c.Ctx.Input.Param(":scheduler_id"))
if err != nil {
c.JsonResult(500,"Parameter error")
}
scheduler := models.NewScheduler()
scheduler.SchedulerId = schedulerId
if err := scheduler.Find();err != nil {
c.JsonResult(500,"Error 50001: Query data error")
}
deailed,err := models.FindRelationDetailedByWhere("AND relation_id = ? AND member_id = ?", scheduler.RelationId,c.Member.MemberId)
if err != nil || len(deailed) <= 0{
c.JsonResult(404,"The data does not exist")
}
scheduler.Status = "suspend"
if err := scheduler.Save(); err != nil {
c.JsonResult(500,"Cancel failed")
}
c.JsonResult(0,"ok")
}
// Resume 重新执行
func (c *SchedulerController) Resume () {
schedulerId,err := strconv.Atoi(c.Ctx.Input.Param(":scheduler_id"))
if err != nil {
c.JsonResult(500,"Parameter error")
}
scheduler := models.NewScheduler()
scheduler.SchedulerId = schedulerId
if err := scheduler.Find();err != nil {
c.JsonResult(500,"Error 50001: Query data error")
}
deailed,err := models.FindRelationDetailedByWhere("AND relation_id = ? AND member_id = ?", scheduler.RelationId,c.Member.MemberId)
if err != nil || len(deailed) <= 0{
c.JsonResult(404,"The data does not exist")
}
newScheduler := models.NewScheduler()
newScheduler.Status = "wait"
newScheduler.ExecuteType = 1
newScheduler.WebHookId = scheduler.WebHookId
newScheduler.ServerId = scheduler.ServerId
newScheduler.RelationId = scheduler.RelationId
newScheduler.Data = scheduler.Data
newScheduler.PushUser = scheduler.PushUser
newScheduler.ShaValue = scheduler.ShaValue
if err := newScheduler.Save(); err != nil {
c.JsonResult(500,"Cancel failed")
}
webModel := newScheduler.ToWebScheduler()
go tasks.Add(tasks.Task{ SchedulerId : newScheduler.SchedulerId ,ServerId:newScheduler.ServerId,WebHookId:newScheduler.WebHookId})
view ,_:= c.ExecuteViewPathTemplate("scheduler/index_item.html",webModel)
data := map[string]interface{}{"view" : view,"data": webModel}
c.JsonResult(0,"ok",data)
}
// Status 任务状态
func (c *SchedulerController) Status() {
c.TplName = "scheduler/status.html"
ws, err := upgrader.Upgrade(c.Ctx.ResponseWriter.ResponseWriter, c.Ctx.Request, nil)
if err != nil {
c.StopRun()
}
defer ws.Close()
buf := bytes.NewBufferString("")
scheduler_id,err := strconv.Atoi(c.Ctx.Input.Param(":scheduler_id"));
if err != nil {
buf.WriteString("Parameter error.")
ws.WriteMessage(websocket.TextMessage,buf.Bytes())
c.StopRun()
}
scheduler := models.NewScheduler()
scheduler.SchedulerId = scheduler_id
if err := scheduler.Find();err != nil {
ws.WriteMessage(websocket.TextMessage,[]byte("Error 50001: Query data error"))
c.StopRun()
}
detailed,err := models.FindRelationDetailedByWhere("AND relation_id = ? AND member_id = ?", scheduler.RelationId,c.Member.MemberId)
if err != nil || len(detailed) <= 0{
ws.WriteMessage(websocket.TextMessage,[]byte("The data does not exist"))
c.StopRun()
}
for{
m := scheduler.ToWebScheduler()
err := ws.WriteJSON(m)
if err != nil {
c.StopRun()
}
time.Sleep(time.Second*2)
scheduler = models.NewScheduler()
scheduler.SchedulerId = scheduler_id
scheduler.Find();
}
c.StopRun()
}
// verification 校验是否可以连接 WebSocket
func verification( *http.Request) bool {
return true
}