fix(bloomberg): avoid collection timeouts
This commit is contained in:
@ -3,6 +3,7 @@ package generation
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@ -135,7 +136,15 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
result.Summary.ModelName = &resolved.Model
|
||||
}
|
||||
}
|
||||
if err := u.Repository.SaveGeneration(ctx, userID, result); err != nil {
|
||||
saveCtx := ctx
|
||||
// Terminal metadata must survive a canceled provider/worker context.
|
||||
// Preserve the job identity for the existing atomic finalization path.
|
||||
if status == "failed" && ctx.Err() != nil {
|
||||
var cancel context.CancelFunc
|
||||
saveCtx, cancel = context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
}
|
||||
if err := u.Repository.SaveGeneration(saveCtx, userID, result); err != nil {
|
||||
return Result{}, fmt.Errorf("save generation: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
@ -232,7 +241,10 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
return finish("no_results", "", "")
|
||||
}
|
||||
content, err := resolved.Summarizer.Summarize(ctx, input.Language, input.Assets, selected)
|
||||
if err != nil || strings.TrimSpace(content) == "" {
|
||||
if err != nil || ctx.Err() != nil {
|
||||
return finish("failed", summaryFailureCode(ctx, err), "")
|
||||
}
|
||||
if strings.TrimSpace(content) == "" {
|
||||
return finish("failed", "summary_failed", "")
|
||||
}
|
||||
if partial {
|
||||
@ -240,3 +252,17 @@ func (u *useCase) Generate(ctx context.Context, userID string) (Result, error) {
|
||||
}
|
||||
return finish("succeeded", "", content)
|
||||
}
|
||||
|
||||
func summaryFailureCode(ctx context.Context, err error) string {
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return "summary_timeout"
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.Canceled) || errors.Is(err, context.Canceled) {
|
||||
return "summary_canceled"
|
||||
}
|
||||
var timeout interface{ Timeout() bool }
|
||||
if errors.As(err, &timeout) && timeout.Timeout() {
|
||||
return "summary_timeout"
|
||||
}
|
||||
return "summary_failed"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user