Skip to content

Commit

Permalink
race conditions on scandir/block
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Sep 16, 2016
1 parent 605f95e commit 241b6e9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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)

Expand Down
3 changes: 2 additions & 1 deletion block.go
Expand Up @@ -8,7 +8,7 @@ import (
"syscall"
"time"

"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

// Block stop until signal received
Expand All @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions 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
Expand Down
6 changes: 6 additions & 0 deletions scandir.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"regexp"
"time"
)

// Scan return func() to work with the scheduler
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions start.go
Expand Up @@ -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))
Expand Down

0 comments on commit 241b6e9

Please sign in to comment.