diff --git a/engine/world.go b/engine/world.go index cadbe96c..2bb97bbc 100644 --- a/engine/world.go +++ b/engine/world.go @@ -44,8 +44,6 @@ type World interface { // TODO: is there a better name for this interface? ResWorld - Scheduler(namespace string, opts ...scheduler.Option) (*scheduler.Result, error) - // URI returns the current FS URI. // TODO: Can we improve this API or deprecate it entirely? URI() string @@ -84,6 +82,13 @@ type ResWorld interface { ResCollect(ctx context.Context, hostnameFilter, kindFilter []string) ([]Res, error) } +// SchedulerWorld is an interface that has to do with distributed scheduling. +// XXX: This should be abstracted to remove the etcd specific types if possible. +type SchedulerWorld interface { + // Scheduler runs a distributed scheduler. + Scheduler(namespace string, opts ...scheduler.Option) (*scheduler.Result, error) +} + // EtcdWorld is a world interface that should be implemented if the world // backend is implementing etcd, and if it supports dynamically resizing things. // TODO: In theory we could generalize this to support other backends, but lets diff --git a/lang/core/world/schedule.go b/lang/core/world/schedule.go index 0bb963a5..56ec46ee 100644 --- a/lang/core/world/schedule.go +++ b/lang/core/world/schedule.go @@ -46,7 +46,8 @@ import ( "fmt" "sort" - "github.com/purpleidea/mgmt/etcd/scheduler" // TODO: is it okay to import this without abstraction? + "github.com/purpleidea/mgmt/engine" + "github.com/purpleidea/mgmt/etcd/scheduler" // XXX: abstract this if possible "github.com/purpleidea/mgmt/lang/funcs" "github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/types" @@ -90,6 +91,8 @@ type ScheduleFunc struct { args []types.Value + world engine.SchedulerWorld + namespace string scheduler *scheduler.Result @@ -278,6 +281,13 @@ func (obj *ScheduleFunc) Info() *interfaces.Info { // Init runs some startup code for this function. func (obj *ScheduleFunc) Init(init *interfaces.Init) error { obj.init = init + + world, ok := obj.init.World.(engine.SchedulerWorld) + if !ok { + return fmt.Errorf("world backend does not support the SchedulerWorld interface") + } + obj.world = world + obj.watchChan = make(chan *schedulerResult) //obj.init.Debug = true // use this for local debugging return nil @@ -378,7 +388,7 @@ func (obj *ScheduleFunc) Stream(ctx context.Context) error { obj.init.Logf("starting scheduler...") } var err error - obj.scheduler, err = obj.init.World.Scheduler(obj.namespace, schedulerOpts...) + obj.scheduler, err = obj.world.Scheduler(obj.namespace, schedulerOpts...) if err != nil { return errwrap.Wrapf(err, "can't create scheduler") }