Skip to content

Commit bd6f9b0

Browse files
authored
Switch to gocron v2 and fix flaky ci (#198)
* Switch to gocron v2 and fix flaky ci * Fix scheduler var name * Revert chart bump
1 parent 1b3f414 commit bd6f9b0

File tree

4 files changed

+93
-66
lines changed

4 files changed

+93
-66
lines changed

Diff for: cmd/root.go

+39-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ governing permissions and limitations under the License.
1212
package cmd
1313

1414
import (
15+
"github.com/google/uuid"
1516
"strings"
1617
"time"
1718

@@ -20,7 +21,7 @@ import (
2021
"github.com/adobe/k8s-shredder/pkg/metrics"
2122
"github.com/adobe/k8s-shredder/pkg/utils"
2223
"github.com/fsnotify/fsnotify"
23-
"github.com/go-co-op/gocron"
24+
"github.com/go-co-op/gocron/v2"
2425
log "github.com/sirupsen/logrus"
2526
"github.com/spf13/cobra"
2627
"github.com/spf13/viper"
@@ -32,7 +33,7 @@ var (
3233
metricsPort int
3334
cfg config.Config
3435
appContext *utils.AppContext
35-
scheduler *gocron.Scheduler
36+
scheduler gocron.Scheduler
3637

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

165166
func run(cmd *cobra.Command, args []string) {
166-
scheduler = gocron.NewScheduler(time.UTC)
167+
var err error
168+
scheduler, err = gocron.NewScheduler(gocron.WithLocation(time.UTC))
169+
defer func() { _ = scheduler.Shutdown() }()
170+
171+
if err != nil {
172+
log.Fatalf("Failed to create scheduler: %s", err)
173+
}
167174

168175
h := handler.NewHandler(appContext)
169-
_, err := scheduler.Every(cfg.EvictionLoopInterval).Do(h.Run)
176+
177+
job, err := scheduler.NewJob(
178+
gocron.DurationJob(
179+
cfg.EvictionLoopInterval,
180+
),
181+
gocron.NewTask(
182+
h.Run,
183+
),
184+
)
185+
170186
if err != nil {
171-
log.Fatalf("Failed to start scheduler: %s", err)
187+
log.Fatalf("Failed to configure scheduler's job: %s", err)
172188
}
173189

174-
scheduler.StartAsync()
190+
// each job has a unique id
191+
log.Infof("Configured scheduler job with ID: %s", job.ID())
175192

193+
activeJobs := make([]uuid.UUID, 0)
194+
for _, j := range scheduler.Jobs() {
195+
activeJobs = append(activeJobs, j.ID())
196+
}
197+
log.Infoln("Active jobs:", activeJobs)
198+
199+
scheduler.Start()
200+
log.Info("Scheduler started, happy shredding!")
176201
select {}
177202
}
178203

179204
func reset() {
180205
// clear all running jobs and stop the scheduler
181-
scheduler.Clear()
182-
scheduler.Stop()
206+
err := scheduler.StopJobs()
207+
if err != nil {
208+
log.Errorf("Failed to stop running jobs: %s", err)
209+
}
210+
err = scheduler.Shutdown()
211+
if err != nil {
212+
log.Errorf("Failed to shutdown scheduler: %s", err)
213+
}
183214
}

Diff for: go.mod

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
module github.com/adobe/k8s-shredder
22

3-
go 1.22.0
4-
5-
toolchain go1.22.3
3+
go 1.22.3
64

75
require (
86
github.com/fsnotify/fsnotify v1.7.0
9-
github.com/go-co-op/gocron v1.37.0
107
github.com/go-co-op/gocron/v2 v2.5.0
8+
github.com/google/uuid v1.6.0
119
github.com/pkg/errors v0.9.1
1210
github.com/prometheus/client_golang v1.19.1
1311
github.com/prometheus/common v0.53.0
@@ -28,7 +26,7 @@ require (
2826
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2927
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3028
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
31-
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
29+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
3230
github.com/go-errors/errors v1.4.2 // indirect
3331
github.com/go-logr/logr v1.4.1 // indirect
3432
github.com/go-openapi/jsonpointer v0.19.6 // indirect
@@ -39,10 +37,10 @@ require (
3937
github.com/google/gnostic-models v0.6.8 // indirect
4038
github.com/google/gofuzz v1.2.0 // indirect
4139
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
42-
github.com/google/uuid v1.6.0 // indirect
4340
github.com/hashicorp/hcl v1.0.0 // indirect
4441
github.com/imdario/mergo v0.3.6 // indirect
4542
github.com/inconshreveable/mousetrap v1.1.0 // indirect
43+
github.com/jonboulle/clockwork v0.4.0 // indirect
4644
github.com/josharian/intern v1.0.0 // indirect
4745
github.com/json-iterator/go v1.1.12 // indirect
4846
github.com/magiconair/properties v1.8.7 // indirect
@@ -62,11 +60,9 @@ require (
6260
github.com/spf13/afero v1.11.0 // indirect
6361
github.com/spf13/cast v1.6.0 // indirect
6462
github.com/spf13/pflag v1.0.5 // indirect
65-
github.com/stretchr/testify v1.9.0 // indirect
6663
github.com/subosito/gotenv v1.6.0 // indirect
6764
github.com/xlab/treeprint v1.2.0 // indirect
6865
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
69-
go.uber.org/atomic v1.10.0 // indirect
7066
go.uber.org/multierr v1.11.0 // indirect
7167
golang.org/x/net v0.23.0 // indirect
7268
golang.org/x/oauth2 v0.18.0 // indirect
@@ -88,5 +84,5 @@ require (
8884
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
8985
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
9086
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
91-
sigs.k8s.io/yaml v1.4.0 // indirect
87+
sigs.k8s.io/yaml v1.3.0 // indirect
9288
)

0 commit comments

Comments
 (0)