修改shell运行时间及部分bug修复

This commit is contained in:
junleea 2025-08-03 17:14:51 +08:00
parent 301817c353
commit fb1752b9d7
4 changed files with 57 additions and 30 deletions

View File

@ -8,12 +8,14 @@ import (
type Shell struct { type Shell struct {
gorm.Model gorm.Model
AuthID uint `gorm:"column:auth_id"` AuthID uint `gorm:"column:auth_id"`
Server string `gorm:"column:server"` Server string `gorm:"column:server"`
ShellName string `gorm:"column:shell_name"` ShellName string `gorm:"column:shell_name"`
ShellContent string `gorm:"column:shell_content"` ShellContent string `gorm:"column:shell_content"`
Status int `gorm:"column:status"` // 0 未执行 1 执行中 2 执行完成 Status int `gorm:"column:status"` // 0 未执行 1 执行中 2 执行完成 3 执行出错
ShellResult string `gorm:"column:shell_result"` 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 { func CreateShell(shellName, shellContent, server string, uid uint) uint {
@ -57,17 +59,16 @@ func FindShellByAuthID(auth_id int) []Shell {
return shells return shells
} }
func UpdateShellByID(id, authId uint, shellName, shellContent string, status int, shellResult string) bool { func UpdateShellByID(id, authId uint, shellName, shellContent string, status int, shellResult string, shellRuntime, shellDuration float64) bool {
pd := FindShellByID(id, authId)
if pd[0].ID == 0 {
return false
}
var result *gorm.DB 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 { 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 { } 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 { if result.Error != nil {
return false return false
} }

View File

@ -16,12 +16,14 @@ type CreateShellReq struct {
Server string `json:"server" form:"server" binding:"required"` Server string `json:"server" form:"server" binding:"required"`
} }
type UpdateShellReq struct { type UpdateShellReq struct {
ID uint `json:"id" form:"id" binding:"required"` ID uint `json:"id" form:"id" binding:"required"`
ShellName string `json:"shell_name" form:"shell_name"` ShellName string `json:"shell_name" form:"shell_name"`
ShellContent string `json:"shell_content" form:"shell_content"` ShellContent string `json:"shell_content" form:"shell_content"`
Server string `json:"server" form:"server"` Server string `json:"server" form:"server"`
Status int `json:"status" form:"status"` Status int `json:"status" form:"status"`
ShellResult string `json:"shell_result" form:"shell_result"` 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 { type UpdateShellReqV2 struct {
@ -83,7 +85,7 @@ func (s *ShellHandler) UpdateShell(c *gin.Context) {
var resp []UpdateShellResp var resp []UpdateShellResp
//log.Println("UpdateShellReqData:", req.Shells) //log.Println("UpdateShellReqData:", req.Shells)
for _, v := range 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}) resp = append(resp, UpdateShellResp{ID: v.ID, Status: v.Status})
} else { } else {
resp = append(resp, UpdateShellResp{ID: v.ID, Status: -1}) resp = append(resp, UpdateShellResp{ID: v.ID, Status: -1})

View File

@ -92,12 +92,13 @@ type SyncUserShellReq struct {
} }
type UpdateShellReq struct { type UpdateShellReq struct {
ID uint `json:"id"` ID uint `json:"id"`
ShellName string `json:"shell_name"` ShellName string `json:"shell_name"`
ShellContent string `json:"shell_content"` ShellContent string `json:"shell_content"`
Server string `json:"server"` Server string `json:"server"`
Status int `json:"status"` Status int `json:"status"`
ShellResult string `json:"shell_result"` ShellResult string `json:"shell_result"`
ShellRuntime float64 `json:"shell_runtime" form:"shell_runtime"` // 执行时间,单位秒
} }
// shell 执行结果返回 // shell 执行结果返回

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"os/exec" "os/exec"
"strings" "strings"
"time"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/proto" "videoplayer/proto"
"videoplayer/worker" "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 { 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) { 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)) shells = dao.FindShellWillRunByServer(server, uint(uid))
//设置状态为执行中 //设置状态为执行中
for _, v := range shells { 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 return shells, err
} }
@ -58,11 +78,14 @@ func ShellWillRunFromServer() {
for _, v := range shells { for _, v := range shells {
//执行shell脚本,go执行命令 //执行shell脚本,go执行命令
start := time.Now()
res, err2 := RunShell(v.ShellContent) res, err2 := RunShell(v.ShellContent)
//执行时间,转成秒
shellRuntime := time.Since(start).Seconds()
if err2 != "" { 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 { } 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 { if len(resp) == 0 {