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

45
job.go
View File

@ -118,6 +118,30 @@ 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++ {
err := j.runTry(log, jobLogFile)
if err == nil {
break
}
if j.JobConfig.RestartRule == No || j.JobConfig.NumberOfRestartAttemts == 0 {
break
}
if i == 0 {
log.Printf("Job failed, restarting in %d seconds.", j.JobConfig.RestartSec)
j.Status = Restarting
} else if i < j.JobConfig.NumberOfRestartAttemts {
j.Status = Restarting
log.Printf("Retry attempt №%d of %d failed, restarting in %d seconds.", i, j.JobConfig.NumberOfRestartAttemts, j.JobConfig.RestartSec)
} else {
log.Printf("Retry attempt №%d of %d failed.", i, j.JobConfig.NumberOfRestartAttemts)
}
time.Sleep(time.Duration(j.JobConfig.RestartSec) * time.Second)
}
}
func (j *Job) runTry(log *log.Entry, jobLogFile *os.File) error {
log.Info("Started.") log.Info("Started.")
startTime := time.Now() startTime := time.Now()
@ -157,26 +181,7 @@ func (j *Job) Run() {
j.LastExecutionDuration = endTime.Sub(startTime).Truncate(time.Second).String() j.LastExecutionDuration = endTime.Sub(startTime).Truncate(time.Second).String()
globalMutex.Unlock() globalMutex.Unlock()
if err == nil { return err
break
}
if j.JobConfig.RestartRule == No || j.JobConfig.NumberOfRestartAttemts == 0 {
break
}
if i == 0 {
log.Printf("Job failed, restarting in %d seconds.", j.JobConfig.RestartSec)
j.Status = Restarting
} else if i < j.JobConfig.NumberOfRestartAttemts {
j.Status = Restarting
log.Printf("Retry attempt №%d of %d failed, restarting in %d seconds.", i, j.JobConfig.NumberOfRestartAttemts, j.JobConfig.RestartSec)
} else {
log.Printf("Retry attempt №%d of %d failed.", i, j.JobConfig.NumberOfRestartAttemts)
}
time.Sleep(time.Duration(j.JobConfig.RestartSec) * time.Second)
}
} }
func (j *Job) runCmd(jobLogFile *os.File) error { func (j *Job) runCmd(jobLogFile *os.File) error {