# HG changeset patch # User Sascha L. Teichmann # Date 1544460402 -3600 # Node ID fe633765e05bf765238eb60f1af28f69a0a56453 # Parent b03db5726ca5387c6067bc8f5451e3523974776b Started with JSON model for import configurations. diff -r b03db5726ca5 -r fe633765e05b pkg/models/importconfig.go --- /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 + +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 +} diff -r b03db5726ca5 -r fe633765e05b pkg/scheduler/scheduler.go --- 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) }