DefaultStartTask is the default TaskStarter.
(conf *Config)
| 150 | |
| 151 | // DefaultStartTask is the default TaskStarter. |
| 152 | func 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 { |