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

Function doLoopTask

looptask.go:124–184  ·  view source on GitHub ↗
(task *LoopTask)

Source from the content-addressed store, hash-verified

122}
123
124func doLoopTask(task *LoopTask) {
125 taskCtx := task.getTaskContext()
126 defer func() {
127 if taskCtx.TimeoutCancel != nil {
128 taskCtx.TimeoutCancel()
129 }
130 task.putTaskContext(taskCtx)
131 if err := recover(); err != nil {
132 task.CounterInfo().ErrorCounter.Inc(1)
133 //task.taskService.Logger().Debug(task.TaskID, " loop handler recover error => ", err)
134 if task.taskService.ExceptionHandler != nil {
135 task.taskService.ExceptionHandler(taskCtx, fmt.Errorf("%v", err))
136 }
137 //goroutine panic, restart cron task
138 startLoopTask(task)
139 task.taskService.Logger().Debug(fmt.Sprint(task.TaskID(), " goroutine panic, restart LoopTask"))
140 }
141 }()
142
143 handler := func() {
144 defer func() {
145 if task.Timeout > 0 {
146 taskCtx.doneChan <- struct{}{}
147 }
148 }()
149 //inc run counter
150 task.CounterInfo().RunCounter.Inc(1)
151 //do log
152 if task.taskService != nil && task.taskService.OnBeforeHandler != nil {
153 task.taskService.OnBeforeHandler(taskCtx)
154 }
155 var err error
156 if !taskCtx.IsEnd {
157 err = task.handler(taskCtx)
158 }
159 if err != nil {
160 taskCtx.Error = err
161 task.CounterInfo().ErrorCounter.Inc(1)
162 if task.taskService != nil && task.taskService.ExceptionHandler != nil {
163 task.taskService.ExceptionHandler(taskCtx, err)
164 }
165 } else {
166 //task.taskService.Logger().Debug(task.TaskID, " loop handler end success")
167 }
168 if task.taskService != nil && task.taskService.OnEndHandler != nil {
169 task.taskService.OnEndHandler(taskCtx)
170 }
171 }
172
173 if task.Timeout > 0 {
174 go handler()
175 select {
176 case <-taskCtx.TimeoutContext.Done():
177 task.taskService.Logger().Debug(fmt.Sprint(task.TaskID(), "do handler timeout."))
178 case <-taskCtx.doneChan:
179 return
180 }
181 } else {

Callers 1

startLoopTaskFunction · 0.85

Calls 8

startLoopTaskFunction · 0.85
getTaskContextMethod · 0.80
putTaskContextMethod · 0.80
LoggerMethod · 0.80
IncMethod · 0.65
CounterInfoMethod · 0.65
DebugMethod · 0.65
TaskIDMethod · 0.65

Tested by

no test coverage detected