Skip to content

Commit f195715

Browse files
authored
Merge pull request #108 from fmotalleb/debuncer
feat: debouncer support (per-job)
2 parents 0a77fe0 + 7a2e969 commit f195715

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

config.local.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
jobs:
55
- name: echo
66
concurrency: 3
7+
debounce: 5s
78
tasks:
8-
- command: echo $env.t{{ .Vars.name }}
9+
- command: echo {{ .Vars.name }}
910
retry-delay: 1s
1011
retries: 0
1112
retry-jitter: 1s

config/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ type Config struct {
2323

2424
// JobConfig represents the configuration for a specific job.
2525
type JobConfig struct {
26-
Name string `mapstructure:"name" json:"name,omitempty"`
27-
Description string `mapstructure:"description" json:"description,omitempty"`
28-
Disabled bool `mapstructure:"disabled" json:"disabled,omitempty"`
29-
Concurrency uint `mapstructure:"concurrency" json:"concurrency,omitempty"`
30-
Tasks []Task `mapstructure:"tasks" json:"tasks,omitempty"`
31-
Events []JobEvent `mapstructure:"events" json:"events"`
32-
Hooks JobHooks `mapstructure:"hooks" json:"hooks,omitempty"`
26+
Name string `mapstructure:"name" json:"name,omitempty"`
27+
Description string `mapstructure:"description" json:"description,omitempty"`
28+
Disabled bool `mapstructure:"disabled" json:"disabled,omitempty"`
29+
Concurrency uint `mapstructure:"concurrency" json:"concurrency,omitempty"`
30+
Tasks []Task `mapstructure:"tasks" json:"tasks,omitempty"`
31+
Events []JobEvent `mapstructure:"events" json:"events"`
32+
Hooks JobHooks `mapstructure:"hooks" json:"hooks,omitempty"`
33+
Debounce time.Duration `mapstructure:"debounce" json:"debounce,omitempty"`
3334
}
3435

3536
// JobEvent represents the scheduling configuration for a job.

core/jobs/runner.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"github.com/prometheus/client_golang/prometheus"
66
"go.uber.org/zap"
77

8+
"github.com/fmotalleb/go-tools/debouncer"
9+
810
"github.com/fmotalleb/crontab-go/abstraction"
911
"github.com/fmotalleb/crontab-go/config"
1012
"github.com/fmotalleb/crontab-go/core/concurrency"
@@ -34,7 +36,10 @@ func InitializeJobs(jobs []*config.JobConfig) {
3436
if err := job.Validate(logger.Named("Validator")); err != nil {
3537
log.Panic("failed to validate job", zap.String("job", job.Name), zap.Error(err))
3638
}
37-
signal := signals.NewSync[abstraction.Event]()
39+
var signal abstraction.EventDispatcher = signals.NewSync[abstraction.Event]()
40+
if job.Debounce > 0 {
41+
signal = debouncer.NewDebouncedSignal(signal, job.Debounce)
42+
}
3843
global.CountSignals(signal,
3944
"events",
4045
"amount of events dispatched for this job",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tool (
1111
require (
1212
github.com/alecthomas/assert/v2 v2.11.0
1313
github.com/docker/docker v28.5.2+incompatible
14-
github.com/fmotalleb/go-tools v0.1.55
14+
github.com/fmotalleb/go-tools v0.1.56
1515
github.com/joho/godotenv v1.5.1
1616
github.com/labstack/echo/v4 v4.13.4
1717
github.com/maniartech/signals v1.3.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
120120
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
121121
github.com/firefart/nonamedreturns v1.0.5 h1:tM+Me2ZaXs8tfdDw3X6DOX++wMCOqzYUho6tUTYIdRA=
122122
github.com/firefart/nonamedreturns v1.0.5/go.mod h1:gHJjDqhGM4WyPt639SOZs+G89Ko7QKH5R5BhnO6xJhw=
123-
github.com/fmotalleb/go-tools v0.1.55 h1:N+ixt/1HUG2QIzlAVDx8rNSi+rm4u6Ixxps2vIqlSlo=
124-
github.com/fmotalleb/go-tools v0.1.55/go.mod h1:r2cMGeVPNSHh491qpXo1gLnYQgrvIN8Wh5eak9nDV+A=
123+
github.com/fmotalleb/go-tools v0.1.56 h1:pDsl2YMdcy4IxSqE3NTFiHLvUyDNIOvVc4E8CFUCJOQ=
124+
github.com/fmotalleb/go-tools v0.1.56/go.mod h1:AT8piibVXbHZ4jMyx750eTKDOZd+W5aEDsntPeyIA+Y=
125125
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
126126
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
127127
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=

schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
"type": "boolean",
3939
"description": "An optional boolean that indicates whether the job is disabled or not."
4040
},
41+
"debounce": {
42+
"type": "string",
43+
"description": "Debounce duration. Every new event is dispatched immediately, and an event is guaranteed after the debounce interval elapses.",
44+
"examples": [
45+
"1s",
46+
"10m"
47+
]
48+
},
4149
"tasks": {
4250
"type": "array",
4351
"items": {

0 commit comments

Comments
 (0)