Mercurial > gemma
diff pkg/imports/scheduled.go @ 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 | pkg/imports/gmsched.go@51a0ba4ede41 |
children | 56b29406a163 |
line wrap: on
line diff
--- /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) +}