annotate pkg/scheduler/scheduler.go @ 1590:2fdd8e57542d

Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 14 Dec 2018 12:34:12 +0100
parents 62171cd9a42b
children f39957ea08aa
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"
1551
d9eba69f6515 Registered a dummy callback function for scheduled fairway imports in the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1550
diff changeset
18 "log"
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "sync"
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "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
22 )
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
24 // ErrNoSuchAction if no fitting action was found.
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 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
26
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
27 // Action is called with a user and an optional configuration id.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
28 type Action func(user string, cfgID *int64)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
29
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
30 type userAction struct {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
31 scheduler *scheduler
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
32 user string
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
33 name string
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
34 cfgID *int64
1540
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
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 type scheduler struct {
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 cr *cron.Cron
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
39 actions map[string]Action
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 mu sync.Mutex
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
43 // Run implements cron.Job.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
44 func (ua *userAction) Run() {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
45 if a := ua.scheduler.action(ua.name); a != nil {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
46 a(ua.user, ua.cfgID)
1556
6869eb94ead2 Don't error if an action is not defined if bound.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1551
diff changeset
47 } else {
6869eb94ead2 Don't error if an action is not defined if bound.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1551
diff changeset
48 log.Printf("warn: scheduled action '%s' not found.", ua.name)
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
49 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
50 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
51
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 var global = scheduler{
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 cr: cron.New(),
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
54 actions: make(map[string]Action),
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
55 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
56
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
57 // RegisterAction registers a named action to the global scheduler.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
58 func RegisterAction(name string, action Action) {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
59 global.registerAction(name, action)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
60 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
61
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
62 // UnregisterAction ungesiters a named action from the global scheduler.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
63 func UnregisterAction(name string) {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
64 global.unregisterAction(name)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
65 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
66
1557
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
67 // BoundAction is a complete set of infos for
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
68 // an action to be bound to a user, schedule and
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
69 // optional configuration id.
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
70 type BoundAction struct {
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
71 Name string
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
72 Spec string
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
73 User string
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
74 CfgID *int64
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
75 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
76
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
77 // BootActions setup the global scheduler with a set
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
78 // of bound actions delivered by the next function.
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
79 func BootActions(next func(*BoundAction) (bool, error)) error {
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
80 return global.bootActions(next)
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
81 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
82
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
83 func (s *scheduler) bootActions(next func(*BoundAction) (bool, error)) error {
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
84
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
85 cr := cron.New()
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
86
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
87 for {
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
88 var ba BoundAction
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
89 ok, err := next(&ba)
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
90 if err != nil {
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
91 return err
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
92 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
93 if !ok {
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
94 break
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
95 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
96 schedule, err := cron.Parse(ba.Spec)
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
97 if err != nil {
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
98 return err
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
99 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
100 job := &userAction{
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
101 scheduler: s,
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
102 user: ba.User,
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
103 name: ba.Name,
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
104 cfgID: ba.CfgID,
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
105 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
106 cr.Schedule(schedule, job)
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
107 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
108
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
109 s.mu.Lock()
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
110 defer s.mu.Unlock()
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
111
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
112 s.cr.Stop()
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
113 s.cr = cr
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
114 cr.Start()
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
115
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
116 return nil
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
117 }
62171cd9a42b Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1556
diff changeset
118
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
119 // BindAction binds a named action to a user, a cron spec and
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
120 // an optional configuration id.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
121 func BindAction(name, spec, user string, cfgID *int64) error {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
122 return global.bindAction(name, spec, user, cfgID)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
123 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
124
1550
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
125 // UnbindAction unbinds a named action from a user and
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
126 // an optional configuration id.
1556
6869eb94ead2 Don't error if an action is not defined if bound.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1551
diff changeset
127 func UnbindAction(name, user string, cfgID *int64) {
6869eb94ead2 Don't error if an action is not defined if bound.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1551
diff changeset
128 global.unbindAction(name, user, cfgID)
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
129 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
130
1590
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
131 // UnbindByID unbinds all schedules with a given id.
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
132 func UnbindByID(cfgID int64) {
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
133 global.unbindByID(cfgID)
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
134 }
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
135
1550
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
136 // UnbindUser unbinds all schedules for a given user.
1544
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
137 func UnbindUser(user string) {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
138 global.unbindUser(user)
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
139 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
140
1550
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
141 // HasAction asks if there is an action with a given name.
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
142 func HasAction(name string) bool {
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
143 return global.hasAction(name)
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
144 }
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
145
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
146 func (s *scheduler) hasAction(name string) bool {
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
147 s.mu.Lock()
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
148 defer s.mu.Unlock()
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
149 return s.actions[name] != nil
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
150 }
fe633765e05b Started with JSON model for import configurations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1544
diff changeset
151
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
152 func sameCfgID(a, b *int64) bool {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
153 return (a == nil && b == nil) || (a != nil && b != nil && *a == *b)
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155
1544
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
156 func (s *scheduler) unbindUser(user string) {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
157 s.mu.Lock()
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
158 defer s.mu.Unlock()
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
159
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
160 entries := s.cr.Entries()
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
161
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
162 if len(entries) == 0 {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
163 return
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
164 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
165
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
166 var found bool
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
167 for _, entry := range entries {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
168 ua := entry.Job.(*userAction)
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
169 if ua.user == user {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
170 found = true
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
171 break
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
172 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
173 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
174 if !found {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
175 return
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
176 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
177
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
178 s.cr.Stop()
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
179 s.cr = cron.New()
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
180 for _, entry := range entries {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
181 ua := entry.Job.(*userAction)
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
182 if ua.user != user {
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
183 s.cr.Schedule(entry.Schedule, entry.Job)
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
184 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
185 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
186 s.cr.Start()
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
187 }
5f80ec319a4f If a user is removed or renamed kill her/his schedule. Needs more case on renaming.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1541
diff changeset
188
1590
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
189 func (s *scheduler) unbindByID(cfgID int64) {
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
190 s.mu.Lock()
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
191 defer s.mu.Unlock()
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
192
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
193 entries := s.cr.Entries()
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
194
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
195 var found bool
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
196 for _, entry := range entries {
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
197 ua := entry.Job.(*userAction)
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
198 if ua.cfgID != nil && *ua.cfgID == cfgID {
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
199 found = true
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
200 break
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
201 }
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
202 }
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
203
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
204 if !found {
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
205 return
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
206 }
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
207
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
208 s.cr.Stop()
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
209 s.cr = cron.New()
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
210 for _, entry := range entries {
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
211 ua := entry.Job.(*userAction)
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
212 if ua.cfgID == nil || *ua.cfgID != cfgID {
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
213 s.cr.Schedule(entry.Schedule, entry.Job)
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
214 }
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
215 }
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
216 s.cr.Start()
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
217 }
2fdd8e57542d Added DELETE /imports/scheduler/{id:[0-9]+} to delete a schedule from the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1557
diff changeset
218
1556
6869eb94ead2 Don't error if an action is not defined if bound.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1551
diff changeset
219 func (s *scheduler) unbindAction(name, user string, cfgID *int64) {
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
220 s.mu.Lock()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
221 defer s.mu.Unlock()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
222
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
223 entries := s.cr.Entries()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
224
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
225 var found *userAction
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
226 for _, entry := range entries {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
227 ua := entry.Job.(*userAction)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
228 if ua.name == name && ua.user == user && sameCfgID(cfgID, ua.cfgID) {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
229 // Already have such a user/action/cfg tuple -> re-schedule.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
230 found = ua
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
231 break
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
232 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
233 }
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
235 if found == nil {
1556
6869eb94ead2 Don't error if an action is not defined if bound.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1551
diff changeset
236 return
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
237 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
238
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
239 s.cr.Stop()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
240 s.cr = cron.New()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
241 for _, entry := range entries {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
242 ua := entry.Job.(*userAction)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
243 if ua != found {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
244 s.cr.Schedule(entry.Schedule, entry.Job)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
245 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
246 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
247 s.cr.Start()
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
248 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
249
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
250 func (s *scheduler) bindAction(name, spec, user string, cfgID *int64) error {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
251
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
252 schedule, err := cron.Parse(spec)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
253 if err != nil {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
254 return err
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
255 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
256
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
257 s.mu.Lock()
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
258 defer s.mu.Unlock()
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
259
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
260 entries := s.cr.Entries()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
261
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
262 var found *userAction
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
263 for _, entry := range entries {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
264 ua := entry.Job.(*userAction)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
265 if ua.name == name && ua.user == user && sameCfgID(cfgID, ua.cfgID) {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
266 // Already have such a user/action/cfg tuple -> re-schedule.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
267 found = ua
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
268 break
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
269 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
270 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
271
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
272 if found == nil {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
273 // Add to current plan.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
274 job := &userAction{scheduler: s, user: user, name: name, cfgID: cfgID}
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
275 s.cr.Schedule(schedule, job)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
276 } else {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
277 // If found re-build all.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
278 s.cr.Stop()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
279 s.cr = cron.New()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
280 for _, entry := range entries {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
281 ua := entry.Job.(*userAction)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
282 var sch cron.Schedule
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
283 if found == ua {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
284 // replace with new schedule.
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
285 sch = schedule
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
286 } else {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
287 sch = entry.Schedule
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
288 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
289 s.cr.Schedule(sch, entry.Job)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
290 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
291 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
292 s.cr.Start()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
293
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
294 return nil
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
295 }
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
296
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
297 func (s *scheduler) action(name string) Action {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
298 s.mu.Lock()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
299 defer s.mu.Unlock()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
300 return s.actions[name]
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
301 }
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
302
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
303 func (s *scheduler) registerAction(name string, action Action) {
1551
d9eba69f6515 Registered a dummy callback function for scheduled fairway imports in the scheduler.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1550
diff changeset
304 log.Printf("info: register action '%s' in scheduler.", name)
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
305 s.mu.Lock()
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
306 defer s.mu.Unlock()
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
307 s.actions[name] = action
1540
251ee25accce Droped on scheduler in favor for a third party library.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
308 }
1541
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
309
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
310 func (s *scheduler) unregisterAction(name string) {
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
311 s.mu.Lock()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
312 defer s.mu.Unlock()
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
313 delete(s.actions, name)
03fcad10104a Added an internal cron job executor which binds actions to tuples of cron schedule, user name and an optional configuation id.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1540
diff changeset
314 }