Skip to content

Commit

Permalink
Switch to gocron v2 and fix flaky ci (#198)
Browse files Browse the repository at this point in the history
* Switch to gocron v2 and fix flaky ci

* Fix scheduler var name

* Revert chart bump
  • Loading branch information
adriananeci authored May 17, 2024
1 parent 1b3f414 commit bd6f9b0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 66 deletions.
47 changes: 39 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ governing permissions and limitations under the License.
package cmd

import (
"github.com/google/uuid"
"strings"
"time"

Expand All @@ -20,7 +21,7 @@ import (
"github.com/adobe/k8s-shredder/pkg/metrics"
"github.com/adobe/k8s-shredder/pkg/utils"
"github.com/fsnotify/fsnotify"
"github.com/go-co-op/gocron"
"github.com/go-co-op/gocron/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -32,7 +33,7 @@ var (
metricsPort int
cfg config.Config
appContext *utils.AppContext
scheduler *gocron.Scheduler
scheduler gocron.Scheduler

rootCmd = &cobra.Command{
Use: "k8s-shredder",
Expand Down Expand Up @@ -163,21 +164,51 @@ func preRun(cmd *cobra.Command, args []string) {
}

func run(cmd *cobra.Command, args []string) {
scheduler = gocron.NewScheduler(time.UTC)
var err error
scheduler, err = gocron.NewScheduler(gocron.WithLocation(time.UTC))
defer func() { _ = scheduler.Shutdown() }()

if err != nil {
log.Fatalf("Failed to create scheduler: %s", err)
}

h := handler.NewHandler(appContext)
_, err := scheduler.Every(cfg.EvictionLoopInterval).Do(h.Run)

job, err := scheduler.NewJob(
gocron.DurationJob(
cfg.EvictionLoopInterval,
),
gocron.NewTask(
h.Run,
),
)

if err != nil {
log.Fatalf("Failed to start scheduler: %s", err)
log.Fatalf("Failed to configure scheduler's job: %s", err)
}

scheduler.StartAsync()
// each job has a unique id
log.Infof("Configured scheduler job with ID: %s", job.ID())

activeJobs := make([]uuid.UUID, 0)
for _, j := range scheduler.Jobs() {
activeJobs = append(activeJobs, j.ID())
}
log.Infoln("Active jobs:", activeJobs)

scheduler.Start()
log.Info("Scheduler started, happy shredding!")
select {}
}

func reset() {
// clear all running jobs and stop the scheduler
scheduler.Clear()
scheduler.Stop()
err := scheduler.StopJobs()
if err != nil {
log.Errorf("Failed to stop running jobs: %s", err)
}
err = scheduler.Shutdown()
if err != nil {
log.Errorf("Failed to shutdown scheduler: %s", err)
}
}
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
module github.com/adobe/k8s-shredder

go 1.22.0

toolchain go1.22.3
go 1.22.3

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/go-co-op/gocron v1.37.0
github.com/go-co-op/gocron/v2 v2.5.0
github.com/google/uuid v1.6.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/common v0.53.0
Expand All @@ -28,7 +26,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -39,10 +37,10 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -62,11 +60,9 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
Expand All @@ -88,5 +84,5 @@ require (
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit bd6f9b0

Please sign in to comment.