diff --git a/README.md b/README.md index a2357e5..067e1c2 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/apis/process/workOrder.go b/apis/process/workOrder.go index 932967a..cbb9aa3 100644 --- a/apis/process/workOrder.go +++ b/apis/process/workOrder.go @@ -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, "", "成功提交工单申请") diff --git a/pkg/service/handle.go b/pkg/service/handle.go index 433080e..1f6e5a2 100644 --- a/pkg/service/handle.go +++ b/pkg/service/handle.go @@ -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 } diff --git a/pkg/service/task.go b/pkg/service/task.go index e0c2cce..bdc3f11 100644 --- a/pkg/service/task.go +++ b/pkg/service/task.go @@ -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) } } } diff --git a/pkg/task/send.go b/pkg/task/send.go index 7902f78..7b24aa0 100644 --- a/pkg/task/send.go +++ b/pkg/task/send.go @@ -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) } diff --git a/pkg/task/server.go b/pkg/task/server.go index 3a12191..fb77d9c 100644 --- a/pkg/task/server.go +++ b/pkg/task/server.go @@ -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()) diff --git a/pkg/task/worker/tasks.go b/pkg/task/worker/tasks.go index 42cdcf5..9ebabe4 100644 --- a/pkg/task/worker/tasks.go +++ b/pkg/task/worker/tasks.go @@ -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()) - return - } - - logger.Info("ProcessState PID:", command.ProcessState.Pid()) - logger.Info("Exit Code", command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()) + logger.Info("Output: ", string(out)) + logger.Info("ProcessState PID: ", command.ProcessState.Pid()) + logger.Info("Exit Code ", command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()) + return } // 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 + logger.Info("start exec shell - ", scriptPath) + err = executeTaskBase(scriptPath, params) + if err != nil { + return + } } else if classify == "python" { - logger.Info("start exec python...", scriptPath) - executeTaskBase(scriptPath) - return nil + logger.Info("start exec python - ", scriptPath) + err = executeTaskBase(scriptPath, params) + 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 = 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)