changeset 1665:da0d1a19ebe6

Fairway availability import: Made schedulable, too.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 23 Dec 2018 13:30:33 +0100
parents 819f67c31dfb
children 56b29406a163
files pkg/imports/gmsched.go pkg/imports/scheduled.go
diffstat 2 files changed, 80 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/gmsched.go	Sun Dec 23 12:57:28 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,66 +0,0 @@
-// This is Free Software under GNU Affero General Public License v >= 3.0
-// without warranty, see README.md and license for details.
-//
-// SPDX-License-Identifier: AGPL-3.0-or-later
-// License-Filename: LICENSES/AGPL-3.0.txt
-//
-// Copyright (C) 2018 by via donau
-//   – Österreichische Wasserstraßen-Gesellschaft mbH
-// Software engineering by Intevation GmbH
-//
-// Author(s):
-//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
-
-package imports
-
-import (
-	"log"
-
-	"gemma.intevation.de/gemma/pkg/common"
-	"gemma.intevation.de/gemma/pkg/scheduler"
-)
-
-func init() {
-	scheduler.RegisterAction("gm", scheduledGM)
-}
-
-func scheduledGM(id int64) {
-	log.Println("info: scheduled GM import")
-
-	cfg, err := loadIDConfig(id)
-	if err != nil {
-		log.Printf("error: %v\n", err)
-		return
-	}
-	if cfg == nil {
-		log.Printf("error: No config found for id %d.\n", id)
-		return
-	}
-	if cfg.URL == nil {
-		log.Println("error: No URL specified")
-		return
-	}
-	gm := &GaugeMeasurement{
-		URL:      *cfg.URL,
-		Insecure: false,
-	}
-
-	var serialized string
-	if serialized, err = common.ToJSONString(gm); err != nil {
-		log.Printf("error: %v\n", err)
-		return
-	}
-
-	var jobID int64
-	if jobID, err = AddJob(
-		GMJobKind,
-		cfg.User,
-		cfg.SendEMail, cfg.AutoAccept,
-		serialized,
-	); err != nil {
-		log.Printf("error: %v\n", err)
-		return
-	}
-
-	log.Printf("info: added import #%d to queue\n", jobID)
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/imports/scheduled.go	Sun Dec 23 13:30:33 2018 +0100
@@ -0,0 +1,80 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2018 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package imports
+
+import (
+	"log"
+
+	"gemma.intevation.de/gemma/pkg/common"
+	"gemma.intevation.de/gemma/pkg/scheduler"
+)
+
+func init() {
+	registerAction(GMJobKind, func(cfg *IDConfig) interface{} {
+		log.Println("info: schedule 'gm' import")
+		return &GaugeMeasurement{
+			URL:      *cfg.URL,
+			Insecure: false,
+		}
+	})
+	registerAction(FAJobKind, func(cfg *IDConfig) interface{} {
+		log.Println("info: schedule 'fa' import")
+		return &FairwayAvailability{
+			URL:      *cfg.URL,
+			Insecure: false,
+		}
+	})
+}
+
+func registerAction(kind JobKind, setup func(cfg *IDConfig) interface{}) {
+
+	action := func(id int64) {
+		cfg, err := loadIDConfig(id)
+		if err != nil {
+			log.Printf("error: %v\n", err)
+			return
+		}
+		if cfg == nil {
+			log.Printf("error: No config found for id %d.\n", id)
+			return
+		}
+		if cfg.URL == nil {
+			log.Println("error: No URL specified")
+			return
+		}
+
+		what := setup(cfg)
+
+		var serialized string
+		if serialized, err = common.ToJSONString(what); err != nil {
+			log.Printf("error: %v\n", err)
+			return
+		}
+
+		var jobID int64
+		if jobID, err = AddJob(
+			kind,
+			cfg.User,
+			cfg.SendEMail, cfg.AutoAccept,
+			serialized,
+		); err != nil {
+			log.Printf("error: %v\n", err)
+			return
+		}
+
+		log.Printf("info: added import #%d to queue\n", jobID)
+	}
+
+	scheduler.RegisterAction(string(kind), action)
+}