Skip to content

Commit

Permalink
修复一秒内重复执行
Browse files Browse the repository at this point in the history
  • Loading branch information
iwannay committed Aug 6, 2019
1 parent e6974e8 commit f061c98
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
18 changes: 12 additions & 6 deletions jiacrontabd/jiacrontabd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (j *Jiacrontabd) removeTmpJob(job *JobEntry) {
j.mux.Unlock()
}

func (j *Jiacrontabd) addJob(job *crontab.Job) {
func (j *Jiacrontabd) addJob(job *crontab.Job, updateLastExecTime bool) {
j.mux.Lock()
if v, ok := j.jobs[job.ID]; ok {
v.job = job
Expand All @@ -81,11 +81,17 @@ func (j *Jiacrontabd) addJob(job *crontab.Job) {
if err := j.crontab.AddJob(job); err != nil {
log.Error("NextExecutionTime:", err, " timeArgs:", job)
} else {
data := map[string]interface{}{
"next_exec_time": job.GetNextExecTime(),
"status": models.StatusJobTiming,
}

if updateLastExecTime {
data["last_exec_time"] = time.Now()
}

if err := models.DB().Model(&models.CrontabJob{}).Where("id=?", job.ID).
Updates(map[string]interface{}{
"next_exec_time": job.GetNextExecTime(),
"status": models.StatusJobTiming,
}).Error; err != nil {
Updates(data).Error; err != nil {
log.Error(err)
}
}
Expand Down Expand Up @@ -411,7 +417,7 @@ func (j *Jiacrontabd) recovery() {
Day: v.TimeArgs.Day,
Month: v.TimeArgs.Month,
Weekday: v.TimeArgs.Weekday,
})
}, false)
}

err = models.DB().Find(&daemonJobs, "status in (?)", []models.JobStatus{models.StatusJobOk}).Error
Expand Down
16 changes: 3 additions & 13 deletions jiacrontabd/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,14 @@ func (j *JobEntry) exec() {
Day: j.detail.TimeArgs.Day,
Month: j.detail.TimeArgs.Month,
Weekday: j.detail.TimeArgs.Weekday,
})
}, false)
return
}
j.jd.addJob(j.job)
j.jd.addJob(j.job, true)
}

if atomic.LoadInt32(&j.processNum) >= int32(j.detail.MaxConcurrent) && j.detail.MaxConcurrent != 0 {
j.logContent = []byte("不得超过job最大并发数量")
j.logContent = []byte("不得超过job最大并发数量\n")
return
}

Expand Down Expand Up @@ -475,7 +475,6 @@ func (j *JobEntry) updateJob(status models.JobStatus, startTime, endTime time.Ti
data := map[string]interface{}{
"status": status,
"process_num": atomic.LoadInt32(&j.processNum),
"last_exec_time": j.job.GetLastExecTime(),
"last_exit_status": "",
"failed": false,
}
Expand All @@ -484,14 +483,6 @@ func (j *JobEntry) updateJob(status models.JobStatus, startTime, endTime time.Ti
data["last_cost_time"] = endTime.Sub(startTime).Seconds()
}

// if j.once && (status == models.StatusJobRunning) {
// data["process_num"] = gorm.Expr("process_num + ?", 1)
// }

// if j.once && (status == models.StatusJobTiming) {
// data["process_num"] = gorm.Expr("process_num - ?", 1)
// }

var errMsg string
if err != nil {
errMsg = err.Error()
Expand All @@ -500,7 +491,6 @@ func (j *JobEntry) updateJob(status models.JobStatus, startTime, endTime time.Ti
}

if j.once {
delete(data, "last_exec_time")
delete(data, "status")
delete(data, "last_exit_status")
}
Expand Down
2 changes: 1 addition & 1 deletion jiacrontabd/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (j *CrontabJob) Start(args proto.ActionJobsArgs, jobs *[]models.CrontabJob)
Day: v.TimeArgs.Day,
Month: v.TimeArgs.Month,
Weekday: v.TimeArgs.Weekday,
})
}, false)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/ver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime"
)

const Binary = "2.0.1"
const Binary = "2.0.2"

func String(app string) string {
return fmt.Sprintf("%s v%s (built w/%s)", app, Binary, runtime.Version())
Expand Down

0 comments on commit f061c98

Please sign in to comment.