Mercurial > gemma
changeset 5169:4f0869b85038
Import queue: Limit number of repetions of failed jobs to 200 and wait at least 5 seconds between the attempts.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 14 Apr 2020 11:36:13 +0200 |
parents | a74390e92c3c |
children | 4db81768f165 |
files | pkg/imports/queue.go |
diffstat | 1 files changed, 37 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/imports/queue.go Tue Apr 07 16:34:35 2020 +0200 +++ b/pkg/imports/queue.go Tue Apr 14 11:36:13 2020 +0200 @@ -125,6 +125,11 @@ reviewJobWait = time.Minute ) +const ( + hardMaxTries = 200 + minWaitRetry = 5 * time.Second +) + var ErrRetrying = errors.New("retrying") type importQueue struct { @@ -512,12 +517,25 @@ var tl sql.NullInt64 if triesLeft != nil { - tl = sql.NullInt64{Int64: int64(*triesLeft), Valid: true} + var many int64 + if *triesLeft > hardMaxTries || *triesLeft < 0 { + many = hardMaxTries + } else { + many = int64(*triesLeft) + } + tl = sql.NullInt64{Int64: many, Valid: true} } var wr pgtype.Interval if waitRetry != nil { - if err := wr.Set(*waitRetry); err != nil { + var howLong time.Duration + if minWaitRetry > *waitRetry { + howLong = minWaitRetry + } else { + howLong = *waitRetry + } + + if err := wr.Set(howLong); err != nil { return 0, nil, err } } else { @@ -810,9 +828,24 @@ } _, err = tx.ExecContext(ctx, updateStateSQL, "running", ji.id) if err == nil { - err = tx.Commit() + if err = tx.Commit(); err != nil { + return err + } } - return err + // Clip repetition back to allowd values. + if ji.waitRetry.Status == pgtype.Present { + var d time.Duration + ji.waitRetry.AssignTo(&d) + if d < minWaitRetry { + ji.waitRetry.Set(minWaitRetry) + } + } + if ji.triesLeft.Valid { + if ji.triesLeft.Int64 < 0 || ji.triesLeft.Int64 > hardMaxTries { + ji.triesLeft.Int64 = hardMaxTries + } + } + return nil }) switch { case err == sql.ErrNoRows: