67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type ExtractorError struct {
|
|
Batch
|
|
LastId int64
|
|
HasLastId bool
|
|
Msg string
|
|
}
|
|
|
|
func (e *ExtractorError) Error() string {
|
|
return e.Msg
|
|
}
|
|
|
|
const maxRetryAttempts = 3
|
|
|
|
func extractorErrorHandler(chErrorsIn <-chan ExtractorError, chBatchesOut chan<- Batch, chGlobalErrorsOut chan<- error) {
|
|
for err := range chErrorsIn {
|
|
if err.RetryCounter >= maxRetryAttempts {
|
|
chGlobalErrorsOut <- fmt.Errorf("batch %v reached max retries (%d): %s", err.Id, maxRetryAttempts, err.Msg)
|
|
continue
|
|
}
|
|
|
|
newBatch := err.Batch
|
|
newBatch.RetryCounter++
|
|
|
|
if err.HasLastId {
|
|
newBatch.ParentId = err.Id
|
|
newBatch.Id = uuid.New()
|
|
newBatch.LowerLimit = err.LastId
|
|
newBatch.IsLowerLimitInclusive = false
|
|
}
|
|
|
|
chBatchesOut <- newBatch
|
|
}
|
|
}
|
|
|
|
func ExtractorErrorFromLastRowMssql(lastRow UnknownRowValues, indexPrimaryKey int, batch *Batch, previousError error) ExtractorError {
|
|
lastIdRawValue := lastRow[indexPrimaryKey]
|
|
|
|
lastId, ok := ToInt64(lastIdRawValue)
|
|
if !ok {
|
|
currentBatch := *batch
|
|
currentBatch.RetryCounter = maxRetryAttempts
|
|
exError := ExtractorError{
|
|
Batch: currentBatch,
|
|
HasLastId: true,
|
|
Msg: fmt.Sprintf("Couldn't cast last id value as int: %s", previousError.Error()),
|
|
}
|
|
return exError
|
|
|
|
}
|
|
|
|
exError := ExtractorError{
|
|
Batch: *batch,
|
|
HasLastId: true,
|
|
LastId: lastId,
|
|
Msg: previousError.Error(),
|
|
}
|
|
return exError
|
|
}
|