添加定时任务运行时间计算

This commit is contained in:
junleea 2025-03-27 11:19:20 +08:00
parent 395f1f681d
commit 9cf9876fb9
2 changed files with 12 additions and 8 deletions

View File

@ -17,11 +17,12 @@ type CID struct {
type CIDRunLog struct { type CIDRunLog struct {
gorm.Model gorm.Model
CID_id int `gorm:"column:cid_id"` CID_id int `gorm:"column:cid_id"`
Auth_id int `form:"column:auth_id"` Auth_id int `form:"column:auth_id"`
Script string `gorm:"column:script"` Script string `gorm:"column:script"`
Log string `gorm:"column:log"` RunTime float64 `gorm:"column:run_time"`
Error string `gorm:"column:error"` Log string `gorm:"column:log"`
Error string `gorm:"column:error"`
} }
// CreateCID 创建持续集成、部署 // CreateCID 创建持续集成、部署
@ -75,8 +76,8 @@ func UpdateCIDByID(id, auth_id, time int, name, url, script, token string) bool
} }
// CreateRunLog,添加执行日志 // CreateRunLog,添加执行日志
func CreateRunLog(cid_id, auth_id int, script, log, err string) uint { func CreateRunLog(cid_id, auth_id int, script, log, err string, runtime float64) uint {
cidRunLog := CIDRunLog{CID_id: cid_id, Auth_id: auth_id, Log: log, Error: err, Script: script} cidRunLog := CIDRunLog{CID_id: cid_id, Auth_id: auth_id, Log: log, Error: err, Script: script, RunTime: runtime}
result := DB.Create(&cidRunLog) result := DB.Create(&cidRunLog)
if result != nil { if result != nil {
fmt.Println(err) fmt.Println(err)

View File

@ -8,6 +8,7 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"time"
"videoplayer/dao" "videoplayer/dao"
"videoplayer/proto" "videoplayer/proto"
"videoplayer/worker" "videoplayer/worker"
@ -221,6 +222,7 @@ func RunShell(username, url, script string, id, authID int) {
echo "start" echo "start"
` + script + ` ` + script + `
echo "end"` echo "end"`
start := time.Now()
//执行脚本 //执行脚本
cmd := exec.Command("/bin/bash", "-c", scriptContent) cmd := exec.Command("/bin/bash", "-c", scriptContent)
// 使用bytes.Buffer捕获输出 // 使用bytes.Buffer捕获输出
@ -231,8 +233,9 @@ echo "end"`
if err3 != nil { if err3 != nil {
err3_info = err3.Error() err3_info = err3.Error()
} }
elapsed := time.Since(start)
//fmt.Println("bash content:", scriptContent) //fmt.Println("bash content:", scriptContent)
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info) //添加执行日志 dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info, elapsed.Seconds()) //添加执行日志
} }
// 定时任务处理逻辑 // 定时任务处理逻辑