1
0
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:
nxshock 2022-05-10 21:36:34 +05:00
parent 828fed25c8
commit c08aadda4f

83
job.go
View File

@ -118,45 +118,7 @@ func (j *Job) Run() {
defer jobLogFile.Close() defer jobLogFile.Close()
for i := 0; i < j.JobConfig.NumberOfRestartAttemts+1; i++ { for i := 0; i < j.JobConfig.NumberOfRestartAttemts+1; i++ {
log.Info("Started.") err := j.runTry(log, jobLogFile)
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()
if err == nil { if err == nil {
break 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 { func (j *Job) runCmd(jobLogFile *os.File) error {
command, params := j.commandAndParams() command, params := j.commandAndParams()