Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
孙龙
/
go-crontab
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bd963b0a
authored
Aug 26, 2020
by
孙龙
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
up
parent
165d91d0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
13 deletions
master/JobMgr.go
worker/Executor.go
master/JobMgr.go
View file @
bd963b0a
...
...
@@ -3,16 +3,18 @@ package master
import
(
"context"
"encoding/json"
"errors"
_
"fmt"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"go-crontab/common"
_
"go.mongodb.org/mongo-driver/bson"
_
"go.mongodb.org/mongo-driver/bson/primitive"
_
"go.mongodb.org/mongo-driver/mongo"
_
"go.mongodb.org/mongo-driver/mongo/options"
_
"go.mongodb.org/mongo-driver/bson"
_
"go.mongodb.org/mongo-driver/bson/primitive"
_
"go.mongodb.org/mongo-driver/mongo"
_
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
"strconv"
"strings"
"time"
)
...
...
@@ -97,6 +99,10 @@ func (jobMgr *JobMgr) SaveJob(req *http.Request) (oldJob *common.Job, err error)
job
.
CronExpr
=
req
.
FormValue
(
"cron_expr"
)
nodeIp
:=
req
.
FormValue
(
"node"
)
if
ok
:=
strings
.
HasSuffix
(
job
.
Command
,
".sh"
);
!
ok
{
return
nil
,
errors
.
New
(
"执行命令必须以.sh结尾"
)
}
// 任务类型:1-普通任务,2-一次性任务
job_type
,
err
:=
strconv
.
ParseInt
(
req
.
FormValue
(
"job_type"
),
10
,
64
)
...
...
worker/Executor.go
View file @
bd963b0a
package
worker
import
(
"errors"
"go-crontab/common"
"math/rand"
"os/exec"
"strings"
"time"
)
...
...
@@ -54,17 +56,25 @@ func (executor *Executor) ExecuteJob(info *common.JobExecuteInfo) {
// 上锁成功后,重置任务启动时间
result
.
StartTime
=
time
.
Now
()
// 执行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)
if
ok
:=
strings
.
HasSuffix
(
info
.
Job
.
Command
,
".sh"
);
!
ok
{
result
.
EndTime
=
time
.
Now
()
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中删除掉执行记录
G_scheduler
.
PushJobResult
(
result
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment