package custom_errors import ( "context" "fmt" "sync" "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models" "github.com/google/uuid" ) type ExtractorError struct { Partition models.Partition LastId int64 HasLastId bool Msg string } func (e *ExtractorError) Error() string { return e.Msg } func ExtractorErrorHandler( ctx context.Context, maxRetryAttempts int, chErrorsIn <-chan ExtractorError, chPartitionsOut chan<- models.Partition, chJobErrorsOut chan<- JobError, wgActivePartitions *sync.WaitGroup, ) { for { if ctx.Err() != nil { return } select { case <-ctx.Done(): return case err, ok := <-chErrorsIn: if !ok { return } if err.Partition.RetryCounter >= maxRetryAttempts { jobError := JobError{ ShouldCancelJob: false, Msg: fmt.Sprintf("Partition %v reached max retries (%d)", err.Partition.Id, maxRetryAttempts), Prev: &err, } select { case chJobErrorsOut <- jobError: case <-ctx.Done(): return } wgActivePartitions.Done() continue } newPartition := err.Partition newPartition.RetryCounter++ if err.HasLastId { newPartition.ParentId = err.Partition.Id newPartition.Id = uuid.New() newPartition.LowerLimit = err.LastId newPartition.IsLowerLimitInclusive = false } select { case chPartitionsOut <- newPartition: case <-ctx.Done(): return } } } }