diff --git a/dao/shell.go b/dao/shell.go index b033a24..fff75a4 100644 --- a/dao/shell.go +++ b/dao/shell.go @@ -8,12 +8,14 @@ import ( type Shell struct { gorm.Model - AuthID uint `gorm:"column:auth_id"` - Server string `gorm:"column:server"` - ShellName string `gorm:"column:shell_name"` - ShellContent string `gorm:"column:shell_content"` - Status int `gorm:"column:status"` // 0 未执行 1 执行中 2 执行完成 - ShellResult string `gorm:"column:shell_result"` + AuthID uint `gorm:"column:auth_id"` + Server string `gorm:"column:server"` + ShellName string `gorm:"column:shell_name"` + ShellContent string `gorm:"column:shell_content"` + Status int `gorm:"column:status"` // 0 未执行 1 执行中 2 执行完成 3 执行出错 + ShellRuntime float64 `gorm:"column:shell_runtime"` // 执行时间,单位秒 + ShellDuration float64 `gorm:"column:shell_duration"` // 执行时长,单位秒 + ShellResult string `gorm:"column:shell_result"` } func CreateShell(shellName, shellContent, server string, uid uint) uint { @@ -57,17 +59,16 @@ func FindShellByAuthID(auth_id int) []Shell { return shells } -func UpdateShellByID(id, authId uint, shellName, shellContent string, status int, shellResult string) bool { - pd := FindShellByID(id, authId) - if pd[0].ID == 0 { - return false - } +func UpdateShellByID(id, authId uint, shellName, shellContent string, status int, shellResult string, shellRuntime, shellDuration float64) bool { var result *gorm.DB + shell := Shell{ShellName: shellName, ShellContent: shellContent, Status: status, ShellResult: shellResult, ShellRuntime: shellRuntime, ShellDuration: shellDuration} + var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { - result = DB.Debug().Model(&Shell{}).Where("id = ? and auth_id = ?", id, authId).Updates(Shell{ShellName: shellName, ShellContent: shellContent, Status: status, ShellResult: shellResult}) + db2 = DB.Debug() } else { - result = DB.Model(&Shell{}).Where("id = ? and auth_id = ?", id, authId).Updates(Shell{ShellName: shellName, ShellContent: shellContent, Status: status, ShellResult: shellResult}) + db2 = DB } + result = db2.Model(&Shell{}).Where("id = ? and auth_id = ?", id, authId).Updates(shell) if result.Error != nil { return false } diff --git a/handler/shell.go b/handler/shell.go index 397857f..79e551f 100644 --- a/handler/shell.go +++ b/handler/shell.go @@ -16,12 +16,14 @@ type CreateShellReq struct { Server string `json:"server" form:"server" binding:"required"` } type UpdateShellReq struct { - ID uint `json:"id" form:"id" binding:"required"` - ShellName string `json:"shell_name" form:"shell_name"` - ShellContent string `json:"shell_content" form:"shell_content"` - Server string `json:"server" form:"server"` - Status int `json:"status" form:"status"` - ShellResult string `json:"shell_result" form:"shell_result"` + ID uint `json:"id" form:"id" binding:"required"` + ShellName string `json:"shell_name" form:"shell_name"` + ShellContent string `json:"shell_content" form:"shell_content"` + Server string `json:"server" form:"server"` + Status int `json:"status" form:"status"` + ShellResult string `json:"shell_result" form:"shell_result"` + ShellRuntime float64 `json:"shell_runtime" form:"shell_runtime"` // 执行时间,单位秒 + ShellDuration float64 `json:"shell_duration" form:"shell_duration"` // 执行时长,单位秒 } type UpdateShellReqV2 struct { @@ -83,7 +85,7 @@ func (s *ShellHandler) UpdateShell(c *gin.Context) { var resp []UpdateShellResp //log.Println("UpdateShellReqData:", req.Shells) for _, v := range req.Shells { - if service.UpdateShellByID(v.ID, uint(id), v.ShellName, v.ShellContent, v.Server, v.Status, v.ShellResult) { + if service.UpdateShellByIDV2(v.ID, uint(id), v.ShellName, v.ShellContent, v.Server, v.Status, v.ShellResult, v.ShellRuntime) { resp = append(resp, UpdateShellResp{ID: v.ID, Status: v.Status}) } else { resp = append(resp, UpdateShellResp{ID: v.ID, Status: -1}) diff --git a/proto/user_req.go b/proto/user_req.go index 08d70bd..9633f65 100644 --- a/proto/user_req.go +++ b/proto/user_req.go @@ -92,12 +92,13 @@ type SyncUserShellReq struct { } type UpdateShellReq struct { - ID uint `json:"id"` - ShellName string `json:"shell_name"` - ShellContent string `json:"shell_content"` - Server string `json:"server"` - Status int `json:"status"` - ShellResult string `json:"shell_result"` + ID uint `json:"id"` + ShellName string `json:"shell_name"` + ShellContent string `json:"shell_content"` + Server string `json:"server"` + Status int `json:"status"` + ShellResult string `json:"shell_result"` + ShellRuntime float64 `json:"shell_runtime" form:"shell_runtime"` // 执行时间,单位秒 } // shell 执行结果返回 diff --git a/service/shellService.go b/service/shellService.go index 9818bc6..eb76c45 100644 --- a/service/shellService.go +++ b/service/shellService.go @@ -6,6 +6,7 @@ import ( "log" "os/exec" "strings" + "time" "videoplayer/dao" "videoplayer/proto" "videoplayer/worker" @@ -29,7 +30,26 @@ func DeleteShellByID(id, authId uint) bool { } func UpdateShellByID(id, authId uint, shellName, shellContent, server string, status int, shellResult string) bool { - return dao.UpdateShellByID(id, authId, shellName, shellContent, status, shellResult) + return dao.UpdateShellByID(id, authId, shellName, shellContent, status, shellResult, 0, 0) +} + +func UpdateShellByIDV2(id, authId uint, shellName, shellContent, server string, status int, shellResult string, shellRuntime float64) bool { + //查看shell是否存在 + pd := dao.FindShellByID(id, authId) + if len(pd) < 1 && pd[0].ID == 0 { + return false + } + shell := pd[0] + //如果状态为2,3转为0,1是不允许的,为了防止出现1确认包阻塞问题 + if (shell.Status == 2 || shell.Status == 3) && (status == 0 || status == 1) { + log.Println("UpdateShellByIDV2: status change from 2/3 to 0/1 is not allowed, shell id:", id) + return false + } + var elapsed float64 //持续时间 + if (shell.Status == 0 || shell.Status == 1) && (status == 2 || status == 3) { //如果状态为执行中,且新更新时间状态为2或3,则获取持续时间 + elapsed = time.Since(shell.CreatedAt).Seconds() + } + return dao.UpdateShellByID(id, authId, shellName, shellContent, status, shellResult, shellRuntime, elapsed) } func FindShellWillRunByServer(server string, uid int) ([]dao.Shell, error) { @@ -43,7 +63,7 @@ func FindShellWillRunByServer(server string, uid int) ([]dao.Shell, error) { shells = dao.FindShellWillRunByServer(server, uint(uid)) //设置状态为执行中 for _, v := range shells { - dao.UpdateShellByID(v.ID, uint(uid), v.ShellName, v.ShellContent, v.Status+1, v.ShellResult) + dao.UpdateShellByID(v.ID, uint(uid), v.ShellName, v.ShellContent, v.Status+1, v.ShellResult, 0, 0) //将状态设置为执行中 } return shells, err } @@ -58,11 +78,14 @@ func ShellWillRunFromServer() { for _, v := range shells { //执行shell脚本,go执行命令 + start := time.Now() res, err2 := RunShell(v.ShellContent) + //执行时间,转成秒 + shellRuntime := time.Since(start).Seconds() if err2 != "" { - resp = append(resp, proto.UpdateShellReq{ID: v.ID, Server: v.Server, Status: 2, ShellResult: err2}) + resp = append(resp, proto.UpdateShellReq{ID: v.ID, Server: v.Server, Status: 3, ShellResult: err2, ShellRuntime: shellRuntime}) //执行出错 } else { - resp = append(resp, proto.UpdateShellReq{ID: v.ID, Server: v.Server, Status: 2, ShellResult: res}) + resp = append(resp, proto.UpdateShellReq{ID: v.ID, Server: v.Server, Status: 2, ShellResult: res}) //执行成功 } } if len(resp) == 0 {