47 lines
614 B
Go
47 lines
614 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type JobError struct {
|
|
ShouldCancelJob bool
|
|
Msg string
|
|
Prev error
|
|
}
|
|
|
|
func (e *JobError) Error() string {
|
|
if e.Prev != nil {
|
|
return fmt.Sprintf("%s: %v", e.Msg, e.Prev)
|
|
}
|
|
|
|
return e.Msg
|
|
}
|
|
|
|
func jobErrorHandler(ctx context.Context, chErrorsIn <-chan JobError) error {
|
|
for {
|
|
if ctx.Err() != nil {
|
|
return nil
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
|
|
case err, ok := <-chErrorsIn:
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
if err.ShouldCancelJob {
|
|
return &err
|
|
}
|
|
|
|
log.Error(err)
|
|
}
|
|
}
|
|
}
|