Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/exponential backoff for auto processing #26

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- **Easy Integration**: Works seamlessly with your existing paperless-ngx setup.
- **User-Friendly Interface**: Intuitive web interface for reviewing and applying suggested titles and tags.
- **Dockerized Deployment**: Simple setup using Docker and Docker Compose.
- **Automatic Document Processing**: Automatically apply generated suggestions for documents with the `paperless-gpt-auto` tag.


## Table of Contents

Expand Down
39 changes: 25 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()

Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions paperless.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading