mirror of
https://github.com/nxshock/gron.git
synced 2024-11-27 03:41:00 +05:00
Move run tries to separate func
This commit is contained in:
parent
828fed25c8
commit
c08aadda4f
83
job.go
83
job.go
@ -118,45 +118,7 @@ func (j *Job) Run() {
|
||||
defer jobLogFile.Close()
|
||||
|
||||
for i := 0; i < j.JobConfig.NumberOfRestartAttemts+1; i++ {
|
||||
log.Info("Started.")
|
||||
startTime := time.Now()
|
||||
|
||||
globalMutex.Lock()
|
||||
j.CurrentRunningCount++
|
||||
j.Status = Running
|
||||
j.LastStartTime = startTime.Format(config.TimeFormat)
|
||||
globalMutex.Unlock()
|
||||
|
||||
var err error
|
||||
switch j.JobConfig.Type {
|
||||
case Cmd:
|
||||
err = j.runCmd(jobLogFile)
|
||||
case Sql:
|
||||
err = j.runSql(jobLogFile)
|
||||
}
|
||||
if err != nil {
|
||||
j.Status = Error
|
||||
log.Error(err.Error())
|
||||
|
||||
globalMutex.Lock()
|
||||
j.LastError = err.Error()
|
||||
globalMutex.Unlock()
|
||||
} else {
|
||||
j.Status = Inactive
|
||||
globalMutex.Lock()
|
||||
j.LastError = ""
|
||||
globalMutex.Unlock()
|
||||
}
|
||||
|
||||
endTime := time.Now()
|
||||
log.Infof("Finished (%s).", endTime.Sub(startTime).Truncate(time.Second).String())
|
||||
|
||||
globalMutex.Lock()
|
||||
j.CurrentRunningCount--
|
||||
j.LastEndTime = endTime.Format(config.TimeFormat)
|
||||
j.LastExecutionDuration = endTime.Sub(startTime).Truncate(time.Second).String()
|
||||
globalMutex.Unlock()
|
||||
|
||||
err := j.runTry(log, jobLogFile)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
@ -179,6 +141,49 @@ func (j *Job) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
func (j *Job) runTry(log *log.Entry, jobLogFile *os.File) error {
|
||||
log.Info("Started.")
|
||||
startTime := time.Now()
|
||||
|
||||
globalMutex.Lock()
|
||||
j.CurrentRunningCount++
|
||||
j.Status = Running
|
||||
j.LastStartTime = startTime.Format(config.TimeFormat)
|
||||
globalMutex.Unlock()
|
||||
|
||||
var err error
|
||||
switch j.JobConfig.Type {
|
||||
case Cmd:
|
||||
err = j.runCmd(jobLogFile)
|
||||
case Sql:
|
||||
err = j.runSql(jobLogFile)
|
||||
}
|
||||
if err != nil {
|
||||
j.Status = Error
|
||||
log.Error(err.Error())
|
||||
|
||||
globalMutex.Lock()
|
||||
j.LastError = err.Error()
|
||||
globalMutex.Unlock()
|
||||
} else {
|
||||
j.Status = Inactive
|
||||
globalMutex.Lock()
|
||||
j.LastError = ""
|
||||
globalMutex.Unlock()
|
||||
}
|
||||
|
||||
endTime := time.Now()
|
||||
log.Infof("Finished (%s).", endTime.Sub(startTime).Truncate(time.Second).String())
|
||||
|
||||
globalMutex.Lock()
|
||||
j.CurrentRunningCount--
|
||||
j.LastEndTime = endTime.Format(config.TimeFormat)
|
||||
j.LastExecutionDuration = endTime.Sub(startTime).Truncate(time.Second).String()
|
||||
globalMutex.Unlock()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (j *Job) runCmd(jobLogFile *os.File) error {
|
||||
command, params := j.commandAndParams()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user