diff 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
line wrap: on
line diff
--- a/pkg/scheduler/scheduler.go	Tue Dec 11 20:47:11 2018 +0100
+++ b/pkg/scheduler/scheduler.go	Tue Dec 11 22:59:10 2018 +0100
@@ -64,6 +64,58 @@
 	global.unregisterAction(name)
 }
 
+// BoundAction is a complete set of infos for
+// an action to be bound to a user, schedule and
+// optional configuration id.
+type BoundAction struct {
+	Name  string
+	Spec  string
+	User  string
+	CfgID *int64
+}
+
+// BootActions setup the global scheduler with a set
+// of bound actions delivered by the next function.
+func BootActions(next func(*BoundAction) (bool, error)) error {
+	return global.bootActions(next)
+}
+
+func (s *scheduler) bootActions(next func(*BoundAction) (bool, error)) error {
+
+	cr := cron.New()
+
+	for {
+		var ba BoundAction
+		ok, err := next(&ba)
+		if err != nil {
+			return err
+		}
+		if !ok {
+			break
+		}
+		schedule, err := cron.Parse(ba.Spec)
+		if err != nil {
+			return err
+		}
+		job := &userAction{
+			scheduler: s,
+			user:      ba.User,
+			name:      ba.Name,
+			cfgID:     ba.CfgID,
+		}
+		cr.Schedule(schedule, job)
+	}
+
+	s.mu.Lock()
+	defer s.mu.Unlock()
+
+	s.cr.Stop()
+	s.cr = cr
+	cr.Start()
+
+	return nil
+}
+
 // BindAction binds a named action to a user, a cron spec and
 // an optional configuration id.
 func BindAction(name, spec, user string, cfgID *int64) error {