changeset 1975:d966f03ea819

Imports: Added the new state 'unchanged' which can be issued by the imports to indicate that the database is not modified by the particular imports.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 23 Jan 2019 11:20:14 +0100
parents da6dc9b73f34
children ed3e770300b1
files pkg/imports/bn.go pkg/imports/email.go pkg/imports/queue.go pkg/imports/wg.go schema/gemma.sql
diffstat 5 files changed, 44 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/bn.go	Wed Jan 23 11:07:17 2019 +0100
+++ b/pkg/imports/bn.go	Wed Jan 23 11:20:14 2019 +0100
@@ -252,8 +252,7 @@
 		feedback.Info("Inserted '%s' into database", bn.OBJNAM)
 	}
 	if len(nids) == 0 {
-		feedback.Error("No new bottlenecks found")
-		return nil, errors.New("No new bottlenecks found")
+		return nil, UnchangedError("No new bottlenecks found")
 	}
 
 	feedback.Info("Storing %d bottlenecks took %s", len(nids), time.Since(start))
--- a/pkg/imports/email.go	Wed Jan 23 11:07:17 2019 +0100
+++ b/pkg/imports/email.go	Wed Jan 23 11:20:14 2019 +0100
@@ -39,9 +39,10 @@
 this email notification.
 
 {{ if eq .State "accepted" }}The imported data were successfully integrated into the database.{{ end -}}
+{{ if eq .State "unchanged" }}The import has not changed any data in the database.{{ end -}}
 {{ if eq .State "failed" }}The import failed for some reasons.{{ end -}}
 {{ if eq .State "pending" }}The imported data could be integrated into the database
-but your final decision is needed.{{ end }}
+but your final decision is needed.{{ end -}}
 
 Please follow this link to have a closer look at the details:
 
--- a/pkg/imports/queue.go	Wed Jan 23 11:07:17 2019 +0100
+++ b/pkg/imports/queue.go	Wed Jan 23 11:20:14 2019 +0100
@@ -51,6 +51,10 @@
 		When time.Time
 	}
 
+	// UnchangedError may be issued by Do of a Job to indicate
+	// That the database has not changed.
+	UnchangedError string
+
 	// Job is the central abstraction of an import job
 	// run by the import queue.
 	Job interface {
@@ -127,6 +131,7 @@
 		"queued",
 		"running",
 		"failed",
+		"unchanged",
 		"pending",
 		"accepted",
 		"declined",
@@ -206,6 +211,11 @@
 	return re.Message
 }
 
+// Error makes UnchangedError an error.
+func (ue UnchangedError) Error() string {
+	return string(ue)
+}
+
 func (q *importQueue) registerJobCreator(kind JobKind, jc JobCreator) {
 	q.creatorsMu.Lock()
 	defer q.creatorsMu.Unlock()
@@ -544,28 +554,42 @@
 						return err
 					})
 			})()
-			if errDo != nil {
-				feedback.Error("error do: %v", errDo)
-			}
-			// Should we try again?
-			retry, shouldRetry := errDo.(*RetryError)
+
+			var retry *RetryError
+			var unchanged bool
 
-			if shouldRetry && idj.trysLeft.Valid { // NULL -> limit less
-				if idj.trysLeft.Int64--; idj.trysLeft.Int64 <= 0 {
-					shouldRetry = false
+			switch v := errDo.(type) {
+			case *RetryError:
+				// NULL -> limit less
+				if idj.trysLeft.Valid && idj.trysLeft.Int64 <= 1 {
+					feedback.Warn("import should be retried, but no retrys left")
+				} else {
+					if idj.trysLeft.Valid {
+						idj.trysLeft.Int64--
+					}
+					feedback.Info("import failed but will be retried")
+					retry = v
+				}
+			case UnchangedError:
+				feedback.Info("unchanged: %s", v.Error())
+				unchanged = true
+			default:
+				if errDo != nil {
+					feedback.Error("error in import: %v", errDo)
 				}
 			}
 
 			var errCleanup error
-			if !shouldRetry { // cleanup debris
-				errCleanup = survive(job.CleanUp)()
-				if errCleanup != nil {
+			if retry == nil { // cleanup debris
+				if errCleanup = survive(job.CleanUp)(); errCleanup != nil {
 					feedback.Error("error cleanup: %v", errCleanup)
 				}
 			}
 
 			var state string
 			switch {
+			case unchanged:
+				state = "unchanged"
 			case errDo != nil || errCleanup != nil:
 				state = "failed"
 			case jc.AutoAccept():
@@ -581,7 +605,7 @@
 				go sendNotificationMail(idj.user, jc.Description(), state, idj.id)
 			}
 
-			if shouldRetry {
+			if retry != nil {
 				nid, err := q.addJob(
 					idj.kind,
 					retry.When, idj.trys(),
--- a/pkg/imports/wg.go	Wed Jan 23 11:07:17 2019 +0100
+++ b/pkg/imports/wg.go	Wed Jan 23 11:20:14 2019 +0100
@@ -18,7 +18,6 @@
 	"database/sql"
 	"errors"
 	"fmt"
-	"log"
 	"strings"
 	"time"
 
@@ -181,7 +180,6 @@
 	data, err := client.GetRisDataXML(request)
 
 	if err != nil {
-		log.Printf("error: %v\n", err)
 		return nil, fmt.Errorf("Error requesting ERDMS service: %v", err)
 	}
 
@@ -254,7 +252,7 @@
 	feedback.Info("update gauges: %d", len(olds))
 
 	if len(news) == 0 && len(olds) == 0 {
-		return nil, errors.New("nothing to do")
+		return nil, UnchangedError("nothing to do")
 	}
 
 	// delete reference water leves of the old.
--- a/schema/gemma.sql	Wed Jan 23 11:07:17 2019 +0100
+++ b/schema/gemma.sql	Wed Jan 23 11:20:14 2019 +0100
@@ -629,8 +629,10 @@
 -- Import queue and respective logging
 --
 CREATE TYPE waterway.import_state AS ENUM (
-    'queued', 'running', 'failed',
-    'pending', 'accepted', 'declined'
+    'queued',
+    'running',
+    'failed', 'unchanged', 'pending',
+    'accepted', 'declined'
 );
 
 CREATE TABLE waterway.imports (