videoplayer/handler/cid.go

379 lines
11 KiB
Go
Raw Permalink Normal View History

package handler
import (
"bytes"
"encoding/json"
2024-07-05 16:55:57 +08:00
"fmt"
"github.com/gin-gonic/gin"
"os/exec"
"strconv"
2024-07-06 19:06:18 +08:00
"strings"
2025-03-27 11:19:20 +08:00
"time"
"videoplayer/dao"
"videoplayer/proto"
2025-06-01 16:34:37 +08:00
"videoplayer/service"
"videoplayer/worker"
)
type CIDCreateReq struct {
Name string `json:"name" form:"name"`
Url string `json:"url" form:"url"`
Time int `json:"time" form:"time"` // 定时任务单位秒大于0表示定时任务
Script string `json:"script" form:"script"`
}
type CIDDeleteReq struct {
ID int `json:"id" form:"id"`
}
type CIDRunReq struct {
ID int `json:"id" form:"id"`
}
type CIDLogReq struct {
ID int `json:"id" form:"id"`
}
type CIDUpdateReq struct {
ID int `json:"id" form:"id"`
Name string `json:"name" form:"name"`
Url string `json:"url" form:"url"`
Time int `json:"time" form:"time"` // 定时任务单位秒大于0表示定时任务
Script string `json:"script" form:"script"`
2024-07-05 16:55:57 +08:00
Token string `json:"cidtoken" form:"cidtoken"`
}
2024-11-23 17:46:13 +08:00
// 全局变量记录是否进行cron定时任务的刷新
var cron_count int
func SetUpCIDGroup(router *gin.Engine) {
cidGroup := router.Group("/cid") //持续集成、部署
cidGroup.POST("/create", CreateCID)
cidGroup.POST("/delete", DeleteCID)
cidGroup.POST("/update", UpdateCID)
cidGroup.POST("/list", GetCIDList)
cidGroup.POST("/run", RunCID)
cidGroup.POST("/log", GetCIDLogList) //获取执行日志
cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情
2024-12-06 19:33:52 +08:00
cidGroup.GET("/callback", CIDCallback)
2025-03-27 14:43:37 +08:00
cidGroup.POST("/callback", CIDCallback)
}
func RunCID(c *gin.Context) {
var req CIDRunReq
2024-07-21 11:12:54 +08:00
id, _ := c.Get("id")
authID := int(id.(float64))
//获取权限
2025-06-01 16:34:37 +08:00
//user := dao.FindUserByUserID(authID)
user := service.GetUserByIDFromUserCenter(authID)
2024-07-21 11:12:54 +08:00
if user.Run == false {
c.JSON(200, gin.H{"error": "no run Permissions", "code": proto.NoRunPermissions, "message": "no run Permissions"})
return
}
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
2024-07-07 11:57:33 +08:00
username, _ := c.Get("username")
cid := dao.FindCIDByID(req.ID, authID)
if cid.ID == 0 {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return
} else {
2024-07-08 15:35:38 +08:00
go RunShell(username.(string), cid.Url, cid.Script, req.ID, authID)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func CreateCID(c *gin.Context) {
var req CIDCreateReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
token, _ := generateRandomHexString(32)
res := dao.CreateCID(req.Name, req.Url, req.Script, token, req.Time, authID)
if res != 0 {
2024-11-23 17:46:13 +08:00
if req.Time > 0 {
updateCronRedisTime(int(res), req.Time)
}
2024-07-05 16:55:57 +08:00
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": res})
} else {
c.JSON(200, gin.H{"error": "CreateCID failed", "code": proto.OperationFailed, "message": "failed"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func DeleteCID(c *gin.Context) {
var req CIDDeleteReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cid := dao.DeleteCIDByID(req.ID, authID)
if cid == false {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return
} else {
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func UpdateCID(c *gin.Context) {
var req CIDUpdateReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cid := dao.UpdateCIDByID(req.ID, authID, req.Time, req.Name, req.Url, req.Script, req.Token)
if cid == false {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return
} else {
2024-11-23 17:46:13 +08:00
if req.Time > 0 {
updateCronRedisTime(req.ID, req.Time)
}
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func GetCIDList(c *gin.Context) {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cids := dao.FindCIDByAuthID(authID)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cids})
}
func GetCIDLog(c *gin.Context) {
var req CIDRunReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cidLogs := dao.FindRunLogByCIDLogID(req.ID, authID)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func GetCIDLogList(c *gin.Context) {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
var req CIDLogReq
if err := c.ShouldBind(&req); err == nil {
cidLogs := dao.FindRunLogByID(authID, req.ID)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func CIDCallback(c *gin.Context) {
// 获取用户ID
token := c.Query("token")
cid_id := c.Query("id")
2024-12-06 19:33:52 +08:00
//fmt.Println("token:", token, "cid_id:", cid_id)
//将cid转换为int
cid, _ := strconv.Atoi(cid_id)
2024-07-21 11:12:54 +08:00
if token == "" || cid == 0 {
c.JSON(200, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return
}
res := dao.FindCIDByIDAndToken(cid, token)
2024-08-04 21:27:38 +08:00
if res.ID == 0 {
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
return
}
2024-07-21 11:12:54 +08:00
2025-06-01 16:34:37 +08:00
//user := dao.FindUserByUserID(res.Auth_id)
user := service.GetUserByIDFromUserCenter(res.Auth_id)
2024-07-21 11:12:54 +08:00
if user.Run == false {
c.JSON(200, gin.H{"error": "no run Permissions", "code": proto.NoRunPermissions, "message": "the user has no run Permissions"})
return
}
if res.ID != 0 {
2024-07-09 10:10:03 +08:00
user := dao.FindUserByID(res.Auth_id)
go RunShell(user[0].Name, res.Url, res.Script, int(res.ID), res.Auth_id)
2024-07-08 15:35:38 +08:00
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
} else {
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
return
}
}
func RunShell(username, url, script string, id, authID int) {
strs := strings.Split(url, "/")
name := strs[len(strs)-1]
names := strings.Split(name, ".")
name = names[0]
//脚本内容,不同用户的持续集成、部署目录不同
scriptContent := `
echo "start"
2024-07-08 15:35:38 +08:00
` + script + `
echo "end"`
2025-03-27 11:19:20 +08:00
start := time.Now()
2024-07-08 15:35:38 +08:00
//执行脚本
cmd := exec.Command("/bin/bash", "-c", scriptContent)
// 使用bytes.Buffer捕获输出
var out bytes.Buffer
cmd.Stdout = &out
err3 := cmd.Run()
err3_info := ""
if err3 != nil {
err3_info = err3.Error()
}
2025-03-27 11:19:20 +08:00
elapsed := time.Since(start)
2024-10-08 13:57:03 +08:00
//fmt.Println("bash content:", scriptContent)
2025-03-27 11:19:20 +08:00
dao.CreateRunLog(id, authID, scriptContent, out.String(), err3_info, elapsed.Seconds()) //添加执行日志
}
// 定时任务处理逻辑
func RunCron() {
2024-11-23 17:46:13 +08:00
cron_count++
if cron_count > 6 {
updateCronFromDBToRedis()
cron_count = 0
}
//从redis查看是否有定时任务
//如果有定时任务,执行定时任务
//如果没有定时任务查找数据库是否有定时任务如果有则加入redis如果没有则不做任何操作
key := "cron_cid_runs"
res := worker.GetRedis(key)
2024-11-06 21:56:28 +08:00
//fmt.Println("cid run cron res:", res)
if res == "" {
2024-11-23 17:46:13 +08:00
readCronFromDBToRedis(key)
} else {
var cid_workers []proto.CIDRUN
2024-11-06 21:20:12 +08:00
err := json.Unmarshal([]byte(res), &cid_workers)
if err != nil {
fmt.Println("json unmarshal failed")
}
2024-11-06 21:56:28 +08:00
//fmt.Println("cid_workers:", cid_workers)
2024-11-06 21:25:57 +08:00
for i, v := range cid_workers {
//查找定时任务
if v.Curr-10 <= 0 {
cid := dao.FindCIDByCID(v.CID)
if cid.ID != 0 {
go RunShell("cron", cid.Url, cid.Script, int(cid.ID), cid.Auth_id)
}
2024-11-06 21:25:57 +08:00
cid_workers[i].Curr = v.Every
} else {
2024-11-06 21:25:57 +08:00
cid_workers[i].Curr = v.Curr - 10
}
}
//将定时任务加入redis
json_data, err := json.Marshal(cid_workers)
if err != nil {
fmt.Println("json marshal failed")
}
worker.SetRedis(key, string(json_data))
}
}
2024-11-23 17:46:13 +08:00
// 将数据库中的定时任务加入redis
func readCronFromDBToRedis(key string) {
cids := dao.FindCIDByTime()
cid_workers := make([]proto.CIDRUN, 0)
for _, v := range cids {
cid_worker := proto.CIDRUN{CID: v.ID, Curr: v.Time, Every: v.Time}
cid_workers = append(cid_workers, cid_worker)
}
if len(cid_workers) > 0 {
//将定时任务加入redis
json_data, err := json.Marshal(cid_workers)
if err != nil {
fmt.Println("json marshal failed")
}
worker.SetRedis(key, string(json_data))
}
}
// 从数据库更新定时任务到redis
func updateCronFromDBToRedis() {
key := "cron_cid_runs"
cids := dao.FindCIDByTime()
cid_maps := make(map[uint]int)
//将数据库中的定时任务加入mapkey为cidvalue为时间便于后续查找
for _, v := range cids {
cid_maps[v.ID] = v.Time
}
res := worker.GetRedis(key)
if res == "" {
readCronFromDBToRedis(key)
return
}
var cid_workers_redis []proto.CIDRUN
err := json.Unmarshal([]byte(res), &cid_workers_redis)
if err != nil {
fmt.Println("json unmarshal failed")
return
}
for i, v := range cid_workers_redis {
if time, ok := cid_maps[v.CID]; ok {
if v.Every != time {
cid_workers_redis[i].Every = time
cid_workers_redis[i].Curr = time
}
}
}
//将定时任务加入redis
json_data, err := json.Marshal(cid_workers_redis)
if err != nil {
fmt.Println("json marshal failed")
return
}
worker.SetRedis(key, string(json_data))
}
// 查看指定定时任务是否存在,如果存在则更新时间,如果不存在则加入
func updateCronRedisTime(id int, time int) {
key := "cron_cid_runs"
res := worker.GetRedis(key)
if res == "" {
readCronFromDBToRedis(key)
return
} else {
var cid_workers []proto.CIDRUN
err := json.Unmarshal([]byte(res), &cid_workers)
if err != nil {
fmt.Println("json unmarshal failed")
}
isContain := false
for i, v := range cid_workers {
if v.CID == uint(id) {
//更新时间,不会继续原来的时间
cid_workers[i].Curr = time
cid_workers[i].Every = time
isContain = true
}
}
if isContain == false {
cid_worker := proto.CIDRUN{CID: uint(id), Curr: time, Every: time}
cid_workers = append(cid_workers, cid_worker)
}
//将定时任务加入redis
json_data, err := json.Marshal(cid_workers)
if err != nil {
fmt.Println("json marshal failed")
}
worker.SetRedis(key, string(json_data))
}
}