Fixed job update bug

issue#16
pull/19/head
miraclesu 2017-06-13 16:00:42 +08:00
parent e608185a96
commit 93832d66a8
4 changed files with 35 additions and 26 deletions

View File

@ -36,7 +36,7 @@ The goal of this project is to make it much easier to manage jobs on lots of mac
## Security
`cronsun` support security with `security.json` config. When `opne=true` job command is only allow local files with special extension on the node.
`cronsun` support security with `security.json` config. When `open=true` job command is only allow local files with special extension on the node.
```json
{

View File

@ -193,7 +193,7 @@ func (n *Node) modJob(job *cronsun.Job) {
cmds := oJob.Cmds(n.ID, n.groups)
for id, cmd := range cmds {
n.addCmd(cmd, true)
n.modCmd(cmd, true)
delete(prevCmds, id)
}
@ -205,34 +205,40 @@ func (n *Node) modJob(job *cronsun.Job) {
}
func (n *Node) addCmd(cmd *cronsun.Cmd, notice bool) {
c, ok := n.cmds[cmd.GetID()]
if ok {
sch := c.JobRule.Timer
*c = *cmd
n.Cron.Schedule(cmd.JobRule.Schedule, cmd)
n.cmds[cmd.GetID()] = cmd
// 节点执行时间不变,不用更新 cron
if c.JobRule.Timer == sch {
return
}
} else {
c = cmd
if notice {
log.Infof("job[%s] group[%s] rule[%s] timer[%s] has added", cmd.Job.ID, cmd.Job.Group, cmd.JobRule.ID, cmd.JobRule.Timer)
}
return
}
func (n *Node) modCmd(cmd *cronsun.Cmd, notice bool) {
c, ok := n.cmds[cmd.GetID()]
if !ok {
n.addCmd(cmd, notice)
return
}
n.Cron.Schedule(c.JobRule.Schedule, c)
if !ok {
n.cmds[c.GetID()] = c
sch := c.JobRule.Timer
*c = *cmd
// 节点执行时间改变,更新 cron
// 否则不用更新 cron
if c.JobRule.Timer != sch {
n.Cron.Schedule(c.JobRule.Schedule, c)
}
if notice {
log.Infof("job[%s] rule[%s] timer[%s] has added", c.Job.ID, c.JobRule.ID, c.JobRule.Timer)
log.Infof("job[%s] group[%s] rule[%s] timer[%s] has updated", c.Job.ID, c.Job.Group, c.JobRule.ID, c.JobRule.Timer)
}
return
}
func (n *Node) delCmd(cmd *cronsun.Cmd) {
delete(n.cmds, cmd.GetID())
n.Cron.DelJob(cmd)
log.Infof("job[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.JobRule.ID, cmd.JobRule.Timer)
log.Infof("job[%s] group[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.Job.Group, cmd.JobRule.ID, cmd.JobRule.Timer)
}
func (n *Node) addGroup(g *cronsun.Group) {

View File

@ -5,7 +5,7 @@ import (
"runtime"
)
const Binary = "v0.1.1"
const Binary = "v0.1.2"
var (
Version = fmt.Sprintf("%s (build %s)", Binary, runtime.Version())

View File

@ -115,19 +115,22 @@ func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
return
}
// remove old key
// it should be before the put method
if len(deleteOldKey) > 0 {
if _, err = cronsun.DefalutClient.Delete(deleteOldKey); err != nil {
log.Errorf("failed to remove old job key[%s], err: %s.", deleteOldKey, err.Error())
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
}
}
_, err = cronsun.DefalutClient.Put(job.Key(), string(b))
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
}
// remove old key
if len(deleteOldKey) > 0 {
if _, err = cronsun.DefalutClient.Delete(deleteOldKey); err != nil {
log.Errorf("failed to remove old job key[%s], err: %s.", deleteOldKey, err.Error())
}
}
outJSONWithCode(w, successCode, nil)
}