feat: implement extractor error handling and batch processing for MSSQL and Postgres
This commit is contained in:
80
internal/app/custom_errors/extractor.error.go
Normal file
80
internal/app/custom_errors/extractor.error.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package custom_errors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ExtractorError struct {
|
||||
Batch models.Batch
|
||||
LastId int64
|
||||
HasLastId bool
|
||||
Msg string
|
||||
}
|
||||
|
||||
func (e *ExtractorError) Error() string {
|
||||
return e.Msg
|
||||
}
|
||||
|
||||
const maxRetryAttempts = 3
|
||||
|
||||
func ExtractorErrorHandler(
|
||||
ctx context.Context,
|
||||
chErrorsIn <-chan ExtractorError,
|
||||
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.Batch.RetryCounter >= maxRetryAttempts {
|
||||
jobError := JobError{
|
||||
ShouldCancelJob: false,
|
||||
Msg: fmt.Sprintf("batch %v reached max retries (%d)", err.Batch.Id, maxRetryAttempts),
|
||||
Prev: &err,
|
||||
}
|
||||
|
||||
select {
|
||||
case chJobErrorsOut <- jobError:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
|
||||
wgActiveBatches.Done()
|
||||
continue
|
||||
}
|
||||
|
||||
newBatch := err.Batch
|
||||
newBatch.RetryCounter++
|
||||
|
||||
if err.HasLastId {
|
||||
newBatch.ParentId = err.Batch.Id
|
||||
newBatch.Id = uuid.New()
|
||||
newBatch.LowerLimit = err.LastId
|
||||
newBatch.IsLowerLimitInclusive = false
|
||||
}
|
||||
|
||||
select {
|
||||
case chBatchesOut <- newBatch:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user