MCPcopy Create free account
hub / github.com/devfeel/dottask / doCronTask

Function doCronTask

crontask.go:153–221  ·  view source on GitHub ↗
(task *CronTask)

Source from the content-addressed store, hash-verified

151}
152
153func doCronTask(task *CronTask) {
154 taskCtx := task.getTaskContext()
155 defer func() {
156 if taskCtx.TimeoutCancel != nil {
157 taskCtx.TimeoutCancel()
158 }
159 task.putTaskContext(taskCtx)
160 if err := recover(); err != nil {
161 task.CounterInfo().ErrorCounter.Inc(1)
162 task.taskService.Logger().Debug(fmt.Sprint(task.TaskID(), " cron handler recover error => ", err))
163 if task.taskService.ExceptionHandler != nil {
164 task.taskService.ExceptionHandler(taskCtx, fmt.Errorf("%v", err))
165 }
166 //goroutine panic, restart cron task
167 startCronTask(task)
168 task.taskService.Logger().Debug(fmt.Sprint(task.TaskID(), " goroutine panic, restart CronTask"))
169 }
170 }()
171
172 handler := func() {
173 defer func() {
174 if task.Timeout > 0 {
175 taskCtx.doneChan <- struct{}{}
176 }
177 }()
178 now := time.Now()
179 if task.time_WeekDay.IsMatch(now) &&
180 task.time_Month.IsMatch(now) &&
181 task.time_Day.IsMatch(now) &&
182 task.time_Hour.IsMatch(now) &&
183 task.time_Minute.IsMatch(now) &&
184 task.time_Second.IsMatch(now) {
185
186 //inc run counter
187 task.CounterInfo().RunCounter.Inc(1)
188 //do log
189 if task.taskService != nil && task.taskService.OnBeforeHandler != nil {
190 task.taskService.OnBeforeHandler(taskCtx)
191 }
192 var err error
193 if !taskCtx.IsEnd {
194 err = task.handler(taskCtx)
195 }
196 if err != nil {
197 taskCtx.Error = err
198 task.CounterInfo().ErrorCounter.Inc(1)
199 if task.taskService != nil && task.taskService.ExceptionHandler != nil {
200 task.taskService.ExceptionHandler(taskCtx, err)
201 }
202 }
203 if task.taskService != nil && task.taskService.OnEndHandler != nil {
204 task.taskService.OnEndHandler(taskCtx)
205 }
206 }
207 }
208
209 if task.Timeout > 0 {
210 go handler()

Callers 1

startCronTaskFunction · 0.85

Calls 9

startCronTaskFunction · 0.85
getTaskContextMethod · 0.80
putTaskContextMethod · 0.80
LoggerMethod · 0.80
IsMatchMethod · 0.80
IncMethod · 0.65
CounterInfoMethod · 0.65
DebugMethod · 0.65
TaskIDMethod · 0.65

Tested by

no test coverage detected