Skip to content

Commit e25d451

Browse files
jkt628ddragosd
authored andcommitted
when a new directory is detected watch the entire subtree (#3)
* update to latest fsnotify and alpinelinux * add debug to watch for possible missing fsnotify events * excise dead code * WatchFolderRecursive and RecursiveWatcher.Run race for fsnotify Events causing inconsistent behavior RecursiveWatcher.Files had no receiver so RecursiveWatcher.Run eventually deadlocks * when a new directory detected watch the entire subtree * test creating directory for RecursiveWatcher
1 parent 05fcbb5 commit e25d451

File tree

214 files changed

+108851
-438
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+108851
-438
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.dockerignore
2+
Dockerfile
3+
.git*
4+
api-gateway-config-supervisor
5+
*.md

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/bin
2-
/pkg
2+
/pkg
3+
api-gateway-config-supervisor

Dockerfile

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,38 @@
66
#
77
FROM alpine:latest
88

9-
ENV GOPATH /usr/lib/go/bin
10-
ENV GOBIN /usr/lib/go/bin
11-
ENV PATH $PATH:/usr/lib/go/bin
9+
ENV GOPATH=/tmp/go \
10+
GOBIN=/usr/lib/go/bin \
11+
PATH=$PATH:/usr/lib/go/bin
1212

13+
COPY . ${GOPATH}/src/github.com/adobe-apiplatform/api-gateway-config-supervisor
1314

14-
RUN mkdir -p /tmp/go
15-
ADD . /tmp/go
16-
RUN echo "http://dl-4.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
15+
RUN echo " installing aws-cli ..." \
1716
&& apk update \
18-
&& apk add make git go \
17+
&& apk add python \
18+
&& apk add py-pip \
19+
&& pip install --upgrade pip \
20+
&& pip install awscli \
21+
&& apk del py-pip \
1922

2023
&& echo " building local project ... " \
21-
&& cd /tmp/go \
24+
&& apk add --virtual .build-deps make git gcc libc-dev go \
25+
&& cd ${GOPATH}/src/github.com/adobe-apiplatform/api-gateway-config-supervisor \
2226
&& make setup \
23-
&& mkdir -p /tmp/go/Godeps/_workspace \
24-
&& ln -s /tmp/go/vendor /tmp/go/Godeps/_workspace/src \
25-
&& mkdir -p /tmp/go-src/src/github.com/adobe-apiplatform \
26-
&& ln -s /tmp/go /tmp/go-src/src/github.com/adobe-apiplatform/api-gateway-config-supervisor \
27-
&& GOPATH=/tmp/go/vendor:/tmp/go-src CGO_ENABLED=0 GOOS=linux /usr/lib/go/bin/godep go test \
28-
&& GOPATH=/tmp/go/vendor:/tmp/go-src CGO_ENABLED=0 GOOS=linux /usr/lib/go/bin/godep go build -ldflags "-s" -a -installsuffix cgo -o api-gateway-config-supervisor ./ \
29-
&& cp /tmp/go/api-gateway-config-supervisor /usr/lib/go/bin \
27+
&& godep go test \
28+
&& godep go build -ldflags "-s" -a -installsuffix cgo -o api-gateway-config-supervisor ./ \
29+
&& cp ${GOPATH}/src/github.com/adobe-apiplatform/api-gateway-config-supervisor/api-gateway-config-supervisor ${GOBIN} \
3030

3131
&& echo "installing rclone sync ... " \
3232
&& go get github.com/ncw/rclone \
3333

3434
&& echo " cleaning up ... " \
35-
&& rm -rf /usr/lib/go/bin/src \
36-
&& rm -rf /tmp/go \
37-
&& rm -rf /tmp/api-gateway-config-supervisor* \
38-
&& rm -rf /tmp/go-src \
39-
&& rm -rf /usr/lib/go/bin/pkg/ \
40-
&& rm -rf /usr/lib/go/bin/godep \
41-
&& apk del make git go \
35+
&& rm -rf ${GOBIN}/src \
36+
${GOPATH} \
37+
/tmp/api-gateway-config-supervisor* \
38+
${GOBIN}/pkg/ \
39+
${GOBIN}/godep \
40+
&& apk del .build-deps \
4241
&& rm -rf /var/cache/apk/*
4342

44-
RUN echo " installing aws-cli ..." \
45-
&& apk update \
46-
&& apk add python \
47-
&& apk add py-pip \
48-
&& pip install --upgrade pip \
49-
&& pip install awscli
50-
51-
ENTRYPOINT ["api-gateway-config-supervisor"]
43+
ENTRYPOINT ["api-gateway-config-supervisor"]

Godeps/Godeps.json

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
GOPATH ?= `pwd`
22
GOBIN ?= `pwd`/bin
33
GOOS ?= $(`uname -a | awk '{print tolower($1)}'`)
4+
.PHONY: setup install test format static docker docker-ssh
45
56
setup:
67
go get github.com/tools/godep
@@ -24,6 +25,5 @@ static:
2425
docker:
2526
docker build -t adobeapiplatform/api-gateway-config-supervisor .
2627
27-
.PHONY: docker-ssh
2828
docker-ssh:
2929
docker run -ti --entrypoint='/bin/sh' adobeapiplatform/api-gateway-config-supervisor:latest

config-supervisor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
syncFolder = pflag.StringP("sync-folder", "", "~/tmp/api-gateway-config", "The folder to watch for changes.")
3030
reloadCmd = pflag.StringP("reload-cmd", "", "echo reload-cmd not defined", "Command used to reload the gateway")
3131
httpAddr = pflag.StringP("http-addr", "", "127.0.0.1:8888", "Http Address exposing a /health-check for the sync process")
32+
debug = pflag.BoolP("debug", "v", false, "Print extra debug information")
3233
status = sync.GetStatusInstance()
3334
)
3435

@@ -79,7 +80,7 @@ func checkForReload() {
7980

8081
//watches for changes in the syncFolder, execute reloadCmd when there are changes
8182
func watchForFSChanges() {
82-
c := sync.WatchFolderRecursive(*syncFolder)
83+
c := sync.WatchFolderRecursive(*syncFolder, *debug)
8384
for {
8485
select {
8586
case file := <-c:

config-supervisor_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func setup(t *testing.T) (tempdir string) {
2626
// setup a sync interval for the test
2727
var sync_interval = time.Duration(time.Second * 1)
2828
syncInterval = &sync_interval
29+
// setup extra debug information
30+
debugOn := true
31+
debug = &debugOn
2932
return tmpDir
3033
}
3134

@@ -66,8 +69,12 @@ func TestThatReloadCommandExecutesOnFsChanges(t *testing.T) {
6669
// wait for some time to init
6770
time.Sleep(500 * time.Millisecond)
6871

69-
//modifyFS: create a new file
70-
f1, err := createFile(t, tmpDir, "new-file-content")
72+
//modifyFS: create a new directory and file
73+
dir, err := ioutil.TempDir(tmpDir, "new-dir-")
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
f1, err := createFile(t, dir, "new-file-content")
7178
if err != nil {
7279
t.Fatal(err)
7380
}

sync/recursiveWatcher.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type RecursiveWatcher struct {
16-
*fsnotify.Watcher
16+
fsw *fsnotify.Watcher
1717
Files chan string
1818
Folders chan string
1919
}
@@ -37,7 +37,7 @@ func NewRecursiveWatcher(path string) (*RecursiveWatcher, error) {
3737
if err != nil {
3838
return nil, err
3939
}
40-
rw := &RecursiveWatcher{Watcher: watcher}
40+
rw := &RecursiveWatcher{fsw: watcher}
4141

4242
rw.Files = make(chan string, 10)
4343
rw.Folders = make(chan string, len(folders))
@@ -49,7 +49,7 @@ func NewRecursiveWatcher(path string) (*RecursiveWatcher, error) {
4949
}
5050

5151
func (watcher *RecursiveWatcher) AddFolder(folder string) {
52-
err := watcher.Add(folder)
52+
err := watcher.fsw.Add(folder)
5353
if err != nil {
5454
log.Println("Error watching: ", folder, err)
5555
}
@@ -60,7 +60,10 @@ func (watcher *RecursiveWatcher) Run(debug bool) {
6060
go func() {
6161
for {
6262
select {
63-
case event := <-watcher.Events:
63+
case event := <-watcher.fsw.Events:
64+
if debug {
65+
log.Println("Event: ", event)
66+
}
6467
// create a file/directory
6568
if event.Op&fsnotify.Create == fsnotify.Create {
6669
fi, err := os.Stat(event.Name)
@@ -73,8 +76,8 @@ func (watcher *RecursiveWatcher) Run(debug bool) {
7376
if debug {
7477
DebugMessage("Detected new directory %s", event.Name)
7578
}
76-
if !shouldIgnoreFile(filepath.Base(event.Name)) {
77-
watcher.AddFolder(event.Name)
79+
for _, folder := range Subfolders(event.Name) {
80+
watcher.AddFolder(folder)
7881
}
7982
} else {
8083
if debug {
@@ -92,7 +95,7 @@ func (watcher *RecursiveWatcher) Run(debug bool) {
9295
watcher.Files <- event.Name
9396
}
9497

95-
case err := <-watcher.Errors:
98+
case err := <-watcher.fsw.Errors:
9699
log.Println("error", err)
97100
}
98101
}

sync/runner.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
"os"
88
"os/exec"
99
"strings"
10-
"sync"
1110
)
1211

13-
func executeCmd(cmd string, wg *sync.WaitGroup) {
12+
func executeCmd(cmd string) {
1413
// splitting head => g++ parts => rest of the command
1514
parts := strings.Fields(cmd)
1615
head := parts[0]
@@ -56,13 +55,9 @@ func executeCmd(cmd string, wg *sync.WaitGroup) {
5655
}
5756

5857
log.Println("done")
59-
wg.Done() // Need to signal to waitgroup that this goroutine is done
6058
}
6159

6260
func Execute(syncCmd string) {
6361
log.Println("Executing sync cmd:", syncCmd)
64-
wg := new(sync.WaitGroup)
65-
wg.Add(1)
66-
executeCmd(syncCmd, wg)
67-
wg.Wait()
62+
executeCmd(syncCmd)
6863
}

sync/watcher.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,26 @@ package sync
22

33
import (
44
"github.com/koyachi/go-term-ansicolor/ansicolor"
5-
"gopkg.in/fsnotify.v1"
65
"log"
76
)
87

9-
type FSWatcher struct {
10-
*fsnotify.Watcher
11-
Files chan string
12-
Folders chan string
13-
Changes <-chan string
14-
}
15-
168
// Watches the specific folder for changes
179
// When a change happens it triggers a notification on a channel
18-
func WatchFolderRecursive(folder string) <-chan string {
10+
func WatchFolderRecursive(folder string, debug bool) <-chan string {
1911
done := make(chan string)
2012
go func() {
2113
watcher, err := NewRecursiveWatcher(folder)
2214
if err != nil {
2315
log.Fatal(err)
2416
}
25-
watcher.Run(false)
26-
defer watcher.Close()
17+
watcher.Run(debug)
18+
defer watcher.fsw.Close()
2719

2820
for {
2921
select {
30-
case event := <-watcher.Events:
31-
done <- event.Name
32-
log.Println(ansicolor.IntenseBlack("FS Changed"), ansicolor.Underline(event.Name))
22+
case file := <-watcher.Files:
23+
done <- file
24+
log.Println(ansicolor.IntenseBlack("FS Changed"), ansicolor.Underline(file))
3325
case folder := <-watcher.Folders:
3426
log.Println(ansicolor.Yellow("Watching path"), ansicolor.Yellow(folder))
3527
}

vendor/github.com/spf13/pflag/.travis.yml

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/spf13/pflag/README.md

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)