diff --git a/Makefile b/Makefile index 736a029..0ce484b 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ get: build: get ${GO} get -u gopkg.in/yaml.v2; - ${GO} build -ldflags "-X main.version=${VERSION}" -o ${BIN_NAME} cmd/epazote/main.go; + ${GO} build -ldflags "-X main.version=${VERSION}" -race -o ${BIN_NAME} cmd/epazote/main.go; clean: @rm -rf ${BIN_NAME} ${BIN_NAME}.debug *.out build debian diff --git a/README.md b/README.md index a583d11..0ef77a6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Download](https://api.bintray.com/packages/nbari/epazote/epazote/images/download.svg)](https://bintray.com/nbari/epazote/epazote/_latestVersion) -[![Build Status](https://travis-ci.org/epazote/epazote.svg?branch=develop)](https://travis-ci.org/epazote/epazote) +[![Build Status](https://travis-ci.org/epazote/epazote.svg?branch=master)](https://travis-ci.org/epazote/epazote) [![Coverage Status](https://coveralls.io/repos/github/epazote/epazote/badge.svg?branch=develop)](https://coveralls.io/github/epazote/epazote?branch=develop) [![Go Report Card](https://goreportcard.com/badge/github.com/epazote/epazote)](https://goreportcard.com/report/github.com/epazote/epazote) diff --git a/block.go b/block.go index 55d383b..f53cedf 100644 --- a/block.go +++ b/block.go @@ -8,7 +8,7 @@ import ( "syscall" "time" - "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v2" ) // Block stop until signal received @@ -22,6 +22,7 @@ func (e *Epazote) Block() { signalType := <-block switch signalType { case syscall.SIGUSR1, syscall.SIGUSR2: + // this creates a race condition due the map y, err := yaml.Marshal(&e) if err != nil { log.Printf("Error: %v", err) diff --git a/epazote.go b/epazote.go index 232696a..416fe86 100644 --- a/epazote.go +++ b/epazote.go @@ -1,12 +1,16 @@ package epazote -import "regexp" +import ( + "regexp" + "sync" +) // Epazote parent struct type Epazote struct { - Config Config - Services Services - debug bool + sync.Mutex `yaml:"-" json:"-"` + Config Config + Services Services + debug bool } // Config struct diff --git a/scandir.go b/scandir.go index d0c643e..40fd171 100644 --- a/scandir.go +++ b/scandir.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "regexp" + "time" ) // Scan return func() to work with the scheduler @@ -65,6 +66,7 @@ func (e *Epazote) search(root string) error { v.RetryLimit = 3 } + e.Lock() // Add/Update existing services if _, ok := e.Services[k]; !ok { e.Services[k] = v @@ -75,6 +77,10 @@ func (e *Epazote) search(root string) error { e.Services[k].status = lastStatus e.Services[k].action = lastAction } + e.Unlock() + + // race condition + time.Sleep(3 * time.Second) if e.debug { log.Printf(Green("Found epazote.yml in path: %s updating/adding service: %q"), path, k) diff --git a/start.go b/start.go index 3c25910..0e11885 100644 --- a/start.go +++ b/start.go @@ -57,6 +57,11 @@ func (e *Epazote) Start(sk Scheduler, debug bool) { sk.AddScheduler(k, GetInterval(60, v.Every), e.Supervice(v)) } + // initialize map in case of no services + if e.Services == nil { + e.Services = make(map[string]*Service) + } + if len(e.Config.Scan.Paths) > 0 { for _, v := range e.Config.Scan.Paths { sk.AddScheduler(v, GetInterval(300, e.Config.Scan.Every), e.Scan(v))