annotate pkg/scheduler/scheduler.go @ 1540:251ee25accce

Droped on scheduler in favor for a third party library.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 07 Dec 2018 18:34:53 +0100
parents
children 03fcad10104a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package scheduler
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "errors"
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "sync"
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "github.com/robfig/cron"
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 )
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 var ErrNoSuchAction = errors.New("No such action")
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 type funcJob struct {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 bound bool
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 fn func()
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 type scheduler struct {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 cr *cron.Cron
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 actions map[string]*funcJob
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 mu sync.Mutex
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 var global = scheduler{
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 cr: cron.New(),
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 actions: make(map[string]*funcJob),
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 func AddAction(name string, fn func()) {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 global.addAction(name, fn)
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 func AddSchedule(spec, action string) error {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 return global.addAction(spec, action)
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 func (s *scheduler) addSchedule(spec, action string) error {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 s.mu.Lock()
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 defer s.mu.Unlock()
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 if s.actions[action] == nil {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 return ErrNoSuchAction
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 // TODO: Implement me!
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 func (s *scheduler) addAction(name string, fn func()) {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 s.mu.Lock()
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 defer s.mu.Unlock()
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 s.actions[name] = &funcJob{fn: fn}
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 }