feat: 任务支持form表单数据传入。

This commit is contained in:
Mr. Lan 2020-09-20 14:05:32 +08:00
parent 8608380d55
commit 59342ea0d1
7 changed files with 77 additions and 33 deletions

View File

@ -31,7 +31,12 @@
演示demo: [http://fdevops.com:8001/#/dashboard](http://fdevops.com:8001/#/dashboard) 演示demo: [http://fdevops.com:8001/#/dashboard](http://fdevops.com:8001/#/dashboard)
账号密码admin/123456 ```
账号admin
密码123456
演示demo登陆需要取消ldap验证就是登陆页面取消ldap的打勾。
```
文档: [https://www.fdevops.com/docs/ferry](https://www.fdevops.com/docs/ferry-tutorial-document/introduction) 文档: [https://www.fdevops.com/docs/ferry](https://www.fdevops.com/docs/ferry-tutorial-document/introduction)

View File

@ -71,6 +71,12 @@ func CreateWorkOrder(c *gin.Context) {
Tasks json.RawMessage `json:"tasks"` Tasks json.RawMessage `json:"tasks"`
Source string `json:"source"` Source string `json:"source"`
} }
paramsValue struct {
Id int `json:"id"`
Title string `json:"title"`
Priority int `json:"priority"`
FormData []interface{} `json:"form_data"`
}
) )
err := c.ShouldBind(&workOrderValue) err := c.ShouldBind(&workOrderValue)
@ -248,7 +254,18 @@ func CreateWorkOrder(c *gin.Context) {
return return
} }
if len(taskList) > 0 { if len(taskList) > 0 {
go service.ExecTask(taskList) paramsValue.Id = workOrderInfo.Id
paramsValue.Title = workOrderInfo.Title
paramsValue.Priority = workOrderInfo.Priority
paramsValue.FormData = workOrderValue.Tpls["form_data"]
params, err := json.Marshal(paramsValue)
if err != nil {
app.Error(c, -1, err, "")
return
}
go service.ExecTask(taskList, string(params))
} }
app.OK(c, "", "成功提交工单申请") app.OK(c, "", "成功提交工单申请")

View File

@ -348,6 +348,12 @@ func (h *Handle) HandleWorkOrder(
noticeList []int noticeList []int
sendSubject string = "您有一条待办工单,请及时处理" sendSubject string = "您有一条待办工单,请及时处理"
sendDescription string = "您有一条待办工单请及时处理,工单描述如下" sendDescription string = "您有一条待办工单请及时处理,工单描述如下"
paramsValue struct {
Id int `json:"id"`
Title string `json:"title"`
Priority int `json:"priority"`
FormData []interface{} `json:"form_data"`
}
) )
defer func() { defer func() {
@ -638,6 +644,8 @@ func (h *Handle) HandleWorkOrder(
return return
} }
paramsValue.FormData = append(paramsValue.FormData, t["tplValue"])
// 是否可写,只有可写的模版可以更新数据 // 是否可写,只有可写的模版可以更新数据
updateStatus := false updateStatus := false
if writeTplList, writeOK := h.stateValue["writeTpls"]; writeOK { if writeTplList, writeOK := h.stateValue["writeTpls"]; writeOK {
@ -823,7 +831,15 @@ continueTag:
} }
execTasks = append(execTasks, task) execTasks = append(execTasks, task)
} }
go ExecTask(execTasks)
paramsValue.Id = h.workOrderDetails.Id
paramsValue.Title = h.workOrderDetails.Title
paramsValue.Priority = h.workOrderDetails.Priority
params, err := json.Marshal(paramsValue)
if err != nil {
return err
}
go ExecTask(execTasks, string(params))
return return
} }

View File

