Added polling for new project files

This commit is contained in:
Tim Shannon 2016-04-01 21:23:11 +00:00
parent 0472b31877
commit 38d20d46fe
2 changed files with 75 additions and 20 deletions

View File

@ -24,13 +24,13 @@ func (p *Project) errHandled(err error) bool {
} }
if p.ds == nil { if p.ds == nil {
log.Printf("Error in project %s: %s", p.id(), err) log.Printf("Error in project %s: %s\n", p.id(), err)
return true return true
} }
defer func() { defer func() {
err = p.ds.Close() err = p.ds.Close()
if err != nil { if err != nil {
log.Printf("Error closing the datastore for project %s: %s", p.id(), err) log.Printf("Error closing the datastore for project %s: %s\n", p.id(), err)
} }
p.ds = nil p.ds = nil
@ -38,7 +38,7 @@ func (p *Project) errHandled(err error) bool {
if p.version != "" { if p.version != "" {
err = os.RemoveAll(p.verDir()) err = os.RemoveAll(p.verDir())
log.Printf("Error deleting the version directory project %s version %s: %s", log.Printf("Error deleting the version directory project %s version %s: %s\n",
p.id(), p.version, err) p.id(), p.version, err)
} }

View File

@ -6,6 +6,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"log"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
@ -28,6 +29,8 @@ const (
stageRelease = "release" stageRelease = "release"
) )
const projectFilePoll = 30 * time.Second
// Project is an ironsmith project that contains how to fetch, build, test, and release a project // Project is an ironsmith project that contains how to fetch, build, test, and release a project
/* /*
The project lifecycle goes like this, each step calling the next if successful The project lifecycle goes like this, each step calling the next if successful
@ -114,9 +117,6 @@ var projects = projectList{
} }
func (p *projectList) load() error { func (p *projectList) load() error {
p.Lock()
defer p.Unlock()
dir, err := os.Open(filepath.Join(projectDir, enabledProjectDir)) dir, err := os.Open(filepath.Join(projectDir, enabledProjectDir))
defer func() { defer func() {
if cerr := dir.Close(); cerr != nil && err == nil { if cerr := dir.Close(); cerr != nil && err == nil {
@ -135,26 +135,15 @@ func (p *projectList) load() error {
for i := range files { for i := range files {
if !files[i].IsDir() && filepath.Ext(files[i].Name()) == ".json" { if !files[i].IsDir() && filepath.Ext(files[i].Name()) == ".json" {
prj := &Project{ p.add(files[i].Name())
filename: files[i].Name(), }
Name: files[i].Name(),
stage: stageLoad,
} }
p.data[files[i].Name()] = prj
prj.load() time.AfterFunc(projectFilePoll, startProjectLoader)
}
}
return nil return nil
} }
func (p *projectList) remove(name string) {
p.Lock()
delete(p.data, name)
p.Unlock()
}
func (p *projectList) exists(name string) bool { func (p *projectList) exists(name string) bool {
p.RLock() p.RLock()
defer p.RUnlock() defer p.RUnlock()
@ -163,7 +152,73 @@ func (p *projectList) exists(name string) bool {
return ok return ok
} }
func (p *projectList) add(name string) {
p.Lock()
defer p.Unlock()
prj := &Project{
filename: name,
Name: name,
stage: stageLoad,
}
p.data[name] = prj
go func() {
prj.load()
}()
}
// removeMissing removes projects that are missing from the passed in list of names
func (p *projectList) removeMissing(names []string) {
p.Lock()
defer p.Unlock()
for i := range p.data {
found := false
for k := range names {
if names[k] == i {
found = true
}
}
if !found {
delete(p.data, i)
}
}
}
// startProjectLoader polls for new projects // startProjectLoader polls for new projects
func startProjectLoader() { func startProjectLoader() {
dir, err := os.Open(filepath.Join(projectDir, enabledProjectDir))
defer func() {
if cerr := dir.Close(); cerr != nil && err == nil {
err = cerr
}
}()
if err != nil {
log.Printf("Error in startProjectLoader opening the filepath %s: %s\n", dir, err)
return
}
files, err := dir.Readdir(0)
if err != nil {
log.Printf("Error in startProjectLoader reading the dir %s: %s\n", dir, err)
return
}
names := make([]string, len(files))
for i := range files {
if !files[i].IsDir() && filepath.Ext(files[i].Name()) == ".json" {
names[i] = files[i].Name()
if !projects.exists(files[i].Name()) {
projects.add(files[i].Name())
}
}
}
//check for removed projects
projects.removeMissing(names)
time.AfterFunc(projectFilePoll, startProjectLoader)
} }