@@ -140,54 +140,59 @@ func NewCronTask(taskID string, isRun bool, express string, handler TaskHandle,
140140//start cron task
141141func startCronTask (task * CronTask ) {
142142 now := time .Now ()
143- nowsecond := time .Date (now .Year (), now .Month (), now .Day (), now .Hour (), now .Minute (), now .Second (), 0 , time .Local )
144- afterTime := nowsecond .Add (time .Second ).Sub (time .Now ().Local ())
143+ nowSecond := time .Date (now .Year (), now .Month (), now .Day (), now .Hour (), now .Minute (), now .Second (), 0 , time .Local )
144+ afterTime := nowSecond .Add (time .Second ).Sub (time .Now ().Local ())
145145 task .TimeTicker = time .NewTicker (DefaultPeriod )
146146 go func () {
147147 time .Sleep (afterTime )
148148 for {
149149 select {
150150 case <- task .TimeTicker .C :
151- defer func () {
152- task .CounterInfo ().RunCounter .Inc (1 )
153- if err := recover (); err != nil {
154- task .CounterInfo ().ErrorCounter .Inc (1 )
155- task .taskService .Logger ().Debug (task .TaskID , " cron handler recover error => " , err )
156- if task .taskService .ExceptionHandler != nil {
157- task .taskService .ExceptionHandler (task .Context (), fmt .Errorf ("%v" , err ))
158- }
159- //goroutine panic, restart cron task
160- startCronTask (task )
161- task .taskService .Logger ().Debug (task .TaskID , " goroutine panic, restart CronTask" )
162- }
163- }()
164- now := time .Now ()
165- if task .time_WeekDay .IsMatch (now ) &&
166- task .time_Month .IsMatch (now ) &&
167- task .time_Day .IsMatch (now ) &&
168- task .time_Hour .IsMatch (now ) &&
169- task .time_Minute .IsMatch (now ) &&
170- task .time_Second .IsMatch (now ) {
171- //do log
172- //task.taskService.Logger().Debug(task.TaskID, " begin dohandler")
173- if task .taskService != nil && task .taskService .OnBeforeHandler != nil {
174- task .taskService .OnBeforeHandler (task .Context ())
175- }
176- var err error
177- if ! task .Context ().IsEnd {
178- err = task .handler (task .Context ())
179- }
180- if err != nil {
181- task .CounterInfo ().ErrorCounter .Inc (1 )
182- if task .taskService != nil && task .taskService .ExceptionHandler != nil {
183- task .taskService .ExceptionHandler (task .Context (), err )
184- }
185- }
186- if task .taskService != nil && task .taskService .OnEndHandler != nil {
187- task .taskService .OnEndHandler (task .Context ())
188- }
189- }
151+ doCronTask (task )
190152 }
191153 }
192154 }()
193155}
156+
157+ func doCronTask (task * CronTask ) {
158+ defer func () {
159+ if err := recover (); err != nil {
160+ task .CounterInfo ().ErrorCounter .Inc (1 )
161+ task .taskService .Logger ().Debug (task .TaskID , " cron handler recover error => " , err )
162+ if task .taskService .ExceptionHandler != nil {
163+ task .taskService .ExceptionHandler (task .Context (), fmt .Errorf ("%v" , err ))
164+ }
165+ //goroutine panic, restart cron task
166+ startCronTask (task )
167+ task .taskService .Logger ().Debug (task .TaskID , " goroutine panic, restart CronTask" )
168+ }
169+ }()
170+ now := time .Now ()
171+ if task .time_WeekDay .IsMatch (now ) &&
172+ task .time_Month .IsMatch (now ) &&
173+ task .time_Day .IsMatch (now ) &&
174+ task .time_Hour .IsMatch (now ) &&
175+ task .time_Minute .IsMatch (now ) &&
176+ task .time_Second .IsMatch (now ) {
177+
178+ //inc run counter
179+ task .CounterInfo ().RunCounter .Inc (1 )
180+ //do log
181+ if task .taskService != nil && task .taskService .OnBeforeHandler != nil {
182+ task .taskService .OnBeforeHandler (task .Context ())
183+ }
184+ var err error
185+ if ! task .Context ().IsEnd {
186+ err = task .handler (task .Context ())
187+ }
188+ if err != nil {
189+ task .CounterInfo ().ErrorCounter .Inc (1 )
190+ if task .taskService != nil && task .taskService .ExceptionHandler != nil {
191+ task .taskService .ExceptionHandler (task .Context (), err )
192+ }
193+ }
194+ if task .taskService != nil && task .taskService .OnEndHandler != nil {
195+ task .taskService .OnEndHandler (task .Context ())
196+ }
197+ }
198+ }
0 commit comments