From 4ce7c500eac63addc5f0ebb5d2caa238bb6e0e72 Mon Sep 17 00:00:00 2001 From: tamal Date: Thu, 22 Jun 2017 16:59:12 -0700 Subject: [PATCH] Rename cron pkg to scheduler. --- crond.go | 14 +++++----- pkg/cron/events.go | 37 --------------------------- pkg/{cron => scheduler}/controller.go | 30 +++++++++++----------- pkg/scheduler/events.go | 37 +++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 59 deletions(-) delete mode 100644 pkg/cron/events.go rename pkg/{cron => scheduler}/controller.go (95%) create mode 100644 pkg/scheduler/events.go diff --git a/crond.go b/crond.go index bb40874c9..ad9c6bfd6 100644 --- a/crond.go +++ b/crond.go @@ -4,13 +4,13 @@ import ( "github.com/appscode/log" rcs "github.com/appscode/stash/client/clientset" "github.com/appscode/stash/pkg/analytics" - "github.com/appscode/stash/pkg/cron" + "github.com/appscode/stash/pkg/scheduler" "github.com/spf13/cobra" clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" ) -func NewCmdCrond(version string) *cobra.Command { +func NewCmdSchedule(version string) *cobra.Command { var ( masterURL string kubeconfigPath string @@ -20,26 +20,26 @@ func NewCmdCrond(version string) *cobra.Command { ) cmd := &cobra.Command{ - Use: "crond", + Use: "schedule", Short: "Run Stash cron daemon", PreRun: func(cmd *cobra.Command, args []string) { if enableAnalytics { analytics.Enable() } - analytics.SendEvent("crond", "started", version) + analytics.SendEvent("scheduler", "started", version) }, PostRun: func(cmd *cobra.Command, args []string) { - analytics.SendEvent("crond", "stopped", version) + analytics.SendEvent("scheduler", "stopped", version) }, Run: func(cmd *cobra.Command, args []string) { config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath) if err != nil { - log.Fatalf("Could not get kubernetes config: %s", err) + log.Fatalf("Could not get Kubernetes config: %s", err) } kubeClient := clientset.NewForConfigOrDie(config) stashClient := rcs.NewForConfigOrDie(config) - ctrl := cron.NewController(kubeClient, stashClient, namespace, name) + ctrl := scheduler.NewController(kubeClient, stashClient, namespace, name) ctrl.RunAndHold() }, } diff --git a/pkg/cron/events.go b/pkg/cron/events.go deleted file mode 100644 index 3a0abbbc1..000000000 --- a/pkg/cron/events.go +++ /dev/null @@ -1,37 +0,0 @@ -package cron - -import ( - "github.com/appscode/stash/pkg/analytics" -) - -func crondSuccessfullyAdded() { - analytics.SendEvent("crond", "added", "success") -} - -func crondFailedToAdd() { - analytics.SendEvent("crond", "added", "failure") -} - -func crondSuccessfullyModified() { - analytics.SendEvent("crond", "modified", "success") -} - -func crondFailedToModify() { - analytics.SendEvent("crond", "modified", "failure") -} - -func backupSuccess() { - analytics.SendEvent("crond", "backup", "success") -} - -func backupFailure() { - analytics.SendEvent("crond", "backup", "failure") -} - -func stashJobSuccess() { - analytics.SendEvent("crond", "job", "success") -} - -func stashJobFailure() { - analytics.SendEvent("crond", "job", "failure") -} diff --git a/pkg/cron/controller.go b/pkg/scheduler/controller.go similarity index 95% rename from pkg/cron/controller.go rename to pkg/scheduler/controller.go index ae69a64c3..36ba66468 100644 --- a/pkg/cron/controller.go +++ b/pkg/scheduler/controller.go @@ -1,4 +1,4 @@ -package cron +package scheduler import ( "errors" @@ -43,8 +43,8 @@ type controller struct { resourceVersion string locked chan struct{} - scheduler *cron.Cron - recorder record.EventRecorder + cron *cron.Cron + recorder record.EventRecorder } func NewController(kubeClient clientset.Interface, stashClient scs.ExtensionInterface, namespace, name string) *controller { @@ -54,12 +54,12 @@ func NewController(kubeClient clientset.Interface, stashClient scs.ExtensionInte resourceNamespace: namespace, resourceName: name, resource: make(chan *sapi.Restic), - recorder: eventer.NewEventRecorder(kubeClient, "stash-crond"), + recorder: eventer.NewEventRecorder(kubeClient, "stash-scheduler"), } } func (c *controller) RunAndHold() { - c.scheduler.Start() + c.cron.Start() lw := &cache.ListWatch{ ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { @@ -79,7 +79,7 @@ func (c *controller) RunAndHold() { c.resource <- r err := c.configureScheduler() if err != nil { - crondFailedToAdd() + schedulerFailedToAdd() c.recorder.Eventf( r, apiv1.EventTypeWarning, @@ -88,7 +88,7 @@ func (c *controller) RunAndHold() { ) log.Errorln(err) } else { - crondSuccessfullyAdded() + schedulerSuccessfullyAdded() } } } @@ -108,7 +108,7 @@ func (c *controller) RunAndHold() { c.resource <- newObj err := c.configureScheduler() if err != nil { - crondFailedToModify() + schedulerFailedToModify() c.recorder.Eventf( newObj, apiv1.EventTypeWarning, @@ -117,14 +117,14 @@ func (c *controller) RunAndHold() { ) log.Errorln(err) } else { - crondSuccessfullyModified() + schedulerSuccessfullyModified() } } }, DeleteFunc: func(obj interface{}) { if r, ok := obj.(*sapi.Restic); ok { if r.Name == c.resourceName { - c.scheduler.Stop() + c.cron.Stop() } } }, @@ -135,10 +135,10 @@ func (c *controller) RunAndHold() { func (c *controller) configureScheduler() error { r := <-c.resource c.resourceVersion = r.ResourceVersion - if c.scheduler == nil { + if c.cron == nil { c.locked = make(chan struct{}) c.locked <- struct{}{} - c.scheduler = cron.New() + c.cron = cron.New() } password, err := getPasswordFromSecret(c.KubeClient, r.Spec.Destination.RepositorySecretName, r.Namespace) @@ -157,8 +157,8 @@ func (c *controller) configureScheduler() error { } } // Remove previous jobs - for _, v := range c.scheduler.Entries() { - c.scheduler.Remove(v.ID) + for _, v := range c.cron.Entries() { + c.cron.Remove(v.ID) } interval := r.Spec.Schedule if _, err = cron.Parse(interval); err != nil { @@ -173,7 +173,7 @@ func (c *controller) configureScheduler() error { c.recorder.Event(r, apiv1.EventTypeNormal, eventer.EventReasonSuccessfulCronExpressionReset, "Cron expression reset") return nil } - _, err = c.scheduler.AddFunc(interval, func() { + _, err = c.cron.AddFunc(interval, func() { if err := c.runOnce(); err != nil { stashJobFailure() c.recorder.Event(r, apiv1.EventTypeWarning, eventer.EventReasonFailedCronJob, err.Error()) diff --git a/pkg/scheduler/events.go b/pkg/scheduler/events.go new file mode 100644 index 000000000..d75f433d8 --- /dev/null +++ b/pkg/scheduler/events.go @@ -0,0 +1,37 @@ +package scheduler + +import ( + "github.com/appscode/stash/pkg/analytics" +) + +func schedulerSuccessfullyAdded() { + analytics.SendEvent("scheduler", "added", "success") +} + +func schedulerFailedToAdd() { + analytics.SendEvent("scheduler", "added", "failure") +} + +func schedulerSuccessfullyModified() { + analytics.SendEvent("scheduler", "modified", "success") +} + +func schedulerFailedToModify() { + analytics.SendEvent("scheduler", "modified", "failure") +} + +func backupSuccess() { + analytics.SendEvent("scheduler", "backup", "success") +} + +func backupFailure() { + analytics.SendEvent("scheduler", "backup", "failure") +} + +func stashJobSuccess() { + analytics.SendEvent("scheduler", "job", "success") +} + +func stashJobFailure() { + analytics.SendEvent("scheduler", "job", "failure") +}