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
15fbcc7b
authored
Jan 16, 2020
by
朱继来
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
temp1
parent
5d1a96f5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
13 deletions
common/Protocol.go
master/ApiServer.go
master/JobMgr.go
common/Protocol.go
View file @
15fbcc7b
...
@@ -13,6 +13,7 @@ type Job struct {
...
@@ -13,6 +13,7 @@ type Job struct {
Name
string
`json:"name"`
// 任务名
Name
string
`json:"name"`
// 任务名
Command
string
`json:"command"`
// shell命令
Command
string
`json:"command"`
// shell命令
CronExpr
string
`json:"cronExpr"`
// cron表达式
CronExpr
string
`json:"cronExpr"`
// cron表达式
NodeIp
string
`json:"nodeIp"`
}
}
// 任务调度计划
// 任务调度计划
...
...
master/ApiServer.go
View file @
15fbcc7b
package
master
package
master
import
(
import
(
"net/http"
_
"encoding/json"
_
"fmt"
"go-crontab/common"
"net"
"net"
"
time
"
"
net/http
"
"strconv"
"strconv"
"go-crontab/common"
"time"
"encoding/json"
)
)
// 任务的HTTP接口
// 任务的HTTP接口
...
@@ -24,7 +25,7 @@ var (
...
@@ -24,7 +25,7 @@ var (
func
handleJobSave
(
resp
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
func
handleJobSave
(
resp
http
.
ResponseWriter
,
req
*
http
.
Request
)
{
var
(
var
(
err
error
err
error
postJob
string
//
postJob string
job
common
.
Job
job
common
.
Job
oldJob
*
common
.
Job
oldJob
*
common
.
Job
bytes
[]
byte
bytes
[]
byte
...
@@ -35,16 +36,25 @@ func handleJobSave(resp http.ResponseWriter, req *http.Request) {
...
@@ -35,16 +36,25 @@ func handleJobSave(resp http.ResponseWriter, req *http.Request) {
goto
ERR
goto
ERR
}
}
// 2, 取表单中的job字段
//// 2, 取表单中的job字段
postJob
=
req
.
PostForm
.
Get
(
"job"
)
//postJob = req.PostForm.Get("job_name")
// 3, 反序列化job
//
if
err
=
json
.
Unmarshal
([]
byte
(
postJob
),
&
job
);
err
!=
nil
{
//// 3, 反序列化job
goto
ERR
//if err = json.Unmarshal([]byte(postJob), &job); err != nil {
}
// goto ERR
//}
// 调整2、3步骤,直接赋值给job
job
.
Name
=
req
.
FormValue
(
"job_name"
);
job
.
Command
=
req
.
FormValue
(
"command"
);
job
.
CronExpr
=
req
.
FormValue
(
"cron_expr"
);
job
.
NodeIp
=
req
.
FormValue
(
"node_ip"
);
// 4, 保存到etcd
// 4, 保存到etcd
if
oldJob
,
err
=
G_jobMgr
.
SaveJob
(
&
job
);
err
!=
nil
{
if
oldJob
,
err
=
G_jobMgr
.
SaveJob
(
&
job
);
err
!=
nil
{
goto
ERR
goto
ERR
}
}
// 5, 返回正常应答 ({"errno": 0, "msg": "", "data": {....}})
// 5, 返回正常应答 ({"errno": 0, "msg": "", "data": {....}})
if
bytes
,
err
=
common
.
BuildResponse
(
0
,
"success"
,
oldJob
);
err
==
nil
{
if
bytes
,
err
=
common
.
BuildResponse
(
0
,
"success"
,
oldJob
);
err
==
nil
{
resp
.
Write
(
bytes
)
resp
.
Write
(
bytes
)
...
@@ -52,7 +62,7 @@ func handleJobSave(resp http.ResponseWriter, req *http.Request) {
...
@@ -52,7 +62,7 @@ func handleJobSave(resp http.ResponseWriter, req *http.Request) {
return
return
ERR
:
ERR
:
// 6, 返回异常应答
// 6, 返回异常应答
if
bytes
,
err
=
common
.
BuildResponse
(
-
1
,
req
.
PostForm
.
Get
(
"job_name"
),
nil
);
err
==
nil
{
if
bytes
,
err
=
common
.
BuildResponse
(
-
1
,
err
.
Error
(
),
nil
);
err
==
nil
{
resp
.
Write
(
bytes
)
resp
.
Write
(
bytes
)
}
}
}
}
...
...
master/JobMgr.go
View file @
15fbcc7b
...
@@ -65,7 +65,7 @@ func (jobMgr *JobMgr) SaveJob(job *common.Job) (oldJob *common.Job, err error) {
...
@@ -65,7 +65,7 @@ func (jobMgr *JobMgr) SaveJob(job *common.Job) (oldJob *common.Job, err error) {
)
)
// etcd的保存key
// etcd的保存key
jobKey
=
common
.
JOB_SAVE_DIR
+
job
.
Name
jobKey
=
common
.
JOB_SAVE_DIR
+
job
.
N
odeIp
+
"/"
+
job
.
N
ame
// 任务信息json
// 任务信息json
if
jobValue
,
err
=
json
.
Marshal
(
job
);
err
!=
nil
{
if
jobValue
,
err
=
json
.
Marshal
(
job
);
err
!=
nil
{
return
return
...
...
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