changeset 2823:b150e5b37afe

Import queue: As it is important to keep the in-memory and the persistent model in sync try harder to store the final state change if it fails.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 27 Mar 2019 12:12:12 +0100
parents f2e4c39cdcfa
children d7c4169516fa
files pkg/imports/queue.go
diffstat 1 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/queue.go	Wed Mar 27 12:02:41 2019 +0100
+++ b/pkg/imports/queue.go	Wed Mar 27 12:12:12 2019 +0100
@@ -502,10 +502,34 @@
 		}
 		s = sql.NullString{String: b.String(), Valid: true}
 	}
-	return auth.RunAs(ctx, queueUser, func(conn *sql.Conn) error {
+
+	// As it is important to keep the persistent model
+	// in sync with the in-memory model try harder to store
+	// the state.
+	const maxTries = 10
+	var sleep = time.Second
+
+	store := func(conn *sql.Conn) error {
 		_, err := conn.ExecContext(ctx, updateStateSummarySQL, state, s, id)
 		return err
-	})
+	}
+
+	for try := 1; ; try++ {
+		var err error
+		if err = auth.RunAs(ctx, queueUser, store); err == nil || try == maxTries {
+			return err
+		}
+		log.Printf("warn: [try %d/%d] Storing state failed: %v (try again in %s).\n",
+			try, maxTries, err, sleep)
+
+		time.Sleep(sleep)
+		if sleep < time.Minute {
+			sleep *= 2
+			if sleep > time.Minute {
+				sleep = time.Minute
+			}
+		}
+	}
 }
 
 func errorAndFail(id int64, format string, args ...interface{}) error {