Skip to content

Commit

Permalink
Jitter watch option
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Dec 28, 2022
1 parent 9fece50 commit a12d296
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ You can override this using the [`--config` flag or `CONFIG` env var with `serve
watch:
workers: 10
schedule: "0 */6 * * *"
jitter: 30s
firstCheckNotif: false

notif:
Expand Down Expand Up @@ -121,6 +122,7 @@ All configuration from file can be transposed into environment variables. As an
watch:
workers: 10
schedule: "0 */6 * * *"
jitter: 30s
firstCheckNotif: false

notif:
Expand Down Expand Up @@ -169,6 +171,7 @@ Can be transposed to:

DIUN_WATCH_WORKERS=10
DIUN_WATCH_SCHEDULE=0 */6 * * *
DIUN_WATCH_JITTER=30s
DIUN_WATCH_FIRSTCHECKNOTIF=false

DIUN_NOTIF_GOTIFY_ENDPOINT=http://gotify.foo.com
Expand Down
19 changes: 19 additions & 0 deletions docs/config/watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
watch:
workers: 10
schedule: "0 */6 * * *"
jitter: 30s
firstCheckNotif: false
compareDigest: true
healthchecks:
Expand Down Expand Up @@ -44,6 +45,24 @@ Maximum number of workers that will execute tasks concurrently. (default `10`)
!!! abstract "Environment variables"
* `DIUN_WATCH_SCHEDULE`

### `jitter`

Enable time jitter. Prior to executing of a job, cron will sleep a random
duration in the range from 0 to _jitter_. (default `30s`)

!!! note
Only works with `schedule` setting. `0` disables time jitter.

!!! example "Config file"
```yaml
watch:
schedule: "0 */6 * * *"
jitter: 30s
```

!!! abstract "Environment variables"
* `DIUN_WATCH_JITTER`

### `firstCheckNotif`

Send notification at the very first analysis of an image. (default `false`)
Expand Down
3 changes: 3 additions & 0 deletions docs/install/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ services:
- "LOG_JSON=false"
- "DIUN_WATCH_WORKERS=20"
- "DIUN_WATCH_SCHEDULE=0 */6 * * *"
- "DIUN_WATCH_JITTER=30s"
- "DIUN_PROVIDERS_DOCKER=true"
labels:
- "diun.enable=true"
Expand All @@ -82,6 +83,7 @@ docker run -d --name diun \
-e "LOG_JSON=false" \
-e "DIUN_WATCH_WORKERS=20" \
-e "DIUN_WATCH_SCHEDULE=0 */6 * * *" \
-e "DIUN_WATCH_JITTER=30s" \
-e "DIUN_PROVIDERS_DOCKER=true" \
-v "$(pwd)/data:/data" \
-v "/var/run/docker.sock:/var/run/docker.sock" \
Expand Down Expand Up @@ -153,6 +155,7 @@ services:
- "LOG_JSON=false"
- "DIUN_WATCH_WORKERS=20"
- "DIUN_WATCH_SCHEDULE=0 */6 * * *"
- "DIUN_WATCH_JITTER=30s"
- "DIUN_PROVIDERS_DOCKER=true"
labels:
- "diun.enable=true"
Expand Down
1 change: 1 addition & 0 deletions docs/providers/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ services:
- "LOG_JSON=false"
- "DIUN_WATCH_WORKERS=20"
- "DIUN_WATCH_SCHEDULE=0 */6 * * *"
- "DIUN_WATCH_JITTER=30s"
- "DIUN_PROVIDERS_DOCKER=true"
restart: always

Expand Down
2 changes: 2 additions & 0 deletions docs/providers/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ spec:
value: "20"
- name: DIUN_WATCH_SCHEDULE
value: "0 */6 * * *"
- name: DIUN_WATCH_JITTER
value: "30s"
- name: DIUN_PROVIDERS_KUBERNETES
value: "true"
volumeMounts:
Expand Down
1 change: 1 addition & 0 deletions docs/providers/swarm.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ services:
- "LOG_JSON=false"
- "DIUN_WATCH_WORKERS=20"
- "DIUN_WATCH_SCHEDULE=0 */6 * * *"
- "DIUN_WATCH_JITTER=30s"
- "DIUN_PROVIDERS_SWARM=true"
deploy:
mode: replicated
Expand Down
1 change: 1 addition & 0 deletions docs/usage/basic-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
- "LOG_JSON=false"
- "DIUN_WATCH_WORKERS=20"
- "DIUN_WATCH_SCHEDULE=0 */6 * * *"
- "DIUN_WATCH_JITTER=30s"
- "DIUN_PROVIDERS_DOCKER=true"
- "DIUN_PROVIDERS_DOCKER_WATCHBYDEFAULT=true"
restart: always
Expand Down
1 change: 1 addition & 0 deletions docs/user-guides/docker-file-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
- "LOG_JSON=false"
- "DIUN_WATCH_WORKERS=20"
- "DIUN_WATCH_SCHEDULE=0 */6 * * *"
- "DIUN_WATCH_JITTER=30s"
- "DIUN_PROVIDERS_DOCKER=true"
- "DIUN_PROVIDERS_DOCKER_WATCHBYDEFAULT=true"
- "DIUN_PROVIDERS_FILE_FILENAME=/custom-images.yml"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/bmatcuk/doublestar/v3 v3.0.0
github.com/containerd/containerd v1.6.14
github.com/containers/image/v5 v5.23.1
github.com/crazy-max/cron/v3 v3.1.1
github.com/crazy-max/gohealthchecks v0.3.0
github.com/crazy-max/gonfig v0.6.0
github.com/docker/docker v20.10.18+incompatible
Expand All @@ -32,7 +33,6 @@ require (
github.com/panjf2000/ants/v2 v2.7.1
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.7.0
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.28.0
github.com/russross/blackfriday/v2 v2.1.0
github.com/sirupsen/logrus v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfc
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/crazy-max/cron/v3 v3.1.1 h1:8tsAXjV522daYSaN6/Mb/Nh8X/Ez+nedU0KuPV98rNU=
github.com/crazy-max/cron/v3 v3.1.1/go.mod h1:yexi3kKoh3GcnmRCppyJKsvYIBWzDVaym0dwNWo+zdg=
github.com/crazy-max/gohealthchecks v0.3.0 h1:mXtTsOinEOcvRcnT3ayRkxGdK18LfrFOWE/gFpfzEI4=
github.com/crazy-max/gohealthchecks v0.3.0/go.mod h1:gkT8QSdEXZJahyswdTGDbd+q20fWm0DmWW7TWBNtgJg=
github.com/crazy-max/gonfig v0.6.0 h1:CwP+x1gfor4Ze8lLQKkZw/DOQEMVgwBcoocTYLo3c4A=
Expand Down Expand Up @@ -762,8 +764,6 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
4 changes: 2 additions & 2 deletions internal/app/diun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync/atomic"
"time"

"github.com/crazy-max/cron/v3"
"github.com/crazy-max/diun/v4/internal/config"
"github.com/crazy-max/diun/v4/internal/db"
"github.com/crazy-max/diun/v4/internal/grpc"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/hako/durafmt"
"github.com/panjf2000/ants/v2"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -110,7 +110,7 @@ func (di *Diun) Start() error {
if len(di.cfg.Watch.Schedule) == 0 {
return nil
}
di.jobID, err = di.cron.AddJob(di.cfg.Watch.Schedule, di)
di.jobID, err = di.cron.AddJobWithJitter(di.cfg.Watch.Schedule, di, *di.cfg.Watch.Jitter)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestLoadFile(t *testing.T) {
Watch: &model.Watch{
Workers: 100,
Schedule: "*/30 * * * *",
Jitter: utl.NewDuration(30 * time.Second),
FirstCheckNotif: utl.NewTrue(),
CompareDigest: utl.NewTrue(),
Healthchecks: &model.Healthchecks{
Expand Down
1 change: 1 addition & 0 deletions internal/config/fixtures/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ db:
watch:
workers: 100
schedule: "*/30 * * * *"
jitter: 30s
firstCheckNotif: true
compareDigest: true
healthchecks:
Expand Down
1 change: 1 addition & 0 deletions internal/config/fixtures/config.validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ db:
watch:
workers: 100
schedule: "*/30 * * * *"
jitter: 30s
firstCheckNotif: false
compareDigest: true
healthchecks:
Expand Down
14 changes: 9 additions & 5 deletions internal/model/watch.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package model

import (
"time"

"github.com/crazy-max/diun/v4/pkg/utl"
)

// Watch holds data necessary for watch configuration
type Watch struct {
Workers int `yaml:"workers,omitempty" json:"workers,omitempty" validate:"required,min=1"`
Schedule string `yaml:"schedule,omitempty" json:"schedule,omitempty"`
FirstCheckNotif *bool `yaml:"firstCheckNotif,omitempty" json:"firstCheckNotif,omitempty" validate:"required"`
CompareDigest *bool `yaml:"compareDigest,omitempty" json:"compareDigest,omitempty" validate:"required"`
Healthchecks *Healthchecks `yaml:"healthchecks,omitempty" json:"healthchecks,omitempty"`
Workers int `yaml:"workers,omitempty" json:"workers,omitempty" validate:"required,min=1"`
Schedule string `yaml:"schedule,omitempty" json:"schedule,omitempty"`
Jitter *time.Duration `yaml:"jitter,omitempty" json:"jitter,omitempty" validate:"required"`
FirstCheckNotif *bool `yaml:"firstCheckNotif,omitempty" json:"firstCheckNotif,omitempty" validate:"required"`
CompareDigest *bool `yaml:"compareDigest,omitempty" json:"compareDigest,omitempty" validate:"required"`
Healthchecks *Healthchecks `yaml:"healthchecks,omitempty" json:"healthchecks,omitempty"`
}

// GetDefaults gets the default values
Expand All @@ -23,6 +26,7 @@ func (s *Watch) GetDefaults() *Watch {
// SetDefaults sets the default values
func (s *Watch) SetDefaults() {
s.Workers = 10
s.Jitter = utl.NewDuration(30 * time.Second)
s.FirstCheckNotif = utl.NewFalse()
s.CompareDigest = utl.NewTrue()
}
1 change: 1 addition & 0 deletions test/docker2/diun.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
watch:
workers: 20
schedule: "0 */6 * * *"
jitter: 5m

providers:
docker:
Expand Down
1 change: 1 addition & 0 deletions test/docker3/diun.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
watch:
workers: 20
schedule: "0 */6 * * *"
jitter: 0

providers:
docker:
Expand Down

0 comments on commit a12d296

Please sign in to comment.