MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / DefaultStartTask

Function DefaultStartTask

pkg/task/task.go:152–216  ·  view source on GitHub ↗

DefaultStartTask is the default TaskStarter.

(conf *Config)

Source from the content-addressed store, hash-verified

150
151// DefaultStartTask is the default TaskStarter.
152func DefaultStartTask(conf *Config) {
153 logger := log.FromContext(conf.Context).WithField("task_id", conf.ID)
154 go func() {
155 defer func() {
156 if done := conf.Done; done != nil {
157 done()
158 }
159 }()
160 for invocation := uint(1); ; invocation++ {
161 if invocation == 0 {
162 logger.Warn("Invocation count rollover detected")
163 invocation = 1
164 }
165 logger := logger.WithField("invocation", invocation)
166 startTime := time.Now()
167 err := conf.Func.Execute(conf.Context, logger)
168 executionDuration := time.Since(startTime)
169 // NOTE: We discard only the common errors here, instead of checking
170 // the error code intentionally. The intent is to drop the commonly
171 // met errors without hiding other errors with the canceled or deadline
172 // exceeded error code.
173 switch {
174 case err == nil:
175 case errors.Is(err, context.Canceled),
176 errors.Is(err, errors.ErrContextCanceled),
177 errors.Is(err, context.DeadlineExceeded),
178 errors.Is(err, errors.ErrContextDeadlineExceeded):
179 case errors.Is(err, io.EOF):
180 default:
181 logger.WithError(err).Warn("Task failed")
182 }
183 switch conf.Restart {
184 case RestartNever:
185 return
186 case RestartAlways:
187 case RestartOnFailure:
188 if err == nil {
189 return
190 }
191 default:
192 panic("Invalid Config.Restart value")
193 }
194 select {
195 case <-conf.Context.Done():
196 return
197 default:
198 }
199 if conf.Backoff == nil {
200 continue
201 }
202 s := conf.Backoff.IntervalFunc(conf.Context, executionDuration, invocation, err)
203 if s == 0 {
204 continue
205 }
206 if conf.Backoff.Jitter != 0 {
207 s = random.Jitter(s, conf.Backoff.Jitter)
208 }
209 select {

Callers 6

StartTaskMethod · 0.92
StartTaskMethod · 0.92
TestProcessDownlinkTaskFunction · 0.92
StartTaskExcludeFunction · 0.92
StartTaskMethod · 0.92
StartTaskMethod · 0.92

Calls 10

FromContextFunction · 0.92
JitterFunction · 0.92
DoneMethod · 0.80
AfterMethod · 0.80
WithFieldMethod · 0.65
WarnMethod · 0.65
NowMethod · 0.65
ExecuteMethod · 0.65
WithErrorMethod · 0.65
IsMethod · 0.45

Tested by 5

StartTaskMethod · 0.74
TestProcessDownlinkTaskFunction · 0.74
StartTaskExcludeFunction · 0.74
StartTaskMethod · 0.74
StartTaskMethod · 0.74