Skip to content

Commit

Permalink
Rename cron pkg to scheduler.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha committed Jun 22, 2017
1 parent 94e8dac commit 4ce7c50
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
14 changes: 7 additions & 7 deletions crond.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
},
}
Expand Down
37 changes: 0 additions & 37 deletions pkg/cron/events.go

This file was deleted.

30 changes: 15 additions & 15 deletions pkg/cron/controller.go → pkg/scheduler/controller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cron
package scheduler

import (
"errors"
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -88,7 +88,7 @@ func (c *controller) RunAndHold() {
)
log.Errorln(err)
} else {
crondSuccessfullyAdded()
schedulerSuccessfullyAdded()
}
}
}
Expand All @@ -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,
Expand All @@ -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()
}
}
},
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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())
Expand Down
37 changes: 37 additions & 0 deletions pkg/scheduler/events.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 4ce7c50

Please sign in to comment.