@@ -90,9 +90,25 @@ func main() {
90
90
91
91
// Start background process for auto-tagging
92
92
go func () {
93
+
94
+ minBackoffDuration := time .Second
95
+ maxBackoffDuration := time .Hour
96
+ pollingInterval := 10 * time .Second
97
+
98
+ backoffDuration := minBackoffDuration
93
99
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 )
96
112
}
97
113
}()
98
114
@@ -153,17 +169,16 @@ func validateEnvVars() {
153
169
}
154
170
155
171
// processAutoTagDocuments handles the background auto-tagging of documents
156
- func (app * App ) processAutoTagDocuments () {
172
+ func (app * App ) processAutoTagDocuments () error {
157
173
ctx := context .Background ()
158
174
159
175
documents , err := app .Client .GetDocumentsByTags (ctx , []string {autoTag })
160
176
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 )
163
178
}
164
179
165
180
if len (documents ) == 0 {
166
- return // No documents to process
181
+ return nil // No documents to process
167
182
}
168
183
169
184
suggestionRequest := GenerateSuggestionsRequest {
@@ -174,19 +189,15 @@ func (app *App) processAutoTagDocuments() {
174
189
175
190
suggestions , err := app .generateDocumentSuggestions (ctx , suggestionRequest )
176
191
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 )
184
193
}
185
194
186
195
err = app .Client .UpdateDocuments (ctx , suggestions )
187
196
if err != nil {
188
- log . Printf ( "Error updating documents: %v " , err )
197
+ return fmt . Errorf ( "error updating documents: %w " , err )
189
198
}
199
+
200
+ return nil
190
201
}
191
202
192
203
// getAllTagsHandler handles the GET /api/tags endpoint
0 commit comments