2 Star 0 Fork 0

TeamsHub/backend-gopkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tasks.go 1.85 KB
一键复制 编辑 原始数据 按行查看 历史
HCY 提交于 2024-05-10 13:07 +08:00 . edit pkg
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzheng0709/backend-gopkg.git
git@gitee.com:wuzheng0709/backend-gopkg.git
wuzheng0709
backend-gopkg
backend-gopkg
v1.5.24

搜索帮助