You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
2.5 KiB
124 lines
2.5 KiB
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/spf13/cast"
|
|
"os/exec"
|
|
"strings"
|
|
"tools/logic/cutil"
|
|
"tools/logic/orm"
|
|
)
|
|
|
|
func Cmd(cxt *gin.Context) {
|
|
cmd := cxt.PostForm("cmd")
|
|
c := exec.Command("bash", "-c", cmd)
|
|
e := c.Run()
|
|
fmt.Println(e)
|
|
}
|
|
|
|
func CreateTask(ctx *gin.Context) {
|
|
taskId := ctx.PostForm("task_id")
|
|
process := ctx.PostForm("process")
|
|
Type := ctx.PostForm("type")
|
|
params := ctx.PostForm("params")
|
|
status := ctx.PostForm("status")
|
|
total := ctx.PostForm("total")
|
|
thread := ctx.PostForm("thread")
|
|
weight := ctx.PostForm("weight")
|
|
script := ctx.PostForm("script")
|
|
task := orm.Get[*Task](cast.ToInt64(taskId))
|
|
if task != nil {
|
|
task.Thread = cast.ToInt(thread)
|
|
task.Status = cast.ToInt(status)
|
|
return
|
|
}
|
|
task = &Task{
|
|
TaskId: cast.ToInt64(taskId),
|
|
Type: Type,
|
|
Params: params,
|
|
Status: noRun,
|
|
Process: cast.ToInt64(process),
|
|
Total: cast.ToInt64(total),
|
|
Thread: cast.ToInt(thread),
|
|
Weight: cast.ToInt32(weight),
|
|
Script: script,
|
|
}
|
|
orm.Register[*Task](task)
|
|
orm.Save()
|
|
}
|
|
|
|
func GetTask(ctx *gin.Context) {
|
|
taskId := ctx.Request.URL.Query().Get("task_id")
|
|
task := orm.Get[*Task](cast.ToInt64(taskId))
|
|
if task != nil {
|
|
ctx.JSON(200, task)
|
|
return
|
|
}
|
|
ctx.JSON(500, nil)
|
|
}
|
|
|
|
func ClearAll(ctx *gin.Context) {
|
|
clearAll()
|
|
ctx.JSON(200, nil)
|
|
}
|
|
|
|
func GetAllTask(ctx *gin.Context) {
|
|
ctx.JSON(200, orm.GetAll[*Task]())
|
|
}
|
|
|
|
func GetAllTaskByIds(ctx *gin.Context) {
|
|
taskIds := ctx.Request.URL.Query().Get("task_ids")
|
|
ids := strings.Split(taskIds, ",")
|
|
var result = make(map[int64]*Task)
|
|
for _, id := range ids {
|
|
task := orm.Get[*Task](cast.ToInt64(id))
|
|
if task != nil {
|
|
result[task.TaskId] = task
|
|
}
|
|
}
|
|
ctx.JSON(200, result)
|
|
}
|
|
func DelTask(ctx *gin.Context) {
|
|
id := ctx.Request.URL.Query().Get("task_id")
|
|
taskId := cast.ToInt64(id)
|
|
err := orm.Delete[*Task](taskId, func(task *Task) {
|
|
task.Lock()
|
|
cutil.DeleteLog(task.TaskId)
|
|
task.delete()
|
|
task.Unlock()
|
|
})
|
|
if err != nil {
|
|
ctx.JSON(500, nil)
|
|
return
|
|
}
|
|
|
|
ctx.JSON(200, nil)
|
|
}
|
|
|
|
func RunTask(ctx *gin.Context) {
|
|
taskId := ctx.PostForm("task_id")
|
|
thread := ctx.PostForm("process")
|
|
task := orm.Get[*Task](cast.ToInt64(taskId))
|
|
if task == nil {
|
|
fmt.Println("task not found")
|
|
return
|
|
}
|
|
task.Lock()
|
|
defer task.Unlock()
|
|
cutil.DeleteLog(task.TaskId)
|
|
task.Thread = cast.ToInt(thread)
|
|
task.run()
|
|
|
|
}
|
|
|
|
func StopTask(ctx *gin.Context) {
|
|
id := ctx.Request.URL.Query().Get("task_id")
|
|
task := orm.Get[*Task](cast.ToInt64(id))
|
|
if task == nil {
|
|
return
|
|
}
|
|
task.Lock()
|
|
defer task.Unlock()
|
|
task.Stop()
|
|
}
|
|
|