Skip to content

Commit e62c544

Browse files
JonasHessicereed
authored andcommitted
feat: Add exponential backoff for auto processing
1 parent eeb821a commit e62c544

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

main.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,25 @@ func main() {
9090

9191
// Start background process for auto-tagging
9292
go func() {
93+
94+
minBackoffDuration := time.Second
95+
maxBackoffDuration := time.Hour
96+
pollingInterval := 10 * time.Second
97+
98+
backoffDuration := minBackoffDuration
9399
for {
94-
app.processAutoTagDocuments()
95-
time.Sleep(10 * time.Second)
100+
if err := app.processAutoTagDocuments(); err != nil {
101+
log.Printf("Error in processAutoTagDocuments: %v", err)
102+
time.Sleep(backoffDuration)
103+
backoffDuration *= 2 // Exponential backoff
104+
if backoffDuration > maxBackoffDuration {
105+
log.Printf("Repeated errors in processAutoTagDocuments detected. Setting backoff to %v", maxBackoffDuration)
106+
backoffDuration = maxBackoffDuration
107+
}
108+
} else {
109+
backoffDuration = minBackoffDuration
110+
}
111+
time.Sleep(pollingInterval)
96112
}
97113
}()
98114

@@ -153,17 +169,16 @@ func validateEnvVars() {
153169
}
154170

155171
// processAutoTagDocuments handles the background auto-tagging of documents
156-
func (app *App) processAutoTagDocuments() {
172+
func (app *App) processAutoTagDocuments() error {
157173
ctx := context.Background()
158174

159175
documents, err := app.Client.GetDocumentsByTags(ctx, []string{autoTag})
160176
if err != nil {
161-
log.Printf("Error fetching documents with autoTag: %v", err)
162-
return
177+
return fmt.Errorf("error fetching documents with autoTag: %w", err)
163178
}
164179

165180
if len(documents) == 0 {
166-
return // No documents to process
181+
return nil // No documents to process
167182
}
168183

169184
suggestionRequest := GenerateSuggestionsRequest{
@@ -174,19 +189,15 @@ func (app *App) processAutoTagDocuments() {
174189

175190
suggestions, err := app.generateDocumentSuggestions(ctx, suggestionRequest)
176191
if err != nil {
177-
log.Printf("Error generating suggestions: %v", err)
178-
return
179-
}
180-
181-
for i := range suggestions {
182-
log.Printf("Processing document ID %d with autoTag", suggestions[i].ID)
183-
suggestions[i].SuggestedTags = removeTagFromList(suggestions[i].SuggestedTags, autoTag)
192+
return fmt.Errorf("error generating suggestions: %w", err)
184193
}
185194

186195
err = app.Client.UpdateDocuments(ctx, suggestions)
187196
if err != nil {
188-
log.Printf("Error updating documents: %v", err)
197+
return fmt.Errorf("error updating documents: %w", err)
189198
}
199+
200+
return nil
190201
}
191202

192203
// getAllTagsHandler handles the GET /api/tags endpoint

paperless.go

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ func (c *PaperlessClient) UpdateDocuments(ctx context.Context, documents []Docum
183183
if len(tags) == 0 {
184184
tags = document.OriginalDocument.Tags
185185
}
186+
// remove autoTag to prevent infinite loop (even if it is in the original tags)
187+
tags = removeTagFromList(tags, autoTag)
186188

187189
// Map suggested tag names to IDs
188190
for _, tagName := range tags {

0 commit comments

Comments
 (0)