mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-14 05:38:01 -05:00
feat: Add exponential backoff for auto processing
This commit is contained in:
parent
11062f4f7b
commit
e0051f7bc1
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
|
// Start background process for auto-tagging
|
||||||
go func() {
|
go func() {
|
||||||
|
|
||||||
|
minBackoffDuration := time.Second
|
||||||
|
maxBackoffDuration := time.Hour
|
||||||
|
pollingInterval := 10 * time.Second
|
||||||
|
|
||||||
|
backoffDuration := minBackoffDuration
|
||||||
for {
|
for {
|
||||||
app.processAutoTagDocuments()
|
if err := app.processAutoTagDocuments(); err != nil {
|
||||||
time.Sleep(10 * time.Second)
|
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
|
// processAutoTagDocuments handles the background auto-tagging of documents
|
||||||
func (app *App) processAutoTagDocuments() {
|
func (app *App) processAutoTagDocuments() error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
documents, err := app.Client.GetDocumentsByTags(ctx, []string{autoTag})
|
documents, err := app.Client.GetDocumentsByTags(ctx, []string{autoTag})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching documents with autoTag: %v", err)
|
return fmt.Errorf("error fetching documents with autoTag: %w", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(documents) == 0 {
|
if len(documents) == 0 {
|
||||||
return // No documents to process
|
return nil // No documents to process
|
||||||
}
|
}
|
||||||
|
|
||||||
suggestionRequest := GenerateSuggestionsRequest{
|
suggestionRequest := GenerateSuggestionsRequest{
|
||||||
|
@ -174,19 +189,15 @@ func (app *App) processAutoTagDocuments() {
|
||||||
|
|
||||||
suggestions, err := app.generateDocumentSuggestions(ctx, suggestionRequest)
|
suggestions, err := app.generateDocumentSuggestions(ctx, suggestionRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error generating suggestions: %v", err)
|
return fmt.Errorf("error generating suggestions: %w", 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = app.Client.UpdateDocuments(ctx, suggestions)
|
err = app.Client.UpdateDocuments(ctx, suggestions)
|
||||||
if err != nil {
|
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
|
// getAllTagsHandler handles the GET /api/tags endpoint
|
||||||
|
|
|
@ -183,6 +183,8 @@ func (c *PaperlessClient) UpdateDocuments(ctx context.Context, documents []Docum
|
||||||
if len(tags) == 0 {
|
if len(tags) == 0 {
|
||||||
tags = document.OriginalDocument.Tags
|
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
|
// Map suggested tag names to IDs
|
||||||
for _, tagName := range tags {
|
for _, tagName := range tags {
|
||||||
|
|
Loading…
Reference in a new issue