mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-12 12:58:02 -05:00
feat: Add exponential backoff for auto processing
This commit is contained in:
parent
eeb821aeb0
commit
e62c5442f5
2 changed files with 27 additions and 14 deletions
39
main.go
39
main.go
|
@ -90,9 +90,25 @@ func main() {
|
|||
|
||||
// Start background process for auto-tagging
|
||||
go func() {
|
||||
|
||||
minBackoffDuration := time.Second
|
||||
maxBackoffDuration := time.Hour
|
||||
pollingInterval := 10 * time.Second
|
||||
|
||||
backoffDuration := minBackoffDuration
|
||||
for {
|
||||
app.processAutoTagDocuments()
|
||||
time.Sleep(10 * time.Second)
|
||||
if err := app.processAutoTagDocuments(); err != nil {
|
||||
log.Printf("Error in processAutoTagDocuments: %v", err)
|
||||
time.Sleep(backoffDuration)
|
||||
backoffDuration *= 2 // Exponential backoff
|
||||
if backoffDuration > maxBackoffDuration {
|
||||
log.Printf("Repeated errors in processAutoTagDocuments detected. Setting backoff to %v", maxBackoffDuration)
|
||||
backoffDuration = maxBackoffDuration
|
||||
}
|
||||
} else {
|
||||
backoffDuration = minBackoffDuration
|
||||
}
|
||||
time.Sleep(pollingInterval)
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -153,17 +169,16 @@ func validateEnvVars() {
|
|||
}
|
||||
|
||||
// processAutoTagDocuments handles the background auto-tagging of documents
|
||||
func (app *App) processAutoTagDocuments() {
|
||||
func (app *App) processAutoTagDocuments() error {
|
||||
ctx := context.Background()
|
||||
|
||||
documents, err := app.Client.GetDocumentsByTags(ctx, []string{autoTag})
|
||||
if err != nil {
|
||||
log.Printf("Error fetching documents with autoTag: %v", err)
|
||||
return
|
||||
return fmt.Errorf("error fetching documents with autoTag: %w", err)
|
||||
}
|
||||
|
||||
if len(documents) == 0 {
|
||||
return // No documents to process
|
||||
return nil // No documents to process
|
||||
}
|
||||
|
||||
suggestionRequest := GenerateSuggestionsRequest{
|
||||
|
@ -174,19 +189,15 @@ func (app *App) processAutoTagDocuments() {
|
|||
|
||||
suggestions, err := app.generateDocumentSuggestions(ctx, suggestionRequest)
|
||||
if err != nil {
|
||||
log.Printf("Error generating suggestions: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for i := range suggestions {
|
||||
log.Printf("Processing document ID %d with autoTag", suggestions[i].ID)
|
||||
suggestions[i].SuggestedTags = removeTagFromList(suggestions[i].SuggestedTags, autoTag)
|
||||
return fmt.Errorf("error generating suggestions: %w", err)
|
||||
}
|
||||
|
||||
err = app.Client.UpdateDocuments(ctx, suggestions)
|
||||
if err != nil {
|
||||
log.Printf("Error updating documents: %v", err)
|
||||
return fmt.Errorf("error updating documents: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getAllTagsHandler handles the GET /api/tags endpoint
|
||||
|
|
|
@ -183,6 +183,8 @@ func (c *PaperlessClient) UpdateDocuments(ctx context.Context, documents []Docum
|
|||
if len(tags) == 0 {
|
||||
tags = document.OriginalDocument.Tags
|
||||
}
|
||||
// remove autoTag to prevent infinite loop (even if it is in the original tags)
|
||||
tags = removeTagFromList(tags, autoTag)
|
||||
|
||||
// Map suggested tag names to IDs
|
||||
for _, tagName := range tags {
|
||||
|
|
Loading…
Reference in a new issue