changeset 1550:fe633765e05b

Started with JSON model for import configurations.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 10 Dec 2018 17:46:42 +0100
parents b03db5726ca5
children d9eba69f6515
files pkg/models/importconfig.go pkg/scheduler/scheduler.go
diffstat 2 files changed, 74 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/models/importconfig.go	Mon Dec 10 17:46:42 2018 +0100
@@ -0,0 +1,61 @@
+package models
+
+import (
+	"encoding/json"
+	"fmt"
+
+	"github.com/robfig/cron"
+
+	"gemma.intevation.de/gemma/pkg/scheduler"
+)
+
+// 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>
+
+type (
+	CronSpec   string
+	ImportKind string
+
+	ImportConfig struct {
+		Kind ImportKind `json:"kind"`
+		Cron CronSpec   `json:"cron"`
+		URL  *string    `json:"url"`
+	}
+)
+
+func (ik *ImportKind) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+
+	if !scheduler.HasAction(s) {
+		return fmt.Errorf("Unknown kind '%s'", s)
+	}
+
+	*ik = ImportKind(s)
+
+	return nil
+}
+
+func (cs *CronSpec) UnmarshalJSON(data []byte) error {
+	var spec string
+	if err := json.Unmarshal(data, &spec); err != nil {
+		return err
+	}
+	if _, err := cron.Parse(spec); err != nil {
+		return err
+	}
+	*cs = CronSpec(spec)
+	return nil
+}
--- a/pkg/scheduler/scheduler.go	Mon Dec 10 17:10:30 2018 +0100
+++ b/pkg/scheduler/scheduler.go	Mon Dec 10 17:46:42 2018 +0100
@@ -67,16 +67,28 @@
 	return global.bindAction(name, spec, user, cfgID)
 }
 
-// UnbindAction unbins a named action from a user and and
+// UnbindAction unbinds a named action from a user and
 // an optional configuration id.
 func UnbindAction(name, user string, cfgID *int64) error {
 	return global.unbindAction(name, user, cfgID)
 }
 
+// UnbindUser unbinds all schedules for a given user.
 func UnbindUser(user string) {
 	global.unbindUser(user)
 }
 
+// HasAction asks if there is an action with a given name.
+func HasAction(name string) bool {
+	return global.hasAction(name)
+}
+
+func (s *scheduler) hasAction(name string) bool {
+	s.mu.Lock()
+	defer s.mu.Unlock()
+	return s.actions[name] != nil
+}
+
 func sameCfgID(a, b *int64) bool {
 	return (a == nil && b == nil) || (a != nil && b != nil && *a == *b)
 }