Commit bd963b0a by 孙龙

up

parent 165d91d0
Showing with 29 additions and 13 deletions
...@@ -3,16 +3,18 @@ package master ...@@ -3,16 +3,18 @@ package master
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
_ "fmt" _ "fmt"
"github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb" "github.com/coreos/etcd/mvcc/mvccpb"
"go-crontab/common" "go-crontab/common"
_"go.mongodb.org/mongo-driver/bson" _ "go.mongodb.org/mongo-driver/bson"
_"go.mongodb.org/mongo-driver/bson/primitive" _ "go.mongodb.org/mongo-driver/bson/primitive"
_"go.mongodb.org/mongo-driver/mongo" _ "go.mongodb.org/mongo-driver/mongo"
_"go.mongodb.org/mongo-driver/mongo/options" _ "go.mongodb.org/mongo-driver/mongo/options"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
) )
...@@ -97,6 +99,10 @@ func (jobMgr *JobMgr) SaveJob(req *http.Request) (oldJob *common.Job, err error) ...@@ -97,6 +99,10 @@ func (jobMgr *JobMgr) SaveJob(req *http.Request) (oldJob *common.Job, err error)
job.CronExpr = req.FormValue("cron_expr") job.CronExpr = req.FormValue("cron_expr")
nodeIp := req.FormValue("node") nodeIp := req.FormValue("node")
if ok :=strings.HasSuffix(job.Command, ".sh");!ok{
return nil,errors.New("执行命令必须以.sh结尾")
}
// 任务类型:1-普通任务,2-一次性任务 // 任务类型:1-普通任务,2-一次性任务
job_type, err := strconv.ParseInt(req.FormValue("job_type"), 10, 64) job_type, err := strconv.ParseInt(req.FormValue("job_type"), 10, 64)
......
package worker package worker
import ( import (
"errors"
"go-crontab/common" "go-crontab/common"
"math/rand" "math/rand"
"os/exec" "os/exec"
"strings"
"time" "time"
) )
...@@ -54,17 +56,25 @@ func (executor *Executor) ExecuteJob(info *common.JobExecuteInfo) { ...@@ -54,17 +56,25 @@ func (executor *Executor) ExecuteJob(info *common.JobExecuteInfo) {
// 上锁成功后,重置任务启动时间 // 上锁成功后,重置任务启动时间
result.StartTime = time.Now() result.StartTime = time.Now()
// 执行shell命令 if ok :=strings.HasSuffix(info.Job.Command, ".sh");!ok{
cmd = exec.CommandContext(info.CancelCtx, G_config.ExecCommand, "-c", info.Job.Command) result.EndTime = time.Now()
//cmd = exec.CommandContext(info.CancelCtx, "C:\\cygwin64\\bin\\bash.exe", "-c", info.Job.Command) result.Output = []byte("该命令不是以.sh结尾的文件,无法执行")
result.Err = errors.New("该命令不是以.sh结尾的文件,无法执行")
}else{
// 执行shell命令
cmd = exec.CommandContext(info.CancelCtx, G_config.ExecCommand, "-c", info.Job.Command)
//cmd = exec.CommandContext(info.CancelCtx, "C:\\cygwin64\\bin\\bash.exe", "-c", info.Job.Command)
// 执行并捕获输出
output, err = cmd.CombinedOutput()
// 记录任务结束时间
result.EndTime = time.Now()
result.Output = output
result.Err = err
}
// 执行并捕获输出
output, err = cmd.CombinedOutput()
// 记录任务结束时间
result.EndTime = time.Now()
result.Output = output
result.Err = err
} }
// 任务执行完成后,把执行的结果返回给Scheduler,Scheduler会从executingTable中删除掉执行记录 // 任务执行完成后,把执行的结果返回给Scheduler,Scheduler会从executingTable中删除掉执行记录
G_scheduler.PushJobResult(result) G_scheduler.PushJobResult(result)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment