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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
4 deletions
master/JobMgr.go
worker/Executor.go
master/JobMgr.go
View file @
bd963b0a
...
@@ -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
)
...
...
worker/Executor.go
View file @
bd963b0a
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,6 +56,11 @@ func (executor *Executor) ExecuteJob(info *common.JobExecuteInfo) {
...
@@ -54,6 +56,11 @@ func (executor *Executor) ExecuteJob(info *common.JobExecuteInfo) {
// 上锁成功后,重置任务启动时间
// 上锁成功后,重置任务启动时间
result
.
StartTime
=
time
.
Now
()
result
.
StartTime
=
time
.
Now
()
if
ok
:=
strings
.
HasSuffix
(
info
.
Job
.
Command
,
".sh"
);
!
ok
{
result
.
EndTime
=
time
.
Now
()
result
.
Output
=
[]
byte
(
"该命令不是以.sh结尾的文件,无法执行"
)
result
.
Err
=
errors
.
New
(
"该命令不是以.sh结尾的文件,无法执行"
)
}
else
{
// 执行shell命令
// 执行shell命令
cmd
=
exec
.
CommandContext
(
info
.
CancelCtx
,
G_config
.
ExecCommand
,
"-c"
,
info
.
Job
.
Command
)
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)
//cmd = exec.CommandContext(info.CancelCtx, "C:\\cygwin64\\bin\\bash.exe", "-c", info.Job.Command)
...
@@ -66,6 +73,9 @@ func (executor *Executor) ExecuteJob(info *common.JobExecuteInfo) {
...
@@ -66,6 +73,9 @@ func (executor *Executor) ExecuteJob(info *common.JobExecuteInfo) {
result
.
Output
=
output
result
.
Output
=
output
result
.
Err
=
err
result
.
Err
=
err
}
}
}
// 任务执行完成后,把执行的结果返回给Scheduler,Scheduler会从executingTable中删除掉执行记录
// 任务执行完成后,把执行的结果返回给Scheduler,Scheduler会从executingTable中删除掉执行记录
G_scheduler
.
PushJobResult
(
result
)
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