2020-07-15 01:40:56 +08:00
|
|
|
|
package worker
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-09-20 14:05:32 +08:00
|
|
|
|
"errors"
|
2020-08-15 23:34:37 +08:00
|
|
|
|
"ferry/pkg/logger"
|
2020-07-15 01:40:56 +08:00
|
|
|
|
"os/exec"
|
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
|
|
"github.com/RichardKnop/machinery/v1/tasks"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var asyncTaskMap map[string]interface{}
|
|
|
|
|
|
2020-09-20 14:05:32 +08:00
|
|
|
|
func executeTaskBase(scriptPath string, params string) (err error) {
|
|
|
|
|
command := exec.Command(scriptPath, params) //初始化Cmd
|
|
|
|
|
out, err := command.CombinedOutput()
|
|
|
|
|
if err != nil {
|
2020-08-15 23:34:37 +08:00
|
|
|
|
logger.Errorf("task exec failed,%v", err.Error())
|
2020-07-15 01:40:56 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-20 14:05:32 +08:00
|
|
|
|
logger.Info("Output: ", string(out))
|
|
|
|
|
logger.Info("ProcessState PID: ", command.ProcessState.Pid())
|
|
|
|
|
logger.Info("Exit Code ", command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus())
|
|
|
|
|
return
|
2020-07-15 01:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecCommand 异步任务
|
2020-09-20 14:05:32 +08:00
|
|
|
|
func ExecCommand(classify string, scriptPath string, params string) (err error) {
|
2020-07-15 01:40:56 +08:00
|
|
|
|
if classify == "shell" {
|
2020-09-20 14:05:32 +08:00
|
|
|
|
logger.Info("start exec shell - ", scriptPath)
|
|
|
|
|
err = executeTaskBase(scriptPath, params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-07-15 01:40:56 +08:00
|
|
|
|
} else if classify == "python" {
|
2020-09-20 14:05:32 +08:00
|
|
|
|
logger.Info("start exec python - ", scriptPath)
|
|
|
|
|
err = executeTaskBase(scriptPath, params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
err = errors.New("目前仅支持Python与Shell脚本的执行,请知悉。")
|
|
|
|
|
return
|
2020-07-15 01:40:56 +08:00
|
|
|
|
}
|
2020-09-20 14:05:32 +08:00
|
|
|
|
return
|
2020-07-15 01:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 14:05:32 +08:00
|
|
|
|
func SendTask(ctx context.Context, classify string, scriptPath string, params string) {
|
2020-07-15 01:40:56 +08:00
|
|
|
|
args := make([]tasks.Arg, 0)
|
|
|
|
|
args = append(args, tasks.Arg{
|
|
|
|
|
Name: "classify",
|
|
|
|
|
Type: "string",
|
|
|
|
|
Value: classify,
|
|
|
|
|
})
|
|
|
|
|
args = append(args, tasks.Arg{
|
|
|
|
|
Name: "scriptPath",
|
|
|
|
|
Type: "string",
|
|
|
|
|
Value: scriptPath,
|
|
|
|
|
})
|
2020-09-20 14:05:32 +08:00
|
|
|
|
args = append(args, tasks.Arg{
|
|
|
|
|
Name: "params",
|
|
|
|
|
Type: "string",
|
|
|
|
|
Value: params,
|
|
|
|
|
})
|
2020-07-15 01:40:56 +08:00
|
|
|
|
task, _ := tasks.NewSignature("ExecCommandTask", args)
|
|
|
|
|
task.RetryCount = 5
|
|
|
|
|
_, err := AsyncTaskCenter.SendTaskWithContext(ctx, task)
|
|
|
|
|
if err != nil {
|
2020-08-15 23:34:37 +08:00
|
|
|
|
logger.Error(err.Error())
|
2020-07-15 01:40:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initAsyncTaskMap() {
|
|
|
|
|
asyncTaskMap = make(map[string]interface{})
|
|
|
|
|
asyncTaskMap["ExecCommandTask"] = ExecCommand
|
|
|
|
|
}
|