package custom_errors import ( "context" "fmt" "sync" "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models" ) type LoaderError struct { models.Batch Msg string } func (e *LoaderError) Error() string { return e.Msg } func LoaderErrorHandler( ctx context.Context, maxRetryAttempts int, chErrorsIn <-chan LoaderError, chBatchesOut chan<- models.Batch, chJobErrorsOut chan<- JobError, wgActiveBatches *sync.WaitGroup, ) { for { if ctx.Err() != nil { return } select { case <-ctx.Done(): return case err, ok := <-chErrorsIn: if !ok { return } if err.RetryCounter >= maxRetryAttempts { jobError := JobError{ ShouldCancelJob: false, Msg: fmt.Sprintf("Batch %v reached max retries (%d)", err.Id, maxRetryAttempts), Prev: &err, } select { case chJobErrorsOut <- jobError: case <-ctx.Done(): return } wgActiveBatches.Done() continue } err.RetryCounter++ select { case chBatchesOut <- err.Batch: case <-ctx.Done(): return } } } }