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)
账号密码admin/123456
```
账号admin
密码123456
演示demo登陆需要取消ldap验证就是登陆页面取消ldap的打勾。
```
文档: [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"`
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)
@ -248,7 +254,18 @@ func CreateWorkOrder(c *gin.Context) {
return
}
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, "", "成功提交工单申请")

View File

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

View File

@ -12,13 +12,13 @@ import (
@Author : lanyulei
*/
func ExecTask(taskList []string) {
func ExecTask(taskList []string, params string) {
for _, taskName := range taskList {
filePath := fmt.Sprintf("%v/%v", viper.GetString("script.path"), taskName)
if strings.HasSuffix(filePath, ".py") {
task.Send("python", filePath)
task.Send("python", filePath, params)
} 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"
)
func Send(classify string, scriptPath string) {
worker.SendTask(context.Background(), classify, scriptPath)
func Send(classify string, scriptPath string, params string) {
worker.SendTask(context.Background(), classify, scriptPath, params)
}

View File

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

View File

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