comparison pkg/scheduler/scheduler.go @ 1557:62171cd9a42b

Import scheduler: Start scheduler a gemma boot time with configurations from database which have a schedule.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 11 Dec 2018 22:59:10 +0100
parents 6869eb94ead2
children 2fdd8e57542d
comparison
equal deleted inserted replaced
1556:6869eb94ead2 1557:62171cd9a42b
60 } 60 }
61 61
62 // UnregisterAction ungesiters a named action from the global scheduler. 62 // UnregisterAction ungesiters a named action from the global scheduler.
63 func UnregisterAction(name string) { 63 func UnregisterAction(name string) {
64 global.unregisterAction(name) 64 global.unregisterAction(name)
65 }
66
67 // BoundAction is a complete set of infos for
68 // an action to be bound to a user, schedule and
69 // optional configuration id.
70 type BoundAction struct {
71 Name string
72 Spec string
73 User string
74 CfgID *int64
75 }
76
77 // BootActions setup the global scheduler with a set
78 // of bound actions delivered by the next function.
79 func BootActions(next func(*BoundAction) (bool, error)) error {
80 return global.bootActions(next)
81 }
82
83 func (s *scheduler) bootActions(next func(*BoundAction) (bool, error)) error {
84
85 cr := cron.New()
86
87 for {
88 var ba BoundAction
89 ok, err := next(&ba)
90 if err != nil {
91 return err
92 }
93 if !ok {
94 break
95 }
96 schedule, err := cron.Parse(ba.Spec)
97 if err != nil {
98 return err
99 }
100 job := &userAction{
101 scheduler: s,
102 user: ba.User,
103 name: ba.Name,
104 cfgID: ba.CfgID,
105 }
106 cr.Schedule(schedule, job)
107 }
108
109 s.mu.Lock()
110 defer s.mu.Unlock()
111
112 s.cr.Stop()
113 s.cr = cr
114 cr.Start()
115
116 return nil
65 } 117 }
66 118
67 // BindAction binds a named action to a user, a cron spec and 119 // BindAction binds a named action to a user, a cron spec and
68 // an optional configuration id. 120 // an optional configuration id.
69 func BindAction(name, spec, user string, cfgID *int64) error { 121 func BindAction(name, spec, user string, cfgID *int64) error {