Skip to content

Commit f59341e

Browse files
committed
Merge pull request #156 from gigablah/watcher
Add -wait parameter to debounce watched events
2 parents dce53d7 + 0c06a92 commit f59341e

File tree

5 files changed

+441
-69
lines changed

5 files changed

+441
-69
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Options:
113113
show version
114114
-watch
115115
watch for container changes
116+
-wait
117+
minimum (and/or maximum) duration to wait after each container change before triggering
116118
117119
Arguments:
118120
template - path to a template to generate
@@ -153,6 +155,9 @@ path to a template to generate
153155
watch = true
154156
watch for container changes
155157
158+
wait = "500ms:2s"
159+
debounce changes with a min:max duration. Only applicable if watch = true
160+
156161
157162
[config.NotifyContainers]
158163
Starts a notify container section
@@ -180,6 +185,7 @@ watch = true
180185
template = "/etc/docker-gen/templates/nginx.tmpl"
181186
dest = "/etc/nginx/conf.d/default.conf"
182187
watch = true
188+
wait = "500ms:2s"
183189
184190
[config.NotifyContainers]
185191
nginx = 1 # 1 is a signal number to be sent; here SIGINT

cmd/docker-gen/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
buildVersion string
2020
version bool
2121
watch bool
22+
wait string
2223
notifyCmd string
2324
notifyOutput bool
2425
notifySigHUPContainerID string
@@ -85,6 +86,7 @@ func initFlags() {
8586
}
8687
flag.BoolVar(&version, "version", false, "show version")
8788
flag.BoolVar(&watch, "watch", false, "watch for container changes")
89+
flag.StringVar(&wait, "wait", "", "minimum and maximum durations to wait (e.g. \"500ms:2s\") before triggering generate")
8890
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
8991

9092
flag.BoolVar(&onlyPublished, "only-published", false,
@@ -127,10 +129,15 @@ func main() {
127129
}
128130
}
129131
} else {
132+
w, err := dockergen.ParseWait(wait)
133+
if err != nil {
134+
log.Fatalf("error parsing wait interval: %s\n", err)
135+
}
130136
config := dockergen.Config{
131137
Template: flag.Arg(0),
132138
Dest: flag.Arg(1),
133139
Watch: watch,
140+
Wait: w,
134141
NotifyCmd: notifyCmd,
135142
NotifyOutput: notifyOutput,
136143
NotifyContainers: make(map[string]docker.Signal),

config.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package dockergen
22

3-
import "github.com/fsouza/go-dockerclient"
3+
import (
4+
"errors"
5+
"strings"
6+
"time"
7+
8+
"github.com/fsouza/go-dockerclient"
9+
)
410

511
type Config struct {
612
Template string
713
Dest string
814
Watch bool
15+
Wait *Wait
916
NotifyCmd string
1017
NotifyOutput bool
1118
NotifyContainers map[string]docker.Signal
@@ -31,3 +38,47 @@ func (c *ConfigFile) FilterWatches() ConfigFile {
3138
Config: configWithWatches,
3239
}
3340
}
41+
42+
type Wait struct {
43+
Min time.Duration
44+
Max time.Duration
45+
}
46+
47+
func (w *Wait) UnmarshalText(text []byte) error {
48+
wait, err := ParseWait(string(text))
49+
if err == nil {
50+
w.Min, w.Max = wait.Min, wait.Max
51+
}
52+
return err
53+
}
54+
55+
func ParseWait(s string) (*Wait, error) {
56+
if len(strings.TrimSpace(s)) < 1 {
57+
return &Wait{0, 0}, nil
58+
}
59+
60+
parts := strings.Split(s, ":")
61+
62+
var (
63+
min time.Duration
64+
max time.Duration
65+
err error
66+
)
67+
min, err = time.ParseDuration(strings.TrimSpace(parts[0]))
68+
if err != nil {
69+
return nil, err
70+
}
71+
if len(parts) > 1 {
72+
max, err = time.ParseDuration(strings.TrimSpace(parts[1]))
73+
if err != nil {
74+
return nil, err
75+
}
76+
if max < min {
77+
return nil, errors.New("Invalid wait interval: max must be larger than min")
78+
}
79+
} else {
80+
max = 4 * min
81+
}
82+
83+
return &Wait{min, max}, nil
84+
}

0 commit comments

Comments
 (0)