changeset 2872:bfea3f80ca9a

Import queue: Extend the idea of changeset 2886:b150e5b37afe to some error cases that might happen before the storing of the after import state.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 01 Apr 2019 10:36:14 +0200
parents 2cdf43c84a8c
children b1707f60f241
files pkg/imports/queue.go
diffstat 1 files changed, 27 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/queue.go	Fri Mar 29 14:23:06 2019 +0100
+++ b/pkg/imports/queue.go	Mon Apr 01 10:36:14 2019 +0200
@@ -488,6 +488,30 @@
 	return &ji, nil
 }
 
+func tryHardToStoreState(ctx context.Context, fn func(*sql.Conn) error) 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
+
+	for try := 1; ; try++ {
+		var err error
+		if err = auth.RunAs(ctx, queueUser, fn); 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 {
+			if sleep *= 2; sleep > time.Minute {
+				sleep = time.Minute
+			}
+		}
+	}
+}
+
 func updateStateSummary(
 	ctx context.Context,
 	id int64,
@@ -503,37 +527,15 @@
 		s = sql.NullString{String: b.String(), Valid: true}
 	}
 
-	// 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 {
+	return tryHardToStoreState(ctx, 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 {
-			if sleep *= 2; sleep > time.Minute {
-				sleep = time.Minute
-			}
-		}
-	}
+	})
 }
 
 func errorAndFail(id int64, format string, args ...interface{}) error {
 	ctx := context.Background()
-	err := auth.RunAs(ctx, queueUser, func(conn *sql.Conn) error {
+	return tryHardToStoreState(ctx, func(conn *sql.Conn) error {
 		tx, err := conn.BeginTx(ctx, nil)
 		if err != nil {
 			return err
@@ -551,7 +553,6 @@
 		}
 		return err
 	})
-	return err
 }
 
 func (q *importQueue) importLoop() {