@@ -180,7 +180,9 @@ func (a *Analyzer) runJobInternalExt(ctx context.Context, job models.Job, maxCon
180180 initialSummary , _ := json .Marshal (map [string ]interface {}{
181181 "conversations_found" : len (conversations ),
182182 })
183- db .DB .Model (& run ).Update ("summary" , string (initialSummary ))
183+ if err := db .DB .Model (& run ).Update ("summary" , string (initialSummary )).Error ; err != nil {
184+ log .Printf ("[analyzer] DB update error (initial summary): %v" , err )
185+ }
184186
185187 // Check batch mode setting (default: enabled with batch size 5)
186188 batchMode := true
@@ -270,7 +272,9 @@ func (a *Analyzer) runJobInternalExt(ctx context.Context, job models.Job, maxCon
270272 "conversations_errors" : errorCount ,
271273 "issues_found" : issuesFound ,
272274 })
273- db .DB .Model (& run ).Update ("summary" , string (errProgressJSON ))
275+ if err := db .DB .Model (& run ).Update ("summary" , string (errProgressJSON )).Error ; err != nil {
276+ log .Printf ("[analyzer] DB update error (error progress): %v" , err )
277+ }
274278 continue
275279 }
276280 analyzedCount ++
@@ -309,7 +313,9 @@ func (a *Analyzer) runJobInternalExt(ctx context.Context, job models.Job, maxCon
309313 "conversations_errors" : errorCount ,
310314 "issues_found" : issuesFound ,
311315 })
312- db .DB .Model (& run ).Update ("summary" , string (progressJSON ))
316+ if err := db .DB .Model (& run ).Update ("summary" , string (progressJSON )).Error ; err != nil {
317+ log .Printf ("[analyzer] DB update error (progress): %v" , err )
318+ }
313319 }
314320
315321 } // end else (non-batch mode)
@@ -329,12 +335,20 @@ complete:
329335 runStatus = "error"
330336 run .ErrorMessage = fmt .Sprintf ("AI errors: %d/%d conversations failed" , errorCount , len (conversations ))
331337 }
332- db .DB .Model (& run ).Updates (map [string ]interface {}{
333- "status" : runStatus ,
334- "finished_at" : & finishedAt ,
335- "summary" : string (summaryJSON ),
336- "error_message" : run .ErrorMessage ,
337- })
338+ // Critical: final status update — retry on failure to prevent stuck "running" state
339+ for retry := 0 ; retry < 3 ; retry ++ {
340+ if err := db .DB .Model (& run ).Updates (map [string ]interface {}{
341+ "status" : runStatus ,
342+ "finished_at" : & finishedAt ,
343+ "summary" : string (summaryJSON ),
344+ "error_message" : run .ErrorMessage ,
345+ }).Error ; err != nil {
346+ log .Printf ("[analyzer] DB update error (final status, attempt %d): %v" , retry + 1 , err )
347+ time .Sleep (2 * time .Second )
348+ continue
349+ }
350+ break
351+ }
338352
339353 // Update job last_run (skip for test runs to avoid affecting future normal runs)
340354 if ! isTestRun {
@@ -627,7 +641,9 @@ func (a *Analyzer) runBatchMode(ctx context.Context, provider ai.AIProvider, job
627641 }
628642
629643 // Process in batches
644+ consecutiveErrors := 0
630645 for i := 0 ; i < len (prepared ); i += batchSize {
646+ batchHadError := false
631647 // Check if context cancelled
632648 select {
633649 case <- ctx .Done ():
@@ -656,6 +672,7 @@ func (a *Analyzer) runBatchMode(ctx context.Context, provider ai.AIProvider, job
656672 if err != nil {
657673 log .Printf ("[analyzer-batch] AI error for batch starting at %d: %v" , i , err )
658674 errorCount += len (batch )
675+ batchHadError = true
659676 continue
660677 }
661678
@@ -741,11 +758,23 @@ func (a *Analyzer) runBatchMode(ctx context.Context, provider ai.AIProvider, job
741758 "conversations_errors" : errorCount ,
742759 "issues_found" : issuesFound ,
743760 })
744- db .DB .Model (& run ).Update ("summary" , string (progressJSON ))
761+ if err := db .DB .Model (& run ).Update ("summary" , string (progressJSON )).Error ; err != nil {
762+ log .Printf ("[analyzer-batch] DB update error (progress): %v" , err )
763+ }
745764
746- // Rate limit between batches
765+ // Adaptive rate limit between batches
747766 if end < len (prepared ) {
748- time .Sleep (1 * time .Second )
767+ if batchHadError {
768+ consecutiveErrors ++
769+ if consecutiveErrors >= 3 {
770+ time .Sleep (30 * time .Second )
771+ } else {
772+ time .Sleep (10 * time .Second )
773+ }
774+ } else {
775+ consecutiveErrors = 0
776+ time .Sleep (2 * time .Second )
777+ }
749778 }
750779 }
751780
0 commit comments