代码拉取完成,页面将自动刷新
package worker
import (
"context"
"errors"
"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/gin/log"
"github.com/RichardKnop/machinery/v1/tasks"
"os/exec"
"syscall"
)
var asyncTaskMap map[string]interface{}
func executeTaskBase(scriptPath string, params string) (err error) {
command := exec.Command(scriptPath, params) //初始化Cmd
out, err := command.CombinedOutput()
if err != nil {
log.Error("task exec failed,%v", err.Error())
return
}
log.Info("Output: ", string(out))
log.Info("ProcessState PID: ", command.ProcessState.Pid())
log.Info("Exit Code ", command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus())
return
}
// ExecCommand 异步任务
func ExecCommand(classify string, scriptPath string, params string) (err error) {
if classify == "shell" {
log.Info("start exec shell - ", scriptPath)
err = executeTaskBase(scriptPath, params)
if err != nil {
return
}
} else if classify == "python" {
log.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, params string) {
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,
})
args = append(args, tasks.Arg{
Name: "params",
Type: "string",
Value: params,
})
task, _ := tasks.NewSignature("ExecCommandTask", args)
task.RetryCount = 5
_, err := AsyncTaskCenter.SendTaskWithContext(ctx, task)
if err != nil {
log.Error(err.Error())
}
}
func initAsyncTaskMap() {
asyncTaskMap = make(map[string]interface{})
asyncTaskMap["ExecCommandTask"] = ExecCommand
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。