@ -12,13 +12,13 @@ import (
@Author : lanyulei @Author : lanyulei
*/ */
func ExecTask(taskList []string) { func ExecTask(taskList []string, params string) {
for _, taskName := range taskList { for _, taskName := range taskList {
filePath := fmt.Sprintf("%v/%v", viper.GetString("script.path"), taskName) filePath := fmt.Sprintf("%v/%v", viper.GetString("script.path"), taskName)
if strings.HasSuffix(filePath, ".py") { if strings.HasSuffix(filePath, ".py") {
task.Send("python", filePath) task.Send("python", filePath, params)
} else if strings.HasSuffix(filePath, ".sh") { } else if strings.HasSuffix(filePath, ".sh") {
task.Send("shell", filePath) task.Send("shell", filePath, params)
} }
} }
} }

View File

@ -9,6 +9,6 @@ import (
"ferry/pkg/task/worker" "ferry/pkg/task/worker"
) )
func Send(classify string, scriptPath string) { func Send(classify string, scriptPath string, params string) {
worker.SendTask(context.Background(), classify, scriptPath) worker.SendTask(context.Background(), classify, scriptPath, params)
} }

View File

@ -14,7 +14,7 @@ func Start() {
worker.StartServer() worker.StartServer()
// 2. 启动异步调度 // 2. 启动异步调度
taskWorker := worker.NewAsyncTaskWorker(1) taskWorker := worker.NewAsyncTaskWorker(10)
err := taskWorker.Launch() err := taskWorker.Launch()
if err != nil { if err != nil {
logger.Errorf("启动machinery失败%v", err.Error()) logger.Errorf("启动machinery失败%v", err.Error())

View File

@ -2,6 +2,7 @@ package worker
import ( import (
"context" "context"
"errors"
"ferry/pkg/logger" "ferry/pkg/logger"
"os/exec" "os/exec"
"syscall" "syscall"
@ -11,41 +12,41 @@ import (
var asyncTaskMap map[string]interface{} var asyncTaskMap map[string]interface{}
func executeTaskBase(scriptPath string) { func executeTaskBase(scriptPath string, params string) (err error) {
command := exec.Command("/bin/bash", "-c", scriptPath) //初始化Cmd command := exec.Command(scriptPath, params) //初始化Cmd
err := command.Start() //运行脚本 out, err := command.CombinedOutput()
if nil != err { if err != nil {
logger.Errorf("task exec failed%v", err.Error()) logger.Errorf("task exec failed%v", err.Error())
return return
} }
logger.Info("Output: ", string(out))
logger.Info("Process PID:", command.Process.Pid) logger.Info("ProcessState PID: ", command.ProcessState.Pid())
logger.Info("Exit Code ", command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus())
err = command.Wait() //等待执行完成 return
if nil != err {
logger.Errorf("task exec failed%v", err.Error())
return
}
logger.Info("ProcessState PID:", command.ProcessState.Pid())
logger.Info("Exit Code", command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus())
} }
// ExecCommand 异步任务 // ExecCommand 异步任务
func ExecCommand(classify string, scriptPath string) error { func ExecCommand(classify string, scriptPath string, params string) (err error) {
if classify == "shell" { if classify == "shell" {
logger.Info("start exec shell...", scriptPath) logger.Info("start exec shell - ", scriptPath)
executeTaskBase(scriptPath) err = executeTaskBase(scriptPath, params)
return nil if err != nil {
return
}
} else if classify == "python" { } else if classify == "python" {
logger.Info("start exec python...", scriptPath) logger.Info("start exec python - ", scriptPath)
executeTaskBase(scriptPath) err = executeTaskBase(scriptPath, params)
return nil if err != nil {
return
}
} else {
err = errors.New("目前仅支持Python与Shell脚本的执行请知悉。")
return
} }
return nil return
} }
func SendTask(ctx context.Context, classify string, scriptPath string) { func SendTask(ctx context.Context, classify string, scriptPath string, params string) {
args := make([]tasks.Arg, 0) args := make([]tasks.Arg, 0)
args = append(args, tasks.Arg{ args = append(args, tasks.Arg{
Name: "classify", Name: "classify",
@ -57,6 +58,11 @@ func SendTask(ctx context.Context, classify string, scriptPath string) {
Type: "string", Type: "string",
Value: scriptPath, Value: scriptPath,
}) })
args = append(args, tasks.Arg{
Name: "params",
Type: "string",
Value: params,
})
task, _ := tasks.NewSignature("ExecCommandTask", args) task, _ := tasks.NewSignature("ExecCommandTask", args)
task.RetryCount = 5 task.RetryCount = 5
_, err := AsyncTaskCenter.SendTaskWithContext(ctx, task) _, err := AsyncTaskCenter.SendTaskWithContext(ctx, task)