81 lines
1.4 KiB
Go
81 lines
1.4 KiB
Go
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 {
|
|
wgActiveBatches.Done()
|
|
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
|
|
}
|
|
|
|
continue
|
|
} else {
|
|
jobError := JobError{
|
|
ShouldCancelJob: false,
|
|
Msg: fmt.Sprintf("Temporal error in batch %v (retries: %d)", err.Batch.Id, err.Batch.RetryCounter),
|
|
Prev: &err,
|
|
}
|
|
|
|
select {
|
|
case chJobErrorsOut <- jobError:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
|
|
err.RetryCounter++
|
|
|
|
select {
|
|
case chBatchesOut <- err.Batch:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